Math Is Fun Forum

  Discussion about math, puzzles, games and fun.   Useful symbols: ÷ × ½ √ ∞ ≠ ≤ ≥ ≈ ⇒ ± ∈ Δ θ ∴ ∑ ∫ • π ƒ -¹ ² ³ °

You are not logged in.

#1 2010-11-06 06:39:33

Apothem
Member
Registered: 2010-11-06
Posts: 17

fun with two points on a graph

I'm trying to calculate coordenates between two points on a graph on the XY plane in a programming project I'm working on. Basically when object B is a certain distance from object A, object A will follow a line that is directly related to the angle (or slope) of said line until it meets the maximum distance again. I already have the code figured out, I just cant remember how to calculate out the coordenates. I think I'm supposed to be using slope for this, but I dont know how to apply it over a distance, nor do I know how to actually properly get the slope between two points using only numbers with no graph.

The main goal here is to find the formula for calculating points on a predetermined line between two sets of coordenates.

Forgive me if I'm not being descriptive enough, I've never really tried asking on a forum for help on this topic before.

Offline

#2 2010-11-06 06:57:05

bobbym
bumpkin
From: Bumpkinland
Registered: 2009-04-12
Posts: 109,606

Re: fun with two points on a graph

Hi Apothem;

Welcome to the forum. You are not giving me quite enough to help out. The slope between 2 coordinates (x1,y1) and (x2,y2) is given by the following formula.


In mathematics, you don't understand things. You just get used to them.
If it ain't broke, fix it until it is.
Always satisfy the Prime Directive of getting the right answer above all else.

Offline

#3 2010-11-06 08:56:40

Apothem
Member
Registered: 2010-11-06
Posts: 17

Re: fun with two points on a graph

okay let's try this again. I think I've found a better way to describe what I'm trying to do.

I've got the coordinates of two points on a graph, and I want to find a point between the two. How do I find a point in the line segment?

Offline

#4 2010-11-06 09:48:35

bobbym
bumpkin
From: Bumpkinland
Registered: 2009-04-12
Posts: 109,606

Re: fun with two points on a graph

Hi;

If you have 2 coordinates than you know the x and y values of both. That is what a coordinate is. All you have to do to find an infinite amount of points that are on that line between those two points. Use this formula:

Where the coordinates are (x1,y1) and (x2,y2). Now you have the equation of the line that passes through the 2 coordinates.

To get a point in between just plug into the equation for some value of x between x1 and x2.

Do you have an example, then it will be much clearer?


In mathematics, you don't understand things. You just get used to them.
If it ain't broke, fix it until it is.
Always satisfy the Prime Directive of getting the right answer above all else.

Offline

#5 2010-11-06 09:59:22

Bob
Administrator
Registered: 2010-06-20
Posts: 10,583

Re: fun with two points on a graph

hi Apothem,

