You are not logged in.

- Topics: Active | Unanswered

Pages: **1**

**jakenoble****Member**- Registered: 2006-04-11
- Posts: 3

Hi all,

First post here, so bear with me.

I am writing a program in Java, and I am having a problem regarding some of the Maths when displaying what I need to display.

I want to be able to move a shape from one coordinate to another, this involves calulating all the coordinate in between, so i can tell the shape which coordinates it needs to move through.

This is proving extremely difficult, I have maned to work out the polar (bearing) coordinates by using 'tan'. What I need is some sort of formula that will determine all the coordinates on a bearing.

E.g. starting at 0,0 with a bearing of 45 degrees, then it could be said that all the coordinates on that bearing are 1,1 2,2 3,3 4,4 . . . etc

If anyone can help, or point me in the right direction it would be a great help.

Thanks

Jake

Offline

**MathsIsFun****Administrator**- Registered: 2005-01-21
- Posts: 7,664

You want smooth motion then? ie you want each new coordinate to be exactly some small distance away from the previous?

At the beginning you mention that you start with coordinates. Well, that should be enough information.

Let's say you start at (2,3) and are heading for (5,10). The difference is (5-2,10-3) = (3,7). That is a total distance of √(3²+7²) = 7.6 (Pythagoras Theorem).

Now let us say that you want to move forward 0.1 every step, that would make 76 steps. So each step must be (3/76,7/76) = (0.0395,0.0921). Probably best done in a loop:

XDiff = XEnd-XStt;

YDiff = YEnd-YStt;

Dist = sqrt(XDiff^2 + XDiff^2);

Steps = Dist/0.1;

XInc=XDiff/steps;

YInc=YDiff/steps;

for (i=0; i<steps; i++) {

NewX = XStt + i*XInc;

NewY = YStt + i*YInc;

}

That is not guaranteed to work (or even to be any particular language), just something to get started with.

"The physicists defer only to mathematicians, and the mathematicians defer only to God ..." - Leon M. Lederman

Offline

**jakenoble****Member**- Registered: 2006-04-11
- Posts: 3

Hi,

Thanks for the response. That is exactly what I want, but I failed to mention one small point at the start, all the coordinates have to integers, that is to say whole numbers.

Your method does make perfect sense but when coupled with the above piece of info its not as accurate. Maybe I will just have to live with this in-accuracy, because if I was to move from 2,3 to 5,10 there is a very limited number of whole numbers in between those points. Therefore a smooth motion is nearly impossible over a short distance.

Offline

**MathsIsFun****Administrator**- Registered: 2005-01-21
- Posts: 7,664

I see. There are methods to do this.

You could choose an axis (X or Y) to be the "incrementing axis", based on the larger of the two differences (in the example above that would be the Y axis because it moved 7 units).

Then increment that axis by whole numbers (i=0 to 7), and at each step calculate the other coordinate (that is most easily done by multiplying by the ratio of differences, 3/7 in the example), and round that to a whole number.

Add those values to the start values and you have a series of new (whole number) coords.

That might work for you.

"The physicists defer only to mathematicians, and the mathematicians defer only to God ..." - Leon M. Lederman

Offline

**jakenoble****Member**- Registered: 2006-04-11
- Posts: 3

Hi again.

That sounds like something i had come up with previously. I found a problem with it though that would need some very detailed micro management code to get around the problem.

If moving from 2,3 to 5,10, then every time a move occurs a check is made to see if the shapes current position is 5,10, if its not then the shape can continue to move. What I found happened was that the shape a route similar to thi, depending of the increment values that were calulated 2,3 2,5, 3,7 3,9 4,11 5,11

Although that is a poor example you may be able to see what I mean, for each shape, moving will be moving in any direction each one may just miss the exact point at which is supposed to stop.

I have thought of another solution which is to make all the Coordinate huge, in the thousands or tens of thousands and work out all the Maths positions with that. Then scale it all down to the screen size. This would presever accuracy mathimatically, which is needed, but the display would be displaying the best it could?

I have another thread here on the same subject

http://forums.devshed.com/java-help-9/drawing-from-one-coordinate-to-another-335813.html#post1460092

*Last edited by jakenoble (2006-04-12 01:39:13)*

Offline

Pages: **1**