wood burning stoves 2.0*
The moose likes Game Development and the fly likes Simple Math Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Game Development
Bookmark "Simple Math Question" Watch "Simple Math Question" New topic
Author

Simple Math Question

Alec Lee
Ranch Hand

Joined: Jan 28, 2004
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.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11256
    
  16

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.


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

Joined: Oct 02, 2003
Posts: 11256
    
  16

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).
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
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

Joined: Feb 08, 2010
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

Joined: Jan 28, 2004
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.
Matt Cartwright
Ranch Hand

Joined: Aug 25, 2008
Posts: 149

just wondering ..

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

Distance Formula

M
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
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.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Simple Math Question