POJ 2826 an easy problem?! Good question

Source: Internet
Author: User

The main idea is that two pieces of wood to form a slot, ask how much rain tank can be installed, pay attention to the vertical fall of rain, thinking is very simple, is the classification of the discussion is a bit bad.
1. If the two segments do not intersect or are parallel, install 0;
2. There is a parallel x-axis, installed 0;
3. If the top cover below, install 0;
4. Other, cross-product to find area.

Directly on the code:

#include <iostream>#include <cmath>#include <stdio.h>Using namespaceSTD;Const Double eps=1e-8;struct point{Doublex;Doubley;};struct line{Point A;Point B;}l1,l2;Double ans;Cross product double Xmult (point p0, point P1, point p2) {return (P1. x-p0. x) * (P2. Y-p0. Y)-(P2. x-p0. x) * (P1. Y-p0. Y);} int dblcmp (double n) {if (Fabs (n) <eps) return0;Return n>0?1:-1;}//determine if they intersect, how to intersect int judge (line L1, line L2) {Double d1=dblcmp (max (L1. A. x, L1. b. x)-min (L2. A. x, L2. b. x));Double d2=dblcmp (Max (L2. A. x, L2. b. x)-min (L1. A. x, L1. b. x));Double d3=dblcmp (Max (L1. A. Y, L1. b. Y)-min (L2. A. Y, L2. b. Y));Double d4=dblcmp (Max (L2. A. Y, L2. b. Y)-min (L1. A. Y, L1. b. Y));Double d5=dblcmp (Xmult (L2. A, L1. A, L1. b));Double d6=dblcmp (Xmult (L2. b, L1. A, L1. b));Double d7=dblcmp (Xmult (L1. A, L2. A, L2. b));Double d8=dblcmp (Xmult (L1. b, L2. A, L2. b));if (d1>=0&&d2>=0&&d3>=0&&d4>=0) {if (d5*d6>0|| D7*d8>0) return0;//Do not intersectelse if (d5==0&&d6==0) return1;//Common line intersectionelse if (d5==0|| d6==0|| d7==0|| d8==0) return2;//Endpoint intersectionelse return3;//Specification intersect} return0;}//slope bool Getslope (line L, double &k) {double t=l. A. x-L. b. x;if (t==0) return False;K= (l. A. Y-L. b. Y)/T;return True;}//the intersection point of Point Getintersect (lines L1, line L2) {point P;Double A1 = L1. b. Y-L1. A. Y;Double B1 = L1. A. x-L1. b. x;Double C1 = (L1. b. x-L1. A. x) * L1. A. Y-(L1. b. Y-L1. A. Y) * L1. A. x;Double A2 = L2. b. Y-L2. A. Y;Double B2 = L2. A. x-L2. b. x;Double C2 = (L2. b. x-L2. A. x) * L2. A. Y-(L2. b. Y-L2. A. Y) * L2. A. x;P. x= (C2*B1-C1*B2)/(A1*B2-A2*B1);P. Y= (C1*A2-C2*A1)/(A1*B2-A2*B1);if (p. x==-0) p. x=0;Return P;}//For a, b two pointyThe point at which the coordinates are larger dot getbiggery;if (DBLCMP (a. Y-B. Y) >0) {Q. x= A. x;Q. Y= A. Y;} else {Q. x= b. x;Q. Y= b. Y;} return Q;}double Getarea (point P, point P1, point p2) {point q;Double A;if (dblcmp (P1. Y-p2. Y) >=0) {Q. Y= P2. Y;Q. x= P. x+ (P1. x-P. x) * (P2. Y-P. Y)/(P1. Y-P. Y);//ask for the coordinates of another pointA=fabs (Xmult (p, p2, q))/2;//cross-product to calculate the area} else {Q. Y= P1. Y;Q. x= P. x+ (P2. x-P. x) * (P1. Y-P. Y)/(P2. Y-P. Y);A=fabs (Xmult (P, p1, q))/2;} return a;}int Main () {int T;Cin>>t;while (t--) {point P1, p2, p;Cin>>l1. A. x>>l1. A. Y>>l1. b. x>>l1. b. Y;Cin>>l2. A. x>>l2. A. Y>>l2. b. x>>l2. b. Y;if (judge (L1,L2) <=1)//whether intersect {ans=0; cout<<0<<endl;}else{P1=getbiggery (L1. A, L1. b);//cout << "(" <<p1.x<< "<<p1.y<<") "<<endl;P2=getbiggery (L2. A, L2. b);//cout << "(" <<p2.x<< "<<p2.y<<") "<<endl;P=getintersect (L1, L2);//cout << "(" <<p.x<< "<<p.y<<") "<<endl;if (!DBLCMP (p. x-p1. x) &AMP;&AMP;!DBLCMP (P. Y-p1. Y) || !DBLCMP (P. x-p2. x) &AMP;&AMP;!DBLCMP (P. x-p2. Y)//Opening direction//{ans=0;//cout<<1<<endl;}else{Double K1, K2;BOOL F1, F2;F1 = getslope (L1, K1);F2 = Getslope (L2, K2);if (!dblcmp (k1) | |!dblcmp (k2))//{ans =0;//cout<<2<<endl;} When a line segment is parallel to the x-axiselse if (F1 && F2) {//If both lines are notyShaft Parallel if (dblcmp (K1*K2) >0{//when the slope symbol of the two segments is the same, int d1 = dblcmp (K1-K2);int d2 = dblcmp (K2);if (d1>0&&d2>0&AMP;&AMP;DBLCMP (P2. x-p1. x) *dblcmp (P2. x-P. x) <=0|| d1<0&&d2>0&AMP;&AMP;DBLCMP (P1. x-p2. x) *dblcmp (P1. x-P. x) <=0|| D1>0&&d2<0&AMP;&AMP;DBLCMP (P1. x-p2. x) *dblcmp (P1. x-P. x) <=0|| d1<0&&d2<0&AMP;&AMP;DBLCMP (P2. x-p1. x) *dblcmp (P2. x-P. x) <=0)//Coverage condition//{ans =0;//cout<<3<<endl;}else//{Ans=getarea (P,P1,P2);//cout<<4<<endl;}} else//{Ans=getarea (P,P1,P2);//cout<<5<<endl;}} else//{Ans=getarea (P,P1,P2);//cout<<6<<endl;}}} printf ("%.2lf\n", ans);} return0;}

POJ 2826 an easy problem?! Good question

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.