views:

518

answers:

2

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?

+3  A: 

Is this homework? To err on the safe side:

  1. Create a vector describing the direction of the line.
  2. Normalize the vector
  3. Your desired point is start_point + distance * vector
Dathan
He wanted the point which is "distance" from the end point, not the start point.
Mads Elvheim
Or maybe she wanted the point which is "distance" from the end point, not the start point. And no it is not homework.
amanda
+7  A: 

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.

Mads Elvheim
unfortunately, mathoverflow is too snobbish to entertain this kind of question; it does belong here.
Victor Liu
That's unfortunate.
Mads Elvheim
Sorry if it's too simple guys, totally out of my normal area of competence. Thanks for the help.
amanda
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
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.