Algorithm Exercise 23: The simplest and quickest way to figure out whether the following circle intersects with a square __ graphics

Source: Internet
Author: User
The simplest and quickest way to figure out whether the following circle intersects a square. "
3D coordinate system origin (0.0,0.0,0.0)
Circular:
Radius r = 3.0
Center o = (*.*, 0.0, *.*)

Square:
4 angular coordinates;
1: (*.*, 0.0, *.*)
2: (*.*, 0.0, *.*)
3: (*.*, 0.0, *.*)

4: (*.*, 0.0, *.*)

————————————————————————————————————

This problem online search, seemingly there is no answer, bad comparison, I will describe my practice, there are wrong also please point out

First you need to compute the center of the Square and the center of the circle.

This problem can be converted to two-dimensional, so I have the following is based on the two-dimensional answer ...

The centre of the Circle is the center of the circle, there is nothing to say, here calculates the center of the next square: and the length of the side

struct rectangle{
	float rectangleX1;
	Float rectangley1,rectanglex2, rectangleY2,
				rectangleX3, rectangleY3,
				rectangleX4, rectangleY4;

	float CenterX, centery;
	float Edge;

	void Calcproperty () {
		CenterX = rectanglex1+rectanglex2+rectanglex3+rectanglex4;
		CenterX/= 4.0;
		CenterY = Rectangley1+rectangley2+rectangley3+rectangley4;
		CenterY/= 4.0;
		Edge = (Rectanglex1-centerx) <0? (-rectanglex1+centerx):(Rectanglex1-centerx);
		Edge = 2*edge;
	}

;


To know whether a circle and a rectangle intersect, you can actually judge the center of two graphs,

At first, a lot of people would use a two-center distance and (R+EDGE/2) to judge


But as shown, (assuming that the intersection is a little bit into the rectangle, the diagram is not clear ha), O1o2 distance is greater than the edge/2+r, but does intersect,

But from the diagram we found that the O1O2 on the y-axis can be compared to (R+EDGE/2), if (R+EDGE/2) >y (O1O2) then the two do intersect, but be aware


The figure O1o2 on the y-axis is indeed less than (R+EDGE/2), but the distance on the x-axis is greater than (R+EDGE/2), so the distances on both axes need to be judged and are less than (R+EDGE/2) intersecting.

But there is one special point, the circle on the diagonal.


From the above we see that the XY axis here is less than (R+EDGE/2) but actually does not intersect, so also need to add two graphics center distance must be less than (rectangular diagonal distance/2+r) This is the farthest distance of the two graph intersection


Therefore, it is concluded that the center distance of the two graphs is less than that of the X and Y axes (R+EDGE/2), and the center distance is less than the rectangular diagonal/2+r.

I'm here to enumerate the three kinds of cases on the x Y axis and on the diagonal to conclude that this may be the wrong conclusion, and I hope we can help you correct it.

============================================================================//Name:JudgeInterception.cpp/A UTHOR:YLF//Version://Copyright:your Copyright notice//Description:hello World in C + +, Ansi-style/ /============================================================================ #include <iostream> #include

<math.h> using namespace std;
	struct circle{float CenterX;
	float CenterY;
float R;

};
	struct rectangle{float rectangleX1;

	Float rectangley1,rectanglex2, rectangleY2, RectangleX3, RectangleY3, rectangleX4, rectangleY4;
	float CenterX, centery;

	float Edge;
		void Calcproperty () {CenterX = rectanglex1+rectanglex2+rectanglex3+rectanglex4;
		CenterX/= 4.0;
		CenterY = Rectangley1+rectangley2+rectangley3+rectangley4;
		CenterY/= 4.0; Edge = (Rectanglex1-centerx) <0?
		(-rectanglex1+centerx):(Rectanglex1-centerx);
	Edge = 2*edge;

}

};
BOOL Judgeinterception (Circle circle,rectangle rect); Float Getdistance (flOat pt1x, float pt1y, float pt2x, float pt2y);
	int main () {Circle Circle;
	Rectangle rect;
	CIRCLE.R = 3.0;
	cin>>circle.centerx>>circle.centery; Cin>>rect.rectanglex1>>rect.rectangley1>>rect.rectanglex2>>rect.rectangley2 >>


	rect.rectanglex3>>rect.rectangley3>>rect.rectanglex4>>rect.rectangley4;
	if (Judgeinterception (circle,rect)) cout<< "intercept!";
	else cout<< "not intercept!";
return 0; BOOL Judgeinterception (Circle circle,rectangle rect) {rect.
	Calcproperty (); if (:: FABSF (Rect.centerx-circle.centerx) < (RECT.EDGE/2+CIRCLE.R) &&:: FABSF (Rect.centery-circle.centery ) < (RECT.EDGE/2+CIRCLE.R) && getdistance (Circle.centerx,circle.centery,rect.centerx,rect.centery) < (
	Getdistance (rect.centerx,rect.centery,rect.rectanglex1,rect.rectangley1) +CIRCLE.R)) return true;
else return false; Float getdistance (float pt1x, float pt1y, float pt2x, float pt2y) {float distance = 0.0;
	Distance = (pt1x-pt2x) * (PT1X-PT2X) + (pt1y-pt2y) * (PT1Y-PT2Y);
	Distance =:: Sqrtf (distance);
return distance;
 }



Give a few test knots

(1) Center in the midpoint, the rectangle intersects its right a little bit

0
0
2.9
1.5
5.9
1.5
5.9
-1.5
2.9
-1.5
intercept!


Below I give the diagonal case 3/sqrt (2) is almost equal to 2.15.

I'm going to use 2.1 here to just intersect a little bit.

0
0
5.1
2.1
5.1
5.1
2.1
5.1 2.1 2.1 intercept!

With 2.2 Here, it's almost intersect.

0
0
2.2
2.2
5.2
2.2 5.2 5.2 2.2
5.2 not
intercept!



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.