如何使用另外两个点及其角度找到第三个点

发布于 2024-12-09 18:54:42 字数 196 浏览 0 评论 0原文

我在这里找到了答案,但无法理解如何将数学转移到 Objective C

找到第三点< /a>

我有两个点,还有相对于轴的角度。如何找到可以形成直线的第三点?距离应该是可变的。

I found an answer here, but can't understand how to transfer the math to Objective C

Find the third point

I have two points and I also have the angle relative to the axes. How do I find a third point which will form a straight line? The distance should be variable.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏末的微笑 2024-12-16 18:54:42

这是我正在使用的代码:

float distanceFromPx2toP3 = 1300.0;    

float mag = sqrt(pow((px2.x - px1.x),2) + pow((px2.y - px1.y),2));
float P3x = px2.x + distanceFromPx2toP3 * (px2.x - px1.x) / mag;
float P3y = px2.y + distanceFromPx2toP3 * (px2.y - px1.y) / mag;

CGPoint  P3 = CGPointMake(P3x, P3y);

This is the code that I am using:

float distanceFromPx2toP3 = 1300.0;    

float mag = sqrt(pow((px2.x - px1.x),2) + pow((px2.y - px1.y),2));
float P3x = px2.x + distanceFromPx2toP3 * (px2.x - px1.x) / mag;
float P3y = px2.y + distanceFromPx2toP3 * (px2.y - px1.y) / mag;

CGPoint  P3 = CGPointMake(P3x, P3y);
在风中等你 2024-12-16 18:54:42

假设我有两个点 pointA 和 pointB。由两点 m 形成的直线的斜率是:

static CGFloat calculateSlope(CGPoint pointA, CGPoint pointB) {
  CGFloat m = (pointB.y - pointA.y) / (pointB.x - pointA.x);
  return m;
}

第三点 pointC 与直线上的点 A 的距离为 d,由下式给出:

static CGPoint calculatePointOnLine(
  CGPoint pointA, CGPoint pointB, CGFloat d, BOOL startAtB) {

  CGFloat m = calculateSlope(pointA, pointB);

  CGFloat dX = pointB.x - pointA.x;
  CGFloat dY = pointB.y - pointA.y;

  CGFloat signDX = dX / fabsf(dX);
  CGFloat signDY = dY / fabsf(dY);

  CGFloat dSquared = d * d;
  CGFloat mSquared = m * m;

  // We know pointC is distance d from pointA,
  // and that pointA and pointC are on the
  // same line
  // dXSquared + dYSquared = dSquared
  // m = dY / dX
  // dY = m * dX
  // dXSquared + mSquared * dXSquared = dSquared
  // dXSquared * ( 1 + mSquared ) = dSquared
  // dXSquared = dSquared / ( 1 + mSquared )

  // Handle a vertical line, dX == 0, and a horizontal line, dY == 0
  if (dX != 0 && dY != 0) {
    // Account for the sign of dX
    dX = signDX * sqrtf(dSquared / ( 1 + mSquared ));

    // Account for the sign of dY
    dY = signDY * m * dX;
  }

  // Handle a vertical line, dX == 0
  if (dX == 0 && dY != 0) {
    dY = signDY * d;
  }

  // Handle a horizontal line, dY == 0
  if (dY == 0 && dX != 0) {
    dX = signDX * d;
  }

  CGPoint startingPoint = pointA;
  if (startAtB) {
    startingPoint = pointB;
  }

  CGPoint pointC = CGMakePoint(startingPoint.x + dX, 
                               startingPoint.y + dY);
  return pointC;
}

pointC 现在始终位于沿直线与点 A 的距离 d 处,
从A点到B点的方向。通过 startAtB 获得 pointC
沿距 B 点的直线距离 d,方向为
A点到B点。

在对 calculatPointOnLine 的调用中交换 piintA 和 pointB 的顺序
计算沿线距离 d 的点 C
PointB,从B点到A点的方向。

您可以使用这两个函数来计算直线上的第三个点。
如果这对您有帮助,感谢您接受这个答案。

Let's say I have two points pointA and pointB. The slope of the line formed by the two points m is:

static CGFloat calculateSlope(CGPoint pointA, CGPoint pointB) {
  CGFloat m = (pointB.y - pointA.y) / (pointB.x - pointA.x);
  return m;
}

A third point pointC a distance d from pointA on the line would be given by:

static CGPoint calculatePointOnLine(
  CGPoint pointA, CGPoint pointB, CGFloat d, BOOL startAtB) {

  CGFloat m = calculateSlope(pointA, pointB);

  CGFloat dX = pointB.x - pointA.x;
  CGFloat dY = pointB.y - pointA.y;

  CGFloat signDX = dX / fabsf(dX);
  CGFloat signDY = dY / fabsf(dY);

  CGFloat dSquared = d * d;
  CGFloat mSquared = m * m;

  // We know pointC is distance d from pointA,
  // and that pointA and pointC are on the
  // same line
  // dXSquared + dYSquared = dSquared
  // m = dY / dX
  // dY = m * dX
  // dXSquared + mSquared * dXSquared = dSquared
  // dXSquared * ( 1 + mSquared ) = dSquared
  // dXSquared = dSquared / ( 1 + mSquared )

  // Handle a vertical line, dX == 0, and a horizontal line, dY == 0
  if (dX != 0 && dY != 0) {
    // Account for the sign of dX
    dX = signDX * sqrtf(dSquared / ( 1 + mSquared ));

    // Account for the sign of dY
    dY = signDY * m * dX;
  }

  // Handle a vertical line, dX == 0
  if (dX == 0 && dY != 0) {
    dY = signDY * d;
  }

  // Handle a horizontal line, dY == 0
  if (dY == 0 && dX != 0) {
    dX = signDX * d;
  }

  CGPoint startingPoint = pointA;
  if (startAtB) {
    startingPoint = pointB;
  }

  CGPoint pointC = CGMakePoint(startingPoint.x + dX, 
                               startingPoint.y + dY);
  return pointC;
}

pointC will now always lie a distance d along the line from pointA,
in the direction from pointA to pointB. Pass startAtB to have pointC
lie a distance d along the line from pointB, in the direction from
pointA to pointB.

Exchange the order of piintA and pointB in the call to calculatPointOnLine
to calculate a pointC which lies a distance d along the line from
PointB, in the direction from pointB to pointA.

You can use these two functions to calculate a third point on the line.
Thanks for accepting this answer if this helps you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文