Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Simple Math Question

Ranch Hand
Posts: 569
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.

lowercase baba
Bartender
Posts: 12624
50
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.

Alec Lee
Ranch Hand
Posts: 569
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?

fred rosenberger
lowercase baba
Bartender
Posts: 12624
50
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).

Ranch Hand
Posts: 686

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.

Greenhorn
Posts: 12
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
Posts: 569
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.

Ranch Hand
Posts: 152
just wondering ..

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

Distance Formula

M

Fred Hamilton
Ranch Hand
Posts: 686

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.

Sheriff
Posts: 23506
47
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.

 You totally ruined the moon. You're gonna hafta pay for that you know. This tiny ad agrees: Rocket Oven Kickstarter - from the trailboss https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss