【问题标题】:I have a line from the center point of a circle to another point. I want to find the point where the line intersects the circumference of the circle我有一条从圆的中心点到另一个点的线。我想找到线与圆的圆周相交的点
【发布时间】:2012-08-04 20:09:46
【问题描述】:

我已经尝试了几种不同的解决方案,但到目前为止都没有运气。

- (CGPoint)contractLineTemp:(CGPoint)point :(CGPoint)circle :(float)circleRadius { 
CGFloat x,y;
x = point.x - circle.x;
y = point.y - circle.y;

CGFloat theta = atan2(x, y);

CGPoint newPoint;

newPoint.x = circle.x + circleRadius * sin(theta);
newPoint.y = circle.y + circleRadius * cos(theta);

return newPoint;
}


- (CGPoint)contractLineTemp:(CGPoint)startPoint :(CGPoint)endPoint :(float)scaleBy {

float dx = endPoint.x - startPoint.x;
float dy = endPoint.y - startPoint.y;

float scale = scaleBy * Q_rsqrt(dx * dx + dy * dy);
return CGPointMake (endPoint.x - dx * scale, endPoint.y - dy * scale);
}

这两种解决方案都有效。如果我将线画到圆的中心,您会看到它与圆相交的位置正好在应该的位置。

http://www.freeimagehosting.net/le5pi

如果我使用上述任一解决方案并根据角度绘制圆的圆周,则它不再朝向圆心。在第二张图片中,这条线应该在圆的右边缘的中间并向右直行。

http://www.freeimagehosting.net/53ovs

http://www.freeimagehosting.net/sb3b2

抱歉链接。我是新手,目前无法发布图片。

感谢您的帮助。

【问题讨论】:

  • 我觉得你的sin和cos可能会颠倒过来。
  • 没关系。只需要清理并重新编译

标签: iphone geometry line-intersection


【解决方案1】:

将其视为向量问题更容易。您的第二种方法很接近,但您没有正确缩放两点之间的矢量。在这种情况下,使用归一化向量会更容易,尽管您必须假设线上两点之间的距离不为零。

给定:

double x0 = CIRC_X0; /* x-coord of center of circle */
double y0 = CIRC_Y0; /* y-coord of center of circle */

double x1 = LINE_X1; /* x-coord of other point on the line */
double y1 = LINE_Y1; /* y-coord of other point on the line */

那么两点之间的向量就是(vx,vy):

double vx = x1 - x0;
double vy = y1 - y0;

使用单位向量更容易,我们可以通过归一化 (vx,vy) 得到:

double vmag = sqrt(vx*vx + vy*vy);
vx /= vmag;  /* Assumption is vmag > 0 */
vy /= vmag;

现在,这条线上的任何一点都可以描述为:

x0 + dist * vx
y0 + dist * vy

其中dist 是到中心的距离。圆和直线的交点距离圆心一定是CIRC_RADIUS,所以:

double x_intersect = x0 + CIRC_RADIUS * vx;
double y_intersect = y0 + CIRC_RADIUS * vy;

【讨论】:

    【解决方案2】:

    我认为关于 theta、x 和 y 的含义可能存在约定冲突。 atan2 函数通过将 theta 的约定作为从 X 轴向 Y 增长的角度来生成 -pi..pi 范围内的值。但是,您将 theta 视为从 Y 到 X 的角度。 尝试更改代码:

    CGFloat theta = atan2(y, x);
    CGPoint newPoint;
    newPoint.x = circle.x + circleRadius * cos(theta);
    newPoint.y = circle.y + circleRadius * sin(theta);
    

    虽然您的公式在坐标系中是一致的,但它可能与屏幕/显示设备坐标系发生冲突。

    【讨论】:

      猜你喜欢
      • 2021-02-20
      • 2018-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-02
      • 2012-10-14
      相关资源
      最近更新 更多