Alec Lee

Ranch Hand

Posts: 569

posted 7 years ago

Given a line formed by 2 points (x1,y1) (x2,y2) (in a 2D plane). How do I find the point on the line that is distance d from, say (x1,y1) and lies in between the 2 points?

This should be a simple math problem but the geometry class I've taken was > 10 years ago - and everything was forgotten.

btw, I am trying to find the coordinate I need to move to in order to get closer my target monster.

This should be a simple math problem but the geometry class I've taken was > 10 years ago - and everything was forgotten.

btw, I am trying to find the coordinate I need to move to in order to get closer my target monster.

posted 7 years ago

I don't think that's a simple math question....

I think you need to do several things. you need to use the Pythagorean theorem to find the set of points X units from your original point (you'll get a set of points that form a circle).

You need the equation for the line, which you can get from the two points.

you need to solve that system of equations, which will give you two points.

you need to determine which of the two you want, probably by looking a which of the two is closer to the second point.

I think you need to do several things. you need to use the Pythagorean theorem to find the set of points X units from your original point (you'll get a set of points that form a circle).

You need the equation for the line, which you can get from the two points.

you need to solve that system of equations, which will give you two points.

you need to determine which of the two you want, probably by looking a which of the two is closer to the second point.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

Alec Lee

Ranch Hand

Posts: 569

posted 7 years ago

Thanks. I am working hard to recall my math knowledge. I show what I've done and see if it is correct or not:

Assuming the point is (xp,yp), then

From 2 point form I get the equation of the line as: (yp-y1)^2 * (x1-x2)^2 = (xp-x1)^2 * (y1-y2)^2 ......(1)

(I took square of the equation for easy substitiion with my 2nd equation)

From distance between 2 point formula: d^2 = (yp-y1)^2 + (xp - x1)^2 .......(2)

And, now it is 2 equations with 2 unknown. It could be solved by eliminiation. Since, I got (yp-y1)^2 in both equations, I can take the one in (2) and substitute it into (1) to get xp. And with xp known I just find yp from (1).

Is that correct?

Assuming the point is (xp,yp), then

From 2 point form I get the equation of the line as: (yp-y1)^2 * (x1-x2)^2 = (xp-x1)^2 * (y1-y2)^2 ......(1)

(I took square of the equation for easy substitiion with my 2nd equation)

From distance between 2 point formula: d^2 = (yp-y1)^2 + (xp - x1)^2 .......(2)

And, now it is 2 equations with 2 unknown. It could be solved by eliminiation. Since, I got (yp-y1)^2 in both equations, I can take the one in (2) and substitute it into (1) to get xp. And with xp known I just find yp from (1).

Is that correct?

posted 7 years ago
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

I think so...i don't have time to really dig through the math...

assuming your two points are (0,2) and (8,2)..

you'll find two solutions...assuming your two points are (0,2) and (8,2), and you want a point 2 units away from (0,2) you'll find one at (2,2) and (-2,2). the 'correct' one would be (2,2) since it's closer to (8,2).

assuming your two points are (0,2) and (8,2)..

you'll find two solutions...assuming your two points are (0,2) and (8,2), and you want a point 2 units away from (0,2) you'll find one at (2,2) and (-2,2). the 'correct' one would be (2,2) since it's closer to (8,2).

Fred Hamilton

Ranch Hand

Posts: 684

posted 7 years ago

I am not so sure about equation 1, it doesn't seem right, or else it is too complicated. equation (2) is correct.

may I suggest a slightly different way of doing.

ok, you have two unknowns x & y, and 4 "knowns" x1, y1, x2, y2

Since you have two unknowns you need two equations.

now the slope of the line is given by slope = (y2-y1)/(x2-x1). This is a known number, not a variable,.

since the point (x,y) is on the same line, it must fit into that formula, so the first equation is

A:

now we get the second equation from pythagorus theorem

B:

since you know what d is you now have two equations in two unknowns (x & y), and you should be able to sole this by substitution.

hopefuly that is a little clearer. my math is a little rusy too, but I'm pretty sure this is right.

Alec Lee wrote:Thanks. I am working hard to recall my math knowledge. I show what I've done and see if it is correct or not:

Assuming the point is (xp,yp), then

From 2 point form I get the equation of the line as: (yp-y1)^2 * (x1-x2)^2 = (xp-x1)^2 * (y1-y2)^2 ......(1)

(I took square of the equation for easy substitiion with my 2nd equation)

From distance between 2 point formula: d^2 = (yp-y1)^2 + (xp - x1)^2 .......(2)

And, now it is 2 equations with 2 unknown. It could be solved by eliminiation. Since, I got (yp-y1)^2 in both equations, I can take the one in (2) and substitute it into (1) to get xp. And with xp known I just find yp from (1).

Is that correct?

I am not so sure about equation 1, it doesn't seem right, or else it is too complicated. equation (2) is correct.

may I suggest a slightly different way of doing.

ok, you have two unknowns x & y, and 4 "knowns" x1, y1, x2, y2

Since you have two unknowns you need two equations.

now the slope of the line is given by slope = (y2-y1)/(x2-x1). This is a known number, not a variable,.

since the point (x,y) is on the same line, it must fit into that formula, so the first equation is

A:

**(y-y1)/(x-x1) = slope**.

now we get the second equation from pythagorus theorem

B:

**d^2 = (y-y1)^2 + (x-x1)^2**

since you know what d is you now have two equations in two unknowns (x & y), and you should be able to sole this by substitution.

hopefuly that is a little clearer. my math is a little rusy too, but I'm pretty sure this is right.

Stole Perov

Greenhorn

Posts: 12

posted 7 years ago

Hello there. I was just about to post the exact same post, luckily i checked this one and...

So, i think that the creator of this post can answer my question:

I have two points with known cordinates A(x1,y1) and B(x2,y2).

So, my question is:

How to find a point C(x3,y3) which lies between A and B and is at location 9/10 of the length of AB.

Since my geometry classes were also long ago, would you mind keeping it simple and clear.

Thank you for the time.

So, i think that the creator of this post can answer my question:

I have two points with known cordinates A(x1,y1) and B(x2,y2).

So, my question is:

How to find a point C(x3,y3) which lies between A and B and is at location 9/10 of the length of AB.

Since my geometry classes were also long ago, would you mind keeping it simple and clear.

Thank you for the time.

Alec Lee

Ranch Hand

Posts: 569

Fred Hamilton

Ranch Hand

Posts: 684

posted 7 years ago

Yeah, except that method tells us the distance given two points, We know the distance, we don't know the 2nd point. So I don't see how we can use your method, but if you do, then I'm all ears. regards.

Matt Cartwright wrote:just wondering ..

java.awt.geom.Point2D distanceSq(Point2D point)

Distance Formula

M

Yeah, except that method tells us the distance given two points, We know the distance, we don't know the 2nd point. So I don't see how we can use your method, but if you do, then I'm all ears. regards.

posted 7 years ago

It's simple if you count basic trigonometry as being simple.

Let theta be the angle between the line and the x-axis; then theta is arctan((x2 - x1) / (y2 - y1)).

Then the point you are looking for is (x1 + d * cos(theta), y1 + d * sin(theta)).

You'll have to watch out for the case where y1 = y2, in which case theta is zero.

Let theta be the angle between the line and the x-axis; then theta is arctan((x2 - x1) / (y2 - y1)).

Then the point you are looking for is (x1 + d * cos(theta), y1 + d * sin(theta)).

You'll have to watch out for the case where y1 = y2, in which case theta is zero.

It is sorta covered in the JavaRanch Style Guide. |