Circle-Rectangle collision detection (intersection)

Here is how I would do it: bool intersects(CircleType circle, RectType rect) { circleDistance.x = abs(circle.x – rect.x); circleDistance.y = abs(circle.y – rect.y); if (circleDistance.x > (rect.width/2 + circle.r)) { return false; } if (circleDistance.y > (rect.height/2 + circle.r)) { return false; } if (circleDistance.x <= (rect.width/2)) { return true; } if (circleDistance.y <= (rect.height/2)) … Read more

How do you detect where two line segments intersect? [closed]

There’s a nice approach to this problem that uses vector cross products. Define the 2-dimensional vector cross product v × w to be vx wy − vy wx. Suppose the two line segments run from p to p + r and from q to q + s. Then any point on the first line is representable as p + t r (for a scalar parameter t) and any point … Read more