Bobbym has already said how to calculate the slope.  [and as I post this, I see he has added another post but I'll leave mine so you have a choice of methods ...basically the same as  we are both using straight line graph formulas.]

Do that and then you can get an equation for all points on the line.

It's usual to write this as

'm' is the slope that you can calculate using bobbym's formula.

Now to find 'c'.

If (a,b) is one of your known points then c = b -ma will give you 'c'.

On any line (that is not parallel to either axis) you can always choose an 'x' that is on the line.

Find the corresponding 'y' by calculating y = mx + c using this 'x' and the calculated values of m, and c.

I hope that makes sense.  If you are having trouble post your coordinates and I'll work the numbers to make it easier.

Bob

Last edited by Bob (2010-11-06 10:02:03)


Children are not defined by school ...........The Fonz
You cannot teach a man anything;  you can only help him find it within himself..........Galileo Galilei
Sometimes I deliberately make mistakes, just to test you!  …………….Bob smile

Offline

#6 2010-11-06 10:08:21

Apothem
Member
Registered: 2010-11-06
Posts: 17

Re: fun with two points on a graph

Well it's more programming related, but the code side of it is figured out. Basically I have a center point which is the focal point for the camera, and then a cursor that you manipulate with the mouse. Whenever the cursor goes outside a certain range, it's supposed to drag the center point along with it in the direction of the cursor.

I figured I could use a slope formula for this and just translate the focal point towards the cursor. That's what I meant by always having the x and y coordinates of both points. It's just that one point is outside the range I want it and I'm more pulling one point closer to the other.

I hope that clears things up, I'll keep trying to figure out a way to explain this until I get it right.

Offline

#7 2010-11-06 10:09:47

Bob
Administrator
Registered: 2010-06-20
Posts: 10,583

Re: fun with two points on a graph

Maybe a diagram would help

Bob


Children are not defined by school ...........The Fonz
You cannot teach a man anything;  you can only help him find it within himself..........Galileo Galilei
Sometimes I deliberately make mistakes, just to test you!  …………….Bob smile

Offline

#8 2010-11-06 10:16:13

bobbym
bumpkin
From: Bumpkinland
Registered: 2009-04-12
Posts: 109,606

Re: fun with two points on a graph

bob bundys wrote:

Maybe a diagram would help

Sure would because this could be a lot more complicated than it seems.

I would not handle the motion in the same manner. But if you want to do it your way then figure the line between the two points with the point point formula and choose an a new x coordinate for the center point.


In mathematics, you don't understand things. You just get used to them.
If it ain't broke, fix it until it is.
Always satisfy the Prime Directive of getting the right answer above all else.

Offline

#9 2010-11-07 10:53:24

Apothem
Member
Registered: 2010-11-06
Posts: 17

Re: fun with two points on a graph

okay, I made a diagram. I hope my mad MSPAINT skillz will be able to clarify. If not, or the diagram makes no sense, I'll just keep trying until either you guys get it or the thread hits 2 pages. tongue

diagram1.PNG

Okay, so let's give this another go. Basically the goal is to keep the above line segment from reaching anything above a maximum size of 512. So what I'm portraying in this diagram is basically when the line is too big. The distance between line segment BC is the distance A has to travel towards C on the already determined line ABC. The coordinates of A and C are known and the distance of line segment AC is known.

What I'm trying to do in game is the equivalent the cursor in a strategy game such as Starcraft. Point A is basically the coordenates of what your perspective is in the game, and point C is the cursor. As the cursor (point C) moves around it will eventually reach an outer edge. You can detect the distance of that via a center focal point being Point A basically.

As I've been writing this I believe I'm basically trying to understand how to apply slope to the coordinates of a single object. Since I'm basically programming it in, I need to be able to apply it to the x and y separately.

I hope that clears things up a bit, I'll keep trying 'till I get it.

Last edited by Apothem (2010-11-07 10:55:23)

Offline

#10 2010-11-07 13:39:22

bobbym
bumpkin
From: Bumpkinland
Registered: 2009-04-12
Posts: 109,606

Re: fun with two points on a graph

Hi Apothem;

From what I can understand, you know A and C. You want to to know where B is so you can reposition A to B. Then AC will be less than or equal to 512.

Check the diagram below. It is an example of how you do it mathematically.

1) Get the equation of the line between A and C the two points you know. In the example A = (0,0) and C = (481,600) The distance of AC is 729. I made it a little bigger so all the coordinates could be integers. In the following equations x1 = 0 , y1 = 0 and x2 = 481, y2 = 600.

To get the equation you use the point point formula.

This is the equation of the line that connects A and C. It will be different each time.

Now you want a new point that is on the line 2) and has a distance of 512 from C. You use the distance formula.

Plugging into 3) elements of 2) you get to solve this quadratic for x:

You get x = 160.7503, to get y you plug x into 2), y = 200.5201.

So the coordinate of B ( where you are going to move A ) is
(160.7503, 200.5201). This point is approximately 512 away from C and on the line AC. I only used 512 for an example. To get whatever distance you want from C just put something other than 512 on the RHS of 4).

Of course you are going to have to program all this math into code.

There is a super easy way to do this though! No heavy math at all. Perfect for a computer. Can you see what it is?


In mathematics, you don't understand things. You just get used to them.
If it ain't broke, fix it until it is.
Always satisfy the Prime Directive of getting the right answer above all else.

Offline

#11 2010-11-08 03:45:27

Apothem
Member
Registered: 2010-11-06
Posts: 17

Re: fun with two points on a graph

Oh noes, I think the last time I touched quadratics musta been about 6 years ago. I'm crazy rusty on how to solve those, are there any reference materials I can teach myself that list again?

Last edited by Apothem (2010-11-08 03:45:40)

Offline

#12 2010-11-08 04:15:01

bobbym
bumpkin
From: Bumpkinland
Registered: 2009-04-12
Posts: 109,606

Re: fun with two points on a graph

Hi Apothem;

You can learn about quadratics from right here:

http://www.mathsisfun.com/algebra/facto … atics.html

But you will not need it for this problem. There is a much easier way.


In mathematics, you don't understand things. You just get used to them.
If it ain't broke, fix it until it is.
Always satisfy the Prime Directive of getting the right answer above all else.

Offline

#13 2010-11-08 12:16:12

Apothem
Member
Registered: 2010-11-06
Posts: 17

Re: fun with two points on a graph

bobbym wrote:

