The circle and the rectangle intersect, and the circle and the rectangle
Today the first write blog, today write their own circle and rectangle intersection of the program to remember, write their own Cocos2D-X when thinking about the game.
Idea: 1: first determine whether the center of the circle is in the rectangle
2: Determine whether the four points of the rectangle are in the circle. If the four points of the rectangle are not in the circle, the possibility that the center of the rectangle is outside the four corner areas of the rectangle can be ruled out.
3: Determine whether the circle and the four sides of the rectangle intersect.
The program code is as follows:
// Used to determine whether a circle contains a vertex
Bool LeapCircle: containsPoint (const Point & pPoint) const {
Return (this-> CircleOrigin. x-pPoint.x.) * (this-> CircleOrigin. x-pPoint.x) + (this-> CircleOrigin. y-pPoint.y.) * (this-> CircleOrigin. y-pPoint.y) <= this-> CircleRadius * this-> CircleRadius;
}
// Determine whether the circle and the rectangle intersect
Bool LeapCircle: intersectsRect (const Rect & rect) const {
Bool intersect1 = rect. containsPoint (this-> CircleOrigin); // is the center inside the rectangle?
Bool intersect2 = this-> containsPoint (Point (rect. getMinX (), rect. getMinY () |
This-> containsPoint (Point (rect. getMinX (), rect. getMaxY () |
This-> containsPoint (Point (rect. getMaxX (), rect. getMinY () |
This-> containsPoint (Point (rect. getMaxX (), rect. getMaxY (); // are the four rectangle points in the garden?
Bool intersect3 = this-> CircleOrigin. x <rect. getMinX () & this-> CircleOrigin. y> rect. getMinY () & this-> CircleOrigin. y <rect. getMaxY () & abs (this-> CircleOrigin. x-rect.getMinX () <this-> CircleRadius; // Do You Want To intersection with the left side of the rectangle?
Bool intersect4 = this-> CircleOrigin. x> rect. getMaxX () & this-> CircleOrigin. y> rect. getMinY () & this-> CircleOrigin. y <rect. getMaxY () & abs (this-> CircleOrigin. x-rect.getMaxX () <this-> CircleRadius; // Do You Want To intersection the right side of the rectangle?
Bool intersect5 = this-> CircleOrigin. y <rect. getMinY () & this-> CircleOrigin. x> rect. getMinX () & this-> CircleOrigin. x <rect. getMaxX () & abs (this-> CircleOrigin. y-rect.getMinY () <this-> CircleRadius; // Do You Want To intersection with the bottom of the rectangle?
Bool intersect6 = this-> CircleOrigin. x> rect. getMaxY () & this-> CircleOrigin. x> rect. getMinX () & this-> CircleOrigin. x <rect. getMaxX () & abs (this-> CircleOrigin. y-rect.getMaxY () <this-> CircleRadius; // Do You Want To intersection the top side of the rectangle?
Return intersect1 | intersect2 | intersect3 | intersect4 | intersect5 | intersect6;
}