## Code for moving an object diagonally?

All your problems with Audio, Video and Images.

### Code for moving an object diagonally?

I feel silly asking this question, but my google searches are coming up empty. Probably because I'm searching for the wrong thing.

I have two sets of x,y coordinates. I need to move my object along the diagonal path between the two sets of x,y's.

Where can I get a mathematical equation that will take two sets of x,y and will give you the next whole integer along the path.

For a simple example, if the two coordinates were 0, 0 and 10, 10 these would be the coodinates that I would move along:
1,1
2,2
3,3
etc.

Thanks!
pcm2a
Developer

Posts: 27
Joined: Tue Aug 11, 2009 4:54 pm

try this:

double slope = (y2-y1)/(x2-x1);

that gives u slope of the line from x1 to x2.

Then iterate through this until you reach your desired point:

Using java Syntax Highlighting
1.
2. for(int i=0; i< (x2-x1); i++){
3.
4. x = Math.round(x1+ slope * i);
5.
6. y = Math.round(y1+ slope * i);
7.
8. }
9.
10.
Parsed in 0.010 seconds, using GeSHi 1.0.8.4

I dunno if this will work, though. The rounding might throw it off.
yaj786
Developer

Posts: 33
Joined: Fri Jul 10, 2009 7:37 am