public bool CollidesWith(Rectangle other) { decimal left1 = X; decimal left2 = other.X; decimal right1 = X + Width; decimal right2 = other.X + other.Width; decimal top1 = Y; decimal top2 = other.Y; decimal bottom1 = Y + Height; decimal bottom2 = other.Y + other.Height; if (bottom1 < top2 || top1 > bottom2 || right1 < left2 || left1 > right2) return false; return true; }
public bool CollidesWith(Rectangle rect) { Circle circle = this; double half_width = rect.Width / 2.0; double half_height = rect.Height / 2.0; double distance_x = Math.Abs(circle.X - rect.X - half_width); double distance_y = Math.Abs(circle.Y - rect.Y - half_height); // Quick rule-out checks. if (distance_x > half_width + Radius || distance_y > half_height + Radius) return false; if (distance_x <= half_width || distance_y <= half_height) return true; // Measure the distance between the corner and the circle. return Math.Sqrt(Math.Pow(distance_x - half_width, 2) + Math.Pow(distance_y - half_height, 2)) <=Radius; }