This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.

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.

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

Joined: Jan 28, 2004
Posts: 569

posted

0

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

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

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.

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.

Alec Lee
Ranch Hand

Joined: Jan 28, 2004
Posts: 569

posted

0

That one is simple......after solving my own question.

There is actually a formula for finding a point cutting the line in a fixed ratio - exactly the thing you need. I need to check out the geometry textbook in the library. I'll post that formula here tomorrow.

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.