I have 4 points that form some quadrilateral. The lines can’t cross or anything like that, it should be a square, rectangle, rhombus, parallelogram, etc.
The lines that connect them break the field into 9 regions. With a square, it would look like a tic-tac-toe board (#), but with other shapes the lines will be at angles.
A point falls randomly into this 9-region field. I know the coordinates of the random point, as well as the coordinates of the quadrilateral’s 4 corners.
Is there any way I can find which field contains the point without using the equations of the lines?
I’m basically looking for something like
if(p.x > q1.x && p.x < q4.x && p.y < q3.y) {
//It's in the top left region
}
etc
I’m thinking that this isn’t possible when using sloped lines (rather than a square/rectangle) without solving the line equations. But I thought I’d run it by the math guys first. THANKS!
Anything you do is effectively “using the equations of the lines”, so I’m not sure what to make of that condition. I assume you just want easy inequalities to check which region the random point
(x,y)is in, so that’s what I’ll show you how to do.From your question, it sounds like you always have a parallelogram, so let’s assume that the points are
(0,0),(a,b),(c,d), and(a+c,b+d), which makes the explanation a little easier to follow. To fix your mental picture, imagine that(a,b)is roughly “to the right” of(0,0)and(c,d)is roughly “above”(0,0). Then the equations for the “horizontal” lines are-bx+ay=0and-bx+ay=-bc+ad, so you get three possibilities, depending on how-bx+cycompares to0and-bc+ad:Similarly, the equations for the “vertical” lines are
dx-cy=0anddx-cy=da-bc, so the three possibilities, depending on howdx-cycompares to0and toda-cb:Of course, if
da-cbis negative, then the three possibilities in each case are “less thanda-cb“, “betweenda-cband0“, and “greater than0” instead. Finally, if equality ever holds, then the point(x,y)is actually on one of the lines rather than in one of the regions.