But you will not need it for this problem. There is a much easier way.

I fail to see what you mean. Could you enlighten me? wink

I figured I could just apply the slope of the line to the coordenates based off of the difference between the current distance and the desired distance, but clearly it was never that simple. Unless that IS it and I just REALLY dont know what I'm doing.

I am incredibly rusty when it comes to this type of math, so I may need to reteach myself a good bit.

Offline

#14 2010-11-08 15:27:35

bobbym
bumpkin
From: Bumpkinland
Registered: 2009-04-12
Posts: 109,606

Re: fun with two points on a graph

Hi Apothem;

Use this algorithm, I will use a real example to illustrate. Supposing you have A at (50,-100) and C at (400,450), Now you say you always know the length of AC.
In this case AC = 651.9202. Call this d = 651.9202.
Label x1 = 50 , y1 = - 100, x2 = 400, y2 = 450

You want to move A so that it is 512 away from C along the line segment AC.

1) Take the coordinates of A and go

You just took the negative values of the x and y coordinates.

2) Translate the line so that A is at the origin. This is done by adding t1 and t2 to the x and y of both A and C.

This translation of the points is always possible
and we say that the essential properties of the line AC are invariant under this translation. It essentially moves AC until A = (0,0). This makes the computation easier and does not alter the length of AC.


3) Now say

4)

5) Translate (x2,y2) back to AC

(x2,y2) is the new point you want to move A to. It will be 512 away from C and on AC.

If you run through my instructions with the numbers given you will get:

x2 = 125.1197 and y2 = 18.045.

As shown in the diagram. This point is 512 away from C.

This algorithm requires only 1 division 2 multiplications and some adds and subtracts. It should be very fast. If you want B to be some other distance from C than 512 then just change 3).


In mathematics, you don't understand things. You just get used to them.
If it ain't broke, fix it until it is.
Always satisfy the Prime Directive of getting the right answer above all else.

Offline

#15 2010-11-08 18:20:34

Apothem
Member
Registered: 2010-11-06
Posts: 17

Re: fun with two points on a graph

in part 3) you have there, what is D? Total distance?

Offline

#16 2010-11-08 18:37:21

bobbym
bumpkin
From: Bumpkinland
Registered: 2009-04-12
Posts: 109,606

Re: fun with two points on a graph

Hi Apothem;

bobbym wrote:

In this case AC = 651.9202. Call this d = 651.9202.

Yes, d is the length of the line segment AC.


In mathematics, you don't understand things. You just get used to them.
If it ain't broke, fix it until it is.
Always satisfy the Prime Directive of getting the right answer above all else.

Offline

#17 2010-11-09 04:14:14

Apothem
Member
Registered: 2010-11-06
Posts: 17

Re: fun with two points on a graph

Okay well it sorta works. Right now I'm having issues with C. Basically it keeps coming up as 0. If I just make step 3 c = d-512, it works. Doing (d-512)/d doesnt work for some reason, I'll post the code here because it's literally what you posted above.

When I take out the /d the object moves in the correct direction, and it does do what it's supposed to, but just really slowly. If point C is far enough away, it starts to quickly travel, but only when the total distance is above like 1024 or so.

Oh and thank you again for your patience and your knowledge, it has been EXTREMELY helpful.


EDIT: NEVERMIND I GOT IT. Turns out I had to used some fixed point division as well as multiplication. The language I'm working with makes a distinction between fixed point numbers and integers. The coordinates are fixed point numbers so when you're working with distance which is returned as an integer, it throws the whole thing off.

Thank you very very much for your help!!!! big_smile

Last edited by Apothem (2010-11-09 04:35:17)

Offline

#18 2010-11-09 04:46:51

bobbym
bumpkin
From: Bumpkinland
Registered: 2009-04-12
Posts: 109,606

Re: fun with two points on a graph

Hi Apothem;

In C++ when you would do step 3 and had defined d as an integer then it will always equal 0. That is because c will always be less then 1. The numerator will always be smaller then the denominator.


In mathematics, you don't understand things. You just get used to them.
If it ain't broke, fix it until it is.
Always satisfy the Prime Directive of getting the right answer above all else.

Offline

#19 2010-11-09 05:43:23

John E. Franklin
Member
Registered: 2005-08-29
Posts: 3,588

Re: fun with two points on a graph

given 2 points.
Choose a ratio of how far you want to go from A to B, call it R for ratio.
R = 1/3 for example
Now yB - yA is distance in y, so multiply the distance by R or 1/3 and add it to yA to move toward yB.
Do the same for the x's...


igloo myrtilles fourmis

Offline

Board footer

Powered by FluxBB