Given a list of circles with its coordinates (x and y) that are moving every second in different direction (South-East, South-West, North-East and North-West), and the circle will change direction if it hits the wall sort of like bouncing, so how do we detect if any of them collide or overlap with each other ? I am not sure if we can use some data structures like a Binary Search Tree because since all the coordinates vary every seconds, so the tree will have to re-build accordingly. Or can we use Vertical Sweep Line Algorithm each time ? Any ideas on how to do this in a efficient way ?
Given a list of circles with its coordinates (x and y) that are moving
Share
Your shapes are only circles, so:
Suppose your rectangle’s boundaries are
X1andX2on the horizontal axis andY1andY2on the vertical axis (withX1 < X2andY1 < Y2). In the first case, if the center of your circle is(x, y)and its radius isryou have to check if :x-r < X1?x+r > X2?y-r < Y1?y+r > Y2?If any of these is true, your circle touches the boundary of the rectangle.
In the second case, suppose your circles are defined by
(x1, y1, r1)and(x2, y2, r2)respectively. You have to check if(x1 - x2)^2 + (y1 - y2)^2 < (r1 + r2)^2. If this is true, your circles touch each other.