And this is the easy case, all nice and flat :p
The problem becomes quite simple if you draw a rectangle around R0,C0 and around R0,C1 and around R0,C2, and ...
and around R2,C0, and R2,C1 and ...
This gives you a nice rectangular grid, where position (x,y) of Rx,Cy is easy to compute. The horizontal/vertical distance from the left/top line of the rectangle is also easy to derive then.
Still with me?
Next, split your rectangle in 4 sub-rectangles, by one horizontal line across from left to right, and one one vertical line down through the middle. Which sub-rectangle you are in is then also easy to decide from the distance from above.
For the top-left and bottom-right sub-rectangle, the dividing line runs from the left bottom to right top corner. For the purpose of the discussing, assume top-left has coordinate (0, 0), left-bottom (0, 1), and right-top (2, 0)
Warning: Math ahead!
General form of a line: y = a*x + b
(0, 1) at the line -> 1 = a*0 + b -> b =1
(2, 0) at the line -> 0 = a*2 + 1 -> a=-1/2
thus the line through (0, 1) and (2, 0) has the formula y = -1/2*x + 1
written somewhat differently y + 1/2*x - 1 = 0
Now comes the smart part. This line formula can also be seen as a distance function of all points to the line. Points on the line have a distance 0 of course, eg (1, 1/2) -> 1/2 + 1/2 * 1 - 1 -> 0
Points away from the line get other values, eg (0,0) -> 0 + 1/2*0 - 1 -> -1
and (2,1) -> 1 + 1/2 * 2 - 1 -> + 1
In other words, for a point in the left-top or bottom-right sub-rectangles, you can with some simple arithmetic decide whether it is in the left-upper half ( < 0 ), on the line itself ( == 0 ), or in the right-bottom half ( > 0 ).
Obviously, you can do the same kind of trick in the other two sub-rectangles.
My project: Messing about in FreeRCT
, dev blog
, and IRC #freerct at oftc.net