HDU 6206 Apple (high-precision && computational Geometry && Three-point construction Circle for center radius)

Source: Internet
Author: User

Test instructions: give four points, ask you whether the fourth point in the first three points constitute the circle, if the output of "Accepted" outside the circle, otherwise output "rejected", the title guarantee the first three points are not in a straight line.

Analysis: A simple computational geometry problem, if you can know the center and radius (radius) and the distance between the fourth and center points (Distance), we can determine whether the fourth point is outside the circle, such as Distance > Radius is outside the circle. The center and radius of the three-point structure circle are the formulas that can be deduced.

(Refer to ==> http://blog.csdn.net/death10001/article/details/77389128)

The setting has

According to the distance between two points formula R2 = (x-x0) 2 + (Y-Y0) 2 We will just the X0 algebra and X (any one of three points) to go in-pass a little to get

[2*x* (Bc-ad) + (DE-BF)/2 (BC-AD)]2 + [2*y* (bc-ad) + (AF-CE)/2 (BC-AD)] 2

Find the denominator is the same, can-pass. Then, or according to two-point spacing we can use the same method to get the fourth point and the distance between the center and the denominator is also 2 (BC-AD), so the process of operation is not to divide operations, as long as the comparison of two molecules can be, high-precision problems can only be solved with BigInteger.

ImportJava.util.Scanner;ImportJava.util.*;Importjava.math.*;ImportJava.io.*;classpo{BigInteger x, y;} Public classmain{ Public Static BooleanSolve (po p1, po p2, PO P3, po p) {//-------------------------------------------------------------------------------------BigInteger a=p1.x.subtract (p2.x); BigInteger b=p1.y.subtract (P2.Y); BigInteger C=p1.x.subtract (p3.x); BigInteger D=p1.y.subtract (P3.Y); BigInteger e=((p1.x). Multiply (p1.x)). Subtract ((p2.x). Multiply (p2.x)). Subtract (((P2.Y). Multiply (P2.Y)). Subtract (((p1.y        ). Multiply (P1.Y))); BigInteger F=((p1.x). Multiply (p1.x)). Subtract ((p3.x). Multiply (p3.x)). Subtract (((P3.Y). Multiply (P3.Y)). Subtract (((p1.y        ). Multiply (P1.Y))); BigInteger=NewBigInteger ("2"); //-------------------------------------------------------------------------------------BigInteger TMP1=((p1.x). Multiply ((b.multiply (c)). Subtract ((a.multiply (d)))). Add ((Multiply (e)). D.multiply (        (B.multiply (f)))); TMP1=tmp1.multiply (TMP1); BigInteger TMP2=((P1.Y). Multiply ((b.multiply (c)). Subtract ((a.multiply (d)))). Add ((Multiply (f)). A.multiply (        (C.multiply (e)))); TMP2=tmp2.multiply (TMP2); BigInteger R= Tmp1.add (TMP2);///Calculate the "length" of the radius (actually the molecule of algebraic type)//-------------------------------------------------------------------------------------TMP1=((p.x). Multiply ((b.multiply (c)). Subtract ((a.multiply (d)))). Add ((Multiply (e)). D.multiply ((        B.multiply (f))); TMP1=tmp1.multiply (TMP1); TMP2=((P.Y). Multiply ((b.multiply (c)). Subtract ((a.multiply (d)))). Add ((Multiply (f)). A.multiply ((        C.multiply (e))); TMP2=tmp2.multiply (TMP2); BigInteger Dis= Tmp1.add (TMP2);///Calculates the "distance" from the center to the fourth point        if(R.compareto (Dis) < 0)return true; Else return false; }     Public Static voidMain (string[] args)throwsexception{Scanner Cin=NewScanner (NewBufferedinputstream (system.in)); intNcase =Cin.nextint ();  for(intT=1; t<=ncase; t++) {PO P1=NewPO (); PO P2 =NewPO (); PO P3=NewPO (); PO aim=NewPO (); p1.x= Cin.nextbiginteger (); P1.Y =Cin.nextbiginteger (); p2.x= Cin.nextbiginteger (); P2.Y =Cin.nextbiginteger (); p3.x= Cin.nextbiginteger (); P3.Y =Cin.nextbiginteger (); Aim.x= Cin.nextbiginteger (); AIM.Y =Cin.nextbiginteger (); Booleanres =Solve (P1, P2, p3, AIM); if(RES) System.out.println ("Accepted"); ElseSystem.out.println ("Rejected"); }    }}
View Code

Blindness: There is actually a simpler way to calculate the square of the center (x0, y0) and radius directly with a more straightforward formula r^2

X0= ((y2-y1) * (y3*y3-y1*y1+x3*x3-x1*x1)-(y3-y1) * (Y2*Y2-Y1*Y1+X2*X2-X1*X1))/(2.0* ((x3-x1) * (y2-y1)-(x2-x1) * (y3-y1) ));

Y0= ((x2-x1) * (x3*x3-x1*x1+y3*y3-y1*y1)-(x3-x1) * (x2*x2-x1*x1+y2*y2-y1*y1))/(2.0* ((y3-y1) * (x2-x1)-(y2-y1) * (X3-X1) ));

R^2= (x1-x0) * (x1-x0) + (y1-y0) * (Y1-Y0);

It involves division, that can be solved with BigDecimal, reference ==> http://blog.csdn.net/cillyb/article/details/78012069

HDU 6206 Apple (high-precision && computational Geometry && Three-point construction Circle for center radius)

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.