17

Looking for the quickest way to calculate a point that lies on a line a given distance away from the end point of the line:

void calculate_line_point(int x1, int y1, int x2, int y2, int distance, int *px, int *py) 
{
    //calculate a point on the line x1-y1 to x2-y2 that is distance from x2-y2
    *px = ???
    *py = ???
}  

Thanks for the responses, no this is not homework, just some hacking out of my normal area of expertise.

This is the function suggested below. It's not close to working. If I calculate points every 5 degrees on the upper right 90 degree portion of a circle as starting points and call the function below with the center of the circle as x2,y2 with a distance of 4 the end points are totally wrong. They lie below and to the right of the center and the length is as long as the center point. Anyone have any suggestions?

void calculate_line_point(int x1, int y1, int x2, int y2, int distance)
{

//calculate a point on the line x1-y1 to x2-y2 that is distance from x2-y2

  double vx = x2 - x1; // x vector
  double vy = y2 - y1; // y vector

  double mag = sqrt(vx*vx + vy*vy); // length

  vx /= mag;
  vy /= mag;

// calculate the new vector, which is x2y2 + vxvy * (mag + distance).

  px = (int) ( (double) x2 + vx * (mag + (double)distance) );
  py = (int) ( (double) y2 + vy * (mag + (double)distance) );

}

I've found this solution on stackoverflow but don't understand it completely, can anyone clarify?

Community
  • 1
  • 1
amanda
  • 601
  • 2
  • 8
  • 14
  • 4
    Maybe you should use floats/doubles, because you will get rounding errors. This might be a concern. – Lucas Nov 25 '09 at 21:41
  • 1
    What Lucas said. Also, you probably read my post while I had a typo. If x1y1 is the origin, you want x1y1 + vxvy * (mag + distance), not x2y2. That is, starting from the origin, you want to travel the distance _to x2y2_ plus the additional distance, using the direction from x1y1 to x2y2. Though I think you might want to rephrase your question. What exactly do you want to do? The question as it is now seems more like an intermediate problem. –  Dec 05 '09 at 02:19
  • [Geometry: Finding a point along a line a certain distance away from another point!](http://math.stackexchange.com/questions/175896/finding-a-point-along-a-line-a-certain-distance-away-from-another-point/1630886#1630886) – Sen Jacob Jan 28 '16 at 18:38

2 Answers2

36

I think this belongs on MathOverflow, but I'll answer since this is your first post. First you calculate the vector from x1y1 to x2y2:

float vx = x2 - x1;
float vy = y2 - y1;

Then calculate the length:

float mag = sqrt(vx*vx + vy*vy);

Normalize the vector to unit length:

vx /= mag;
vy /= mag;

Finally calculate the new vector, which is x2y2 + vxvy * (mag + distance).

*px = (int)((float)x1 + vx * (mag + distance));
*py = (int)((float)y1 + vy * (mag + distance));

You can omit some of the calculations multiplying with distance / mag instead.

  • 17
    unfortunately, mathoverflow is too snobbish to entertain this kind of question; it does belong here. – Victor Liu Nov 25 '09 at 21:51
  • 1
    Sorry if it's too simple guys, totally out of my normal area of competence. Thanks for the help. – amanda Dec 01 '09 at 19:58
  • 5
    Good work. I was a professor once upon a time, and the first, biggest lesson I learned was that what I thought was simple was not simple to others. I like SO as a means of helping each other along. – Mike Dunlavey Dec 01 '09 at 22:24
  • 1
    Whatever the merits of MathOverflow are, the task of writing the correct **code** would be actually outside the core competences of people at MathOverflow. I'm more active on MathOverflow now (I have about 3k rep there), but I'll continue to answer math questions on SO as well. The sites complement each other well, in my opinion. – ilya n. Dec 10 '09 at 19:00
  • 1
    +1 @Mads Elvheim - thanks. You saved my day! :D it is a shame that the guy did not accept your answer! – Duck Apr 13 '11 at 03:42
  • 4
    I think this could be much simpler: *px = (int)((float)x2 + vx * distance)); *py = (int)((float)y2 + vy * distance)); – Nicolas Mommaerts Jul 16 '12 at 17:18
2

These equations are wrong:

px = (int) ( (double) x2 + vx * (mag + (double)distance) );

py = (int) ( (double) y2 + vy * (mag + (double)distance) );

The correct equations are:

px = (int) ( (double) x2 + vx * (double)distance );

py = (int) ( (double) y2 + vy * (double)distance );

Tom

Tom Orsi
  • 121
  • 3