The circle and the rectangle intersect, and the circle and the rectangle

Source: Internet
Author: User

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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.