我需要一些关于如何找到弧中点的帮助。 我有起点和终点、圆心以及半径。 我在网上搜索了很多地方,但找不到任何可转换为代码的答案。 如果有人有任何想法,请告诉我。下图是我试图找到的内容(假设已经找到了圆的中心)。
使用x1,x2的平均值和y1,y2的平均值来计算Atan2()函数,可以得到中点的角度。因此,在弧线上找到中点的方法如下:
double c=Math.Atan2(y1+y2, x1+x2);
double x_mid=R*Math.Cos(c);
double y_mid=R*Math.Sin(c);
获取终点。
(x1, y1), (x2, y2)
将它们关于圆心归一化。然后转换为极坐标。
(r, theta1), (r, theta2)
圆弧的半径将相同。 圆弧的中心是
(r, (theta2 + theta1) / 2)
将坐标转换为笛卡尔坐标并加上中心坐标。
编辑:类似于这样:
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
编辑2:当你转换为极坐标时,需要确保theta2>theta1,否则就像弧线是向后的。
编辑3:此外,tan<sup>-1</sup>(y/x)
是正确的操作,但对于许多语言,您应该将其称为atan2(y, x)
而不是atan(y/x)
。atan2
是为此用途设计的,并且在x=0时避免错误并可能提供更准确的结果。
let (
x2 , y2) = (end.x - center.x, end.y - center.y)
- Michaël Corriveau-Côté虽然此函数返回的是一个近似点,但它对实际目的非常有用。我自己刚刚弄明白了这个问题,它运行得很好。
先决条件:
- 这里假定圆弧中心为 (0, 0),尽管可以修改为使用中心点参数
- 您必须知道圆弧开始的角度(例如270)
- 您必须知道圆弧的角度测量值(例如90度)
以下代码是用 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;
}