I've looked around but haven't exactly found what I'm looking for. At first, I thought this would be a simple problem.. Until I tried to implement it and realized there were many cases and I could not find a generic solution.
Say, I have two AABBs. If their outlines intersect, I want to get all their points of intersection. The maximum amount of points is 4, the minimum non-zero amount of points is 1.
My AABBs are composed of a min
and max
point that are always automatically sorted so that min
always has the lowest x-y values and max
always has the highest x-y values.
The image below shows a few AABBs. The blue lines indicate their points of intersection. Note that if a side of an AABB overlaps another side, it's kind of like having an infinite amount of intersection points along the line-segments that overlap; in those cases, I want to ignore them and say, "There are no intersection points there".
Anyone got an idea for an algorithm? I realize I haven't posted any code and I'm pretty sure that's frowned upon but I have thought about this problem quite a bit. As for why I want to do this.. I duno', I'm just curious as to whether it's possible to do this cleanly.
Oh, yeah, my AABBs have methods like these that should be useful:
Vector<2> getSize () const;
Point<2> getCenter () const;
//0=Quadrant-1, 1=Quadrant-2, 2=Quadrant-3, 3=Quadrant-4, else, error
Point<2> getCorner (int index) const;
//0=right, 1=up, 2=left, 3=down, else, error
LineSegment2D getSide (int index) const;
I could do an ugly hack and just do line-segment <-> line-segment checks but, I duno'.. It feels kinda', well, ugly and inelegant. I'm pretty sure there must be some trickery I could do with the min
and max
points to do this quickly.