ID:1067544
 


I am trying to figure out the formula for finding where my grey lines intersect with the green box (circled in red). This would be for any grid or coordinate system. Orange represents a single point within a set of points and green represents the coordinate system minimum and maximum point range. The grey lines represent cardinal and diagonal direction. We are trying to find the formula that calculates at which point each direction would intersect maximum or minimum bounds based on a direction value from a single position within the bounds.
I was about to write about Pythagoras but I believe there is a much simpler solution. Any diagonal that touches the boundary can be made into a right triangle with both "flat" sides equal, one of them being the border itself.

All you have to do is get the distance between you and the boundary, then subtract that from your coordinate in the other direction. So if you measure the distance to the boundary north, you would subtract that from your X

To figure out which side to use you could test the sum of your x and y coordinates for northwest/southeast diagonals, and for the other direction...wait I'm confused. I have to run to the store...

:P

EDIT: Okay I'm back and I figured it out. For northeast/southwest diagonals, just test if x > y or if x < y. Here's a quick diagram to show what I mean:

It has to work for any point within the boundaries. So if you have 1,1 to 10,10 and you're at 7,7 the values will be different than if you were centered at point 4,6
Alright well this is what I came up with:

mob
var
ne_x
ne_y

se_x
se_y

nw_x
nw_y

sw_x
sw_y


proc
Calculate_Intercepts(mob/source)

if(source.y > source.x) //For northeast and southwest diagonals, coordinates directly tell which side of the center
source.ne_y = world.maxy //diagonal we are on. When Y is greater than X, intercepts always hit the north and west
source.ne_x = source.x + (world.maxy - source.y) //boundaries.

source.sw_y = source.y - source.x + 1
source.sw_x = 1

else if(source.y < source.x)
source.ne_y = source.y + (world.maxx - source.x)
source.ne_x = world.maxx

source.sw_y = 1
source.sw_x = source.x - source.y + 1

else //x and y are equal
source.ne_y = world.maxy
source.ne_x = world.maxx

source.sw_y = 1
source.sw_x = 1

//----------------------------------------------------------//

var/sum = source.x + source.y - 1

if(sum > world.maxx) //For northwest and southeast diagonals, the coordinates have to be added together.
source.nw_y = world.maxy
source.nw_x = source.x - (world.maxy - source.y)

source.se_y = source.y - (world.maxx - source.x)
source.se_x = world.maxx

else if(sum < world.maxx)
source.nw_y = sum
source.nw_x = 1

source.se_y = 1
source.se_x = sum

else

source.nw_y = world.maxy
source.nw_x = 1

source.se_y = 1
source.se_x = world.maxx


I also made a little demo if you want to see it in action:

http://wikisend.com/download/166516/intercepts_src.zip