Let's say we have an arbitrary number of different sized rectangles randomly placed on a 2D Cartesian plane. For the sake of brevity, let's also say that none of these rectangles overlap each other.
Drawing a vector from the center of one of these rectangles, and moving to the center of another arbitrary rectangle, how can we return an array of rectangles which intersect this path?
My first thought is to:
- Normalize the vector
- Use linear interpolation to get each XY point along this vector, between V1 and V2 (where V1 = center of 1st rect, and V2 is center of 2nd rect)
- Perform a "point in rect" check, for every existing rectangle that isn't the 1st and 2nd rects
- Return the array of any rectangles found in the previous step
While this would likely work, is there a more optimal way of doing things? Perhaps some way of eliminating having to loop through every existing rectangle?