Coordinates problem
-
I have two coordinates on the screen x1 and y1 and i have an object at that location.. now i have x2 and y2 which will be the destination point of my object.. I am planning to move my object 1 pixel at a time following the path from x1, y1 to x2, y2.. is there a formula that you can recommend?
-
I have two coordinates on the screen x1 and y1 and i have an object at that location.. now i have x2 and y2 which will be the destination point of my object.. I am planning to move my object 1 pixel at a time following the path from x1, y1 to x2, y2.. is there a formula that you can recommend?
Just work out the ratio of x2 - x1 to y2 - y1, and that will tell you the ratio of pixels to move in x versus y. If you want reasonably smooth movement you'll need to track positions in floats. If you want very smooth movement you'll need to take the time per update frame into account.
There are three kinds of people in the world - those who can count and those who can't...
-
Just work out the ratio of x2 - x1 to y2 - y1, and that will tell you the ratio of pixels to move in x versus y. If you want reasonably smooth movement you'll need to track positions in floats. If you want very smooth movement you'll need to take the time per update frame into account.
There are three kinds of people in the world - those who can count and those who can't...
-
Try something like :-
float ratio = (float)(x2 - x1) / (float)(y2 - y1);
The casts make sure you don't truncate the result.
There are three kinds of people in the world - those who can count and those who can't...
-
Try something like :-
float ratio = (float)(x2 - x1) / (float)(y2 - y1);
The casts make sure you don't truncate the result.
There are three kinds of people in the world - those who can count and those who can't...
well actually from what i understand the locations in the C# controls such as a form and a panel are by pixels and of the INT data type. I'm trying to move it pixel by pixel.. I do remember I had a formula taught to me by my math teacher that given the coordinates of both ends of a straight line, you can solve all the POINTS in between.
-
well actually from what i understand the locations in the C# controls such as a form and a panel are by pixels and of the INT data type. I'm trying to move it pixel by pixel.. I do remember I had a formula taught to me by my math teacher that given the coordinates of both ends of a straight line, you can solve all the POINTS in between.
-
well actually from what i understand the locations in the C# controls such as a form and a panel are by pixels and of the INT data type. I'm trying to move it pixel by pixel.. I do remember I had a formula taught to me by my math teacher that given the coordinates of both ends of a straight line, you can solve all the POINTS in between.
Silvyster wrote:
from what i understand the locations in the C# controls such as a form and a panel are by pixelsand of the INT data type.
The problem you'll have is that unless you're moving your object exactly along the X or Y direction, or at 45 degrees, you won't have an integer number of pixels in each direction. What you need to do is something like :-
float ratio = (float)(y2 - y1) / (float)(x2 - x1); // note : this is dY / dX, not as in previous post int step = (x2 > x1)? 1 : -1; float newFloatY = y1; int newX = x1; for (int loop=x1; loop!=x2; loop+=step) { newX++; newFloatY += ratio; int newY = (int)(newFloatY); // position object at newX, newY... }
You'll need to modify it if you want to move by 1 pixel in Y, and cope with special cases like moving exactly horizontally or vertically, but those are "left as an exercise for the reader"... :)
There are three kinds of people in the world - those who can count and those who can't...