So I have a bounded plane (X axis range 0-1000, Y axis range 0-2000).
there are multiple points (x,y) in it, and a rectangle (4 points that makes the rectangle). Some points are in the rectangle and some aren't. like this:
The rectangle is able to move and change it's size over time. while the points are static and won't change. I need to check what points are inside the rectangle after it changes.
Problem:
Giving a rectangle, what points are inside of it?
Set<Point> getPointsInsideRectangle(Rectangle rec);
My naive solution:
for each point
- check if it inside the rectangle, if yes, add the point to a Point Set
return the Point Set
My thoughts:
It seems that iterating over all the points everytime the rectangle changes seems extremely not efficient with big CPU usage (there are many points). Something tells me to use HashMap but since the rectangle is a X range and a Y range (and not just 1 value), it seems I cannot use it.
My questions:
1. Is there a java data structure that can handle this situation?
2. if not, any thoughts on how to implement a data structure that holds all the points, and giving a rectangle, returns the points in it?