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?

3
  • 4
    Maybe you should use floats/doubles, because you will get rounding errors. This might be a concern. Commented Nov 25, 2009 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. Commented Dec 5, 2009 at 2:19
  • Geometry: Finding a point along a line a certain distance away from another point! Commented Jan 28, 2016 at 18:38

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

unfortunately, mathoverflow is too snobbish to entertain this kind of question; it does belong here.
Sorry if it's too simple guys, totally out of my normal area of competence. Thanks for the help.
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.
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.
I think this could be much simpler: *px = (int)((float)x2 + vx * distance)); *py = (int)((float)y2 + vy * distance));
|
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.