[英]Find arc's mid-point given start, end, and center (of circle) points
I'm looking for some help on how to find an arc's mid-point. 我正在寻找一些关于如何找到弧线中点的帮助。 I have the start and end points, center of circle, and radius.
我有起点和终点,圆心和半径。 I've searched everywhere online and cannot find an answer that I can convert into code anywhere.
我在网上到处搜索,无法找到我可以在任何地方转换成代码的答案。 If anyone has any ideas, please let me know.
如果有人有任何想法,请告诉我。 The following picture is what I'm trying to find (assume that the center of the circle is already found).
以下图片是我想要找到的(假设已经找到了圆心)。
Atan2() of the mean of x1,x2 and the mean of y1,y2 gives you the angle to the mid point. 平均值为x1,x2和平均值y1,y2的Atan2()给出了与中点的角度。 The mid point at the arc is therefore found as:
因此,弧的中点位于:
double c=Math.Atan2(y1+y2, x1+x2);
double x_mid=R*Math.Cos(c);
double y_mid=R*Math.Sin(c);
Note that I removed the factor of 1/2 (for the mean) from both arguments to Atan2 since that does not change the angle. 请注意,我从两个参数中删除了因子1/2(对于平均值)到Atan2,因为这不会改变角度。
Update: that this method will always find the mid point on the shortest arc between the two points on the perimeter. 更新:此方法将始终在周边两点之间的最短弧上找到中点。 That may or may not be what you need.
这可能是也可能不是你需要的。
Take the end points. 拿端点。
(x1, y1), (x2, y2)
Normalize them about the center of the circle. 将它们标准化为圆心。 Then convert to polar.
然后转换为极地。
(r, theta1), (r, theta2)
The radii will be the same. 半径将是相同的。 The center of the arc is
弧的中心是
(r, (theta2 + theta1) / 2)
Convert to Cartesian coordinates and add the coordinates of the center. 转换为笛卡尔坐标并添加中心坐标。
EDIT: something like this: 编辑:这样的事情:
def Point CenterOfArc(Point start, end, center)
let (x1, y1) = (start.x - center.x, start.y - center.y)
let (x2, y2) = (end.x - center.x, end.y - center.y)
let (r1, theta1) = (sqrt(x1^2 + y1^2), atan(y1/x1))
let (r2, theta2) = (sqrt(x2^2 + y2^2), atan(y2/x2))
if (theta1 > theta2) theta2 += 2 * pi
let (r, theta) = ((r1 + r2) / 2, (theta1 + theta2) / 2) // averaging in case of rounding error
let (x, y) = (r * cos(theta), r * sin(theta))
return (x + center.x, y + center.y)
end
EDIT2: When you convert to polar, you need to ensure that theta2 > theta1, otherwise it'll be as though the arc was backward. 编辑2:当你转换为极地时,你需要确保theta2> theta1,否则就好像弧是向后的。
EDIT3: Also, tan<sup>-1</sup>(y/x)
is the correct operation, but for many languages, you should call it as atan2(y, x)
rather than atan(y/x)
. EDIT3:另外,
tan<sup>-1</sup>(y/x)
是正确的操作,但是对于许多语言,你应该把它称为atan2(y, x)
而不是atan(y/x)
。 atan2
is designed for this use, and it avoids errors when x=0 and may give more accurate results. atan2
专为此用途而设计,它可避免x = 0时的错误,并可提供更准确的结果。
Although this function returns an approximate point, it's useful for practical purposes. 虽然此函数返回一个近似点,但它对实际用途很有用。 I just figured this one out myself and it works great.
我自己想出了这个,它很有效。
Prerequisites: 先决条件:
- An arc center of (0, 0) is assumed here, although this could be modified to work with a center point parameter - 这里假设弧中心为(0,0),尽管可以修改它以使用中心点参数
- You must know the angle at which the arc starts (270 for example) - 你必须知道弧开始的角度(例如270)
- You must know the measurement of the angle of the arc (90 degrees for example) - 您必须知道弧度角度的测量值(例如90度)
The code below is written in Objective-C: 下面的代码是用Objective-C编写的:
#define DEGREES_TO_RADIANS(degrees) ((M_PI * degrees)/ 180)
- (CGPoint)getApproximateMidPointForArcWithStartAngle:(CGFloat)startAngle andDegrees:(CGFloat)degrees {
CGFloat midPointDegrees = fmodf(startAngle + degrees / 2, 360);
CGFloat midStartAngle = midPointDegrees - .1f;
CGFloat midEndAngle = midPointDegrees + .1f;
UIBezierPath *midPointPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(0, 0) radius:self.radius startAngle:DEGREES_TO_RADIANS(midStartAngle) endAngle:DEGREES_TO_RADIANS(midEndAngle) clockwise:YES];
CGRect midPointPathFrame = CGPathGetPathBoundingBox(midPointPath.CGPath);
CGPoint approximateMidPointCenter = CGPointMake(CGRectGetMidX(midPointPathFrame), CGRectGetMidY(midPointPathFrame));
return approximateMidPointCenter;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.