circle - rectangle collision in 2D, most efficient way
- by john smith
Suppose I have a circle intersecting a rectangle, what is ideally the least cpu intensive way between the two?
method A
calculate rectangle boundaries
loop through all points of the circle and, for each of those, check if inside the rect.
method B
calculate rectangle boundaries
check where the center of the circle is, compared to the rectangle
make 9 switch/case statements for the following positions:
top, bottom, left, right
top left, top right, bottom left, bottom right
inside rectangle
check only one distance using the circle's radius depending on where the circle happens t be.
I know there are other ways that are definitely better than these two, and if could point me a link to them, would be great but, exactly between those two, which one would you consider to be better, regarding both performance and quality/precision?
Thanks in advance.