I'm trying to figure out a formula that will tell me the length of a line from the center of one square, to the center of another, BASED on where the line crosses one box and the other.
I made a diagram to explain it better, since I can't do it without a bunch of text.
http://i42.tinypic.com/27y254n.jpg
I need this formula for an RTS game I'm programming.
Thanks.
I made a diagram to explain it better, since I can't do it without a bunch of text.
http://i42.tinypic.com/27y254n.jpg
I need this formula for an RTS game I'm programming.
Thanks.
-
Once you have the coordinates of each point, you just apply the standard distance formula.
If the points are (r, s) and (u,v) distance = sqrt ( (r-u)^2 + (s - v)^2 )
Finding the coordinates is not too difficult, but there are lots of cases.
Using your example with the points (-5, 2) and (0, 0).
First determine the slope of the line: (0 - 2) / (0 - (-5) ) = -2/5
Let the end point of the red line closest to (-5,2) = (r, s)
and the end point closest to (0,0) = (u, v)
Then we have:
r = -5 + (x/2)
s = 2 + (-2/5) * y/2
u = 0 - x/2
v = 0 - (-2/5) * y / 2
The tricky part is figuring out what sign to use in the middle,
and when to use the slope (for x or y, and how).
There are quite a few different cases.
Call the points A and B.
In this case A = (-5, 2), B = (0, 0)
The slope can be positive or negative,
or zero, and more than one or less than one,
or vertical if the x-coordinates are the same.
So you must break it down by cases,
and in some cases you will apply the slope to the x-coordinate,
in some cases to the y-coordinate, and in some cases to neither or both.
It might also depend on whether x or y (box dimensions) is the larger,
and their relationship to the slope to determine
whether the horizontal or vertical line or the corner or the box
is where the intersection is.
The cases are: slope is ....
< -1
= -1
-1 < slope < 0
0 (y coordinates equal)
0 < slope < 1
= 1
1 < slope
vertical slope (x coordinates equal)
It's possible some of those collapse, being the same as an adjacent one.
Then you'll have:
either ± x/2 or y/2
or ± slope * (x or y)/2
or no x or y adjustment for the horizontal or vertical cases.
But since this is your project rather than mine,
I'm not going to work all that out in detail for you,
but this should help you get started.
If the points are (r, s) and (u,v) distance = sqrt ( (r-u)^2 + (s - v)^2 )
Finding the coordinates is not too difficult, but there are lots of cases.
Using your example with the points (-5, 2) and (0, 0).
First determine the slope of the line: (0 - 2) / (0 - (-5) ) = -2/5
Let the end point of the red line closest to (-5,2) = (r, s)
and the end point closest to (0,0) = (u, v)
Then we have:
r = -5 + (x/2)
s = 2 + (-2/5) * y/2
u = 0 - x/2
v = 0 - (-2/5) * y / 2
The tricky part is figuring out what sign to use in the middle,
and when to use the slope (for x or y, and how).
There are quite a few different cases.
Call the points A and B.
In this case A = (-5, 2), B = (0, 0)
The slope can be positive or negative,
or zero, and more than one or less than one,
or vertical if the x-coordinates are the same.
So you must break it down by cases,
and in some cases you will apply the slope to the x-coordinate,
in some cases to the y-coordinate, and in some cases to neither or both.
It might also depend on whether x or y (box dimensions) is the larger,
and their relationship to the slope to determine
whether the horizontal or vertical line or the corner or the box
is where the intersection is.
The cases are: slope is ....
< -1
= -1
-1 < slope < 0
0 (y coordinates equal)
0 < slope < 1
= 1
1 < slope
vertical slope (x coordinates equal)
It's possible some of those collapse, being the same as an adjacent one.
Then you'll have:
either ± x/2 or y/2
or ± slope * (x or y)/2
or no x or y adjustment for the horizontal or vertical cases.
But since this is your project rather than mine,
I'm not going to work all that out in detail for you,
but this should help you get started.