Zoj 1450 minimal circle [minimum coverage round problem]

Source: Internet
Author: User
Link: http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemcode = 1450 http://acm.hust.edu.cn/vjudge/contest/view.action? Cid = 29328 # Problem/F Minimal circle

Time Limit: 5 seconds memory limit: 32768 KB

You are to write a program to find a circle which covers a set of points and has the minimal area. There will be no more than 100 points in one problem.

Input

The input contains several problems. The first line of each problem is a line containing only one integer n which indicates the number of points to be covered. The next n lines contain
N points. Each point is represented by X and Y coordinates separated by a space. After the last problem, there will be a line contains only a zero.

Output

For each input problem, you shoshould give a one-line answer which contains three numbers separated by spaces. The first two numbers indicate the X and Y coordinates of the result circle,
And the third number is the radius of the circle. (Use escape sequence %. 2f)

Sample Input

2
0.0 0.0
3 0
5
0 0
0 1
1 0
1 1
2 2
0

Sample output

1.50 0.00 1.50
1.00 1.00 1.41

Source: Asia 1997, Shanghai (Mainland China)

Question: give you n vertices, and find the smallest circle covering all vertices. The coordinates and radius of the output circle.

Algorithm: Calculate geometric vector cross product, vector dot product, distance between two points, Triangle outfield A fixed circle, and a fixed circle at two points.

Idea: paste the information I read on the Internet. I feel that I cannot explain it better than him: the source of the information.

The algorithm introduced here is to first select any two points and use the line of the two points as the diameter for the circle. Then we can judge the remaining points to see if they are all in the circle (or on the circle). If they are all in, it indicates that the circle has been found. If none of them exist: assume that the first two vertices we use are p1, P [2], and the first vertex we find is not in the circle (or on the circle) the point is P [I], so we use this point P [I] To find the smallest covering circle covering P1 to P [I-1.

So, how should we find the minimum covering circle of the point P [I] from P1 to P [I-1?

We first use P1 and P [I] to do the circle, and then from 2 to the I-1 to determine whether a bit is not on this circle, if both are in, it indicates that the circle covering 1 TO THE I-1 has been found. If none of them exist: Suppose we find the first vertex not on this circle as P [J], so we use two known Points P [J] and P [I] To find the smallest covering circle covering 1 to J-1.

For the two known vertices P [J] and P [I] To find the smallest covering circle, as long as from 1 to the J-1, the K vertex calculates P [K], P [J], p [I] the circle of the three points, and then judge whether k + 1 to the J-1 are on the circle, if are in, it indicates to find the circle; if there is any other, use the new P [k] to update the circle.

As a result, this problem is transformed into several subproblems to solve.

Since the three points determine a circle,In general, our process involves the process of finding a circle from no definite point to a definite point, then to two definite points, and then to three definite points..

We will not describe the proof of correctness and computation of complexity here. Let's look at the complete algorithm Introduction: ACM calculates the geometric least circle coverage algorithm.

For details: PS: We recommend that you use your own templates for acmer..


A. How can we find the circle through three points?
Calculate the cross product first.
If the cross product is 0, that is, the three points are in the same straight line, find the farthest pair of points, and use their line diameter as the circle;
If the cross product is not 0, that is, the three vertices are not collocated, it is the second problem. How can we find the outer circle of the triangle?

B. How do I find the outer circle of a triangle?
Suppose three points (x1, Y1), (X2, Y2), (X3, Y3 );
If the linear L1 equation of (x1, Y1) and (X2, Y2) is AX + by = C, its midpoint is (midx, midy) = (X1 + x2) /2, (Y1 + y2)/2), L1 vertical line equation is a1x + b1y = C1; then in its vertical line equation a1 =-B = x2-x1, b1 = A = y2-y1, C1 =-BMidx +Midy = (X2 ^ 2-x1 ^ 2) + (Y2 ^ 2-y1 ^ 2)/2;
Likewise, we can know the vertical line equations of (x1, Y1) and (X3, Y3.
The intersection of the two vertical lines is the center of the center.

C. How to calculate the intersection of two straight lines?
Set the two straight lines to a1x + b1y = C1 and a2x + b2y = c2.
Set a variable det = A1B2-A2B1;
If det = 0, the two straight lines are parallel. if not equal to 0, the intersection is X = (B2C1-B1C2)/det, y = (A1C2-A2C1)/det;

D. There is wood ..

Detailed handling changes: Finding the outer circle of a triangle: recommended formulas for the triangle center, center of gravity, and outer Coordinate

P = (a ^ 2 + B ^ 2 + C ^ 2)/2;

Q = 1/(1/(p-a ^ 2) + 1/(p-B ^ 2) + 1/(p-C ^ 2 ));

λ 1 = Q/(p-a ^ 2), λ 2 = Q/(p-B ^ 2), λ 3 = Q/(p-C ^ 2 ); (Note: λ 1 + λ 2 + λ 3 = 1)

Center: h (x, y) = λ 1 * a (x, y) + λ 2 * B (X, Y) + λ 3 * C (x, y ),

Center of gravity: g (x, y) = 1/3 * a (x, y) + 1/3 * B (X, Y) + 1/3 * C (x, y ),

External: O (x, y) = (1-λ 1)/2 * a (x, y) + (1-λ 2)/2 * B (X, Y) + (1-λ 3)/2 * C (x, y );


Templated: the center of the external circle is the point of the vertical line in each side, that is, the external heart.

// The outer circle center of the triangle, which has previously guaranteed that the three points A, B, C are not collocated void trianglecircumcircle (point a, point B, point C) {vector A = C-B, B = A-C, c = B-A; double da = POW (Dist (a), 2), DB = POW (Dist (B), 2 ), dc = POW (Dist (c), 2); double PX = (DA + dB + DC)/2.0; double Q = 1/(1/(PX-da) + 1/(PX-DB) + 1/(PX-DC); // t1 + T2 + T3 = 1 double T1 = Q/(PX-da ), t2 = Q/(PX-DB), T3 = Q/(PX-DC); point O = A * (1-t1)/2.0) + B * (1-t2) /2.0) + C * (1-t3)/2.0); // external // point H = A * t1 + B * t2 + C * T3; // center of gravity // point G = A * (1/3) + B * (1/3) + C * (1/3); // center of gravity = O ;}
Reference: Go upstairs. Source: Click to open the link.
This is also the first time I went to DEQ and thought this community was amazing! Proof of thinking on the upstairs: I feel very clear when I click to open the link. I don't need to check this proof. Five-hearted question about a triangle: Five-hearted formula of a triangle in Baidu Baike: the formula of the Five-hearted formula of an ACM triangle has not been proved yet. Wait for your template ing...
PS: Let's take a look at a patchwork question.
I found a problem when I started looking for ideas. I didn't understand it at the time. Now it seems that the idea is the same as this, and the code is concise. Here we recommend: Click to open the link Analysis of game errors:
The diameter of the convex hull is the diameter of the smallest covered circle. obviously wrong, an equi-edge triangle is directly rejected. However, the idea is provided. If there are many vertices during enumeration, can we find the smallest covering circle starting from the diameter? Is it more efficient? Considering that the number of points here is very small, it is complicated to determine the convex hull and then determine the diameter of the convex hull using the rotating jamming case. So here is a problem: Determine whether the endpoint of the convex hull diameter must be in the circle with the smallest coverage .... Code.
F Accepted 188 KB MS 0 C ++ (G ++ 4.4.5) 4104 B 2013-08-20 23:05:22

# Include <stdio. h> # include <math. h >#include <algorithm> # include <iostream> using namespace STD; const int maxn = 110; int N; struct point {Double X, Y; point () {} Point (double _ x, double _ y) {x = _ x; y = _ y;} point operator-(const point & B) const {// returns point (x-b.x, y-b.y);} point operator + (const point & B) const {return point (x + B. x, Y + B. y);} point operator * (const double & K) const {retu Rn point (K * X, K * Y);} double operator * (const point & B) const {// Point product return x * B. X + Y * B. y;} double operator ^ (const point & B) const {// return x * B. y-y * B. X ;}} P [maxn]; typedef point vector; double dist (point a, point B) {// return SQRT (a-B) the length of the vector AB) * (a-B);} double dist (point a) {return SQRT (A * A);} Point midpoint (point a, point B) {// calculate the return point (. X + B. (x)/2.0, (. Y + B. y)/2.0 );} Double radius; // radius point center; // center of the circle // The center of the outer circle of the triangle. Previously, void trianglecircumcircle (point, point B, point C) {vector A = C-B, B = A-C, c = B-A; double da = POW (Dist (a), 2 ), DB = POW (Dist (B), 2), Dc = POW (Dist (c), 2); double PX = (DA + dB + DC)/2.0; double q = 1/(1/(PX-da) + 1/(PX-DB) + 1/(PX-DC )); // t1 + T2 + T3 = 1 double T1 = Q/(PX-da), T2 = Q/(PX-DB), T3 = Q/(PX-DC ); point O = *(( 1-t1)/2.0) + B * (1-t2)/2.0) + C * (1-t3)/2.0 ); // external heart // point H = A * t1 + B * t2 + C * T3; // center/point G = A * (1/3) + B * (1/3) + C * (1/3); // center of gravity = O;} // If the J-th vertex PJ is not within the circle with the diameter of the first vertex and I-th vertex // note: M = J-1 // repeat the diameter of point I and Point J to continue to judge void minidiscwith2point (point pi, point PJ, int m) {center = midpoint (PI, PJ); radius = dist (PI, PJ)/2.0; For (int K = 1; k <= m; k ++) // If P [k] is not in the circle, enumerate the outer circle of the triangle [Pi, PJ, P [k] {If (Dist (C Enter, P [k]) <= radius) continue; // If the k-th vertex is not in the current circle, double cross = (Pi-PJ) ^ (P [k]-PJ); If (cross! = 0) // if the three points are not co-located {trianglecircumcircle (PI, PJ, P [k]); radius = dist (center, Pi );} else // if the three-point collinearity {double d1 = dist (PI, PJ); // d1 is definitely not the largest double D2 = dist (PI, P [k]); double D3 = dist (PJ, P [k]); If (D2> = D3) {center = midpoint (PI, P [k]); radius = dist (PI, P [k])/2.0;} else {center = midpoint (PJ, P [k]); radius = dist (PJ, P [k]) /2.0; }}/// the current circle cannot cover the pi of the I point. The circle with the diameter of the first vertex and the I point is judged again. // note: M = i-1void m Inidiscwithpoint (point pi, int m) {center = midpoint (PI, P [1]); // radius = dist (PI, P [1])/2.0; For (Int J = 2; j <= m; j ++) // determine whether the second point to the I-1 point is included in the current park {If (Dist (center, P [J]) <= radius) continue; else minidiscwith2point (PI, P [J], J-1); // If the J vertex is not in the current circle} int main () {While (scanf ("% d", & N )! = EOF) {If (n = 0) break; Double X, Y; For (INT I = 1; I <= N; I ++) {scanf ("% lf", & X, & Y); P [I] = point (x, y) ;}if (n = 1) {printf ("%. 2lf %. 2lf 0.00 \ n ", P [1]. x, p [1]. y); continue;} // any two points of the first circle can be enumerated. My personal opinion is to take the point on the convex hull diameter as the circle diameter first, judge again // considering the trouble of finding the convex hull diameter, the sum of radius = dist (P [1], p [2])/100 is only 2.0 points; // first enumerate the first and second vertices for diameter center = midpoint (P [1], p [2]); For (INT I = 3; I <= N; I ++) // The first point must be ensured that the first point to the I-1 has been overwritten {If (Dist (center, P [I]) <= radius) continue; // find the first vertex that cannot be overwritten by the current circle else minidiscwithpoint (P [I], I-1); // The current circle cannot override P [I]} printf ("%. 2lf %. 2lf %. 2lf \ n ", center. x, center. y, radius) ;}return 0 ;}

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.