How to find a third point using two other points and their angle

≡放荡痞女 提交于 2019-12-11 06:25:46

问题


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.


回答1:


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);



回答2:


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.



来源:https://stackoverflow.com/questions/7780374/how-to-find-a-third-point-using-two-other-points-and-their-angle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!