Preventing two draggable circles from overlapping

2019-09-17 21:12发布

问题:

Ive looked everywhere i could for what i am guessing has a simple solution. But i am a new programmer and am having a bit of trouble. I have a few draggable image view circles in my view and i want to prevent any overlapping between the image views. Im not looking for any serious collision solutions(friction, vectors, etc...), just something to stop the image views from going any further once they make contact with each other.

Any guidance to the solution is appreciated. Thanks.

回答1:

That should just be a simple comparison of location and radii... When a drag is attempted, compute the distance between any two circles' center points (using the new location), and if that's less than the sum of the radii, they overlap. Pseudocode:

onMoveAttempted() { distance = sqrt (((moving(x) - existingcircle(x))^2) + ((moving(y) - existingcircle(y))^2))

if (distance < (radius of moving circle + radius of existing circle) then prohibit the move, as the circles would overlap end if }