POJ 2826 an easy problem?! (Computational geometry)

Source: Internet
Author: User

Basic and online solution the same idea, put several cases blablabla. It's WA.

Then add an EPS to the answer. Obviously this question did not write SPJ. The accuracy of the card

Code:

#include <cstdio> #include <cstring> #include <cmath> #include <algorithm>using namespace std;    struct point {double x, y;        Point () {}-point (Double x, double y) {this->x = x;    This->y = y;    } void Read () {scanf ("%lf%lf", &x, &y); }};typedef Point Vector; Vector operator + (vector A, vector B) {return vector (a.x + b.x, A.Y + b.y);} Vector operator-(vector A, vector B) {return vector (a.x-b.x, a.y-b.y);} Vector operator * (vector A, double p) {return vector (a.x * p, A.Y * p);} Vector operator/(vector A, double p) {return vector (a.x/p, a.y/p);} BOOL operator < (const point& A, const point& b) {return a.x < b.x | | (a.x = = b.x && a.y < B.Y);}    Const DOUBLE EPS = 1e-8;int dcmp (double x) {if (Fabs (x) < EPS) return 0; else return x < 0? -1:1;} BOOL operator = = (const point& A, const point& b) {return dcmp (a.x-b.x) = = 0 && dcmp (a.y-b.y)= = 0;} Double dot (vector A, vector B) {return a.x * b.x + a.y * B.Y;}/dot product double Length (vector a) {return sqrt (Dot (A, a));}//Vector  Modulus double Angle (vector A, vector b) {return ACOs (Dot (A, b)/Length (a)/length (b));}//Vector angle Double cross (vector a, vector b) {return a.x * b.y-a.y * b.x;}    Fork Product Double Area2 (Point A, point B, point C) {return cross (b-a, c-a);}//has an area of struct line {point V, p;        Line () {} line (point V, point p) {this->v = v;    This->p = p;    } point point (Double t) {return v + p * t; }};//vector rotation vectors Rotate (vector A, double rad) {return vector (a.x * cos (RAD)-A.y * sin (rad), a.x * sin (rad) + A.y * CO S (RAD));}    Vector Anglebisector (point p, vector v1, vector v2) {//given two vectors, quadrature line Double rad = Angle (v1, v2); Return Rotate (v1, dcmp (Cross (v1, v2)) * 0.5 * rad);} Determine 3 points collinear bool Linecoincide (point p1, dot P2, point p3) {return dcmp (cross (P2-P1, p3-p1)) = = 0;} Judging vector parallel bool Lineparallel (vector v, vector w) {return Cross (V, w) = =0;} Judgment Vector vertical bool Linevertical (vector v, vector w) {return Dot (V, w) = = 0;}    Calculate the intersection of two straight lines, parallel, coincident to first Judge Point Getlineintersection (Point P, Vector v, point Q, vector w) {vector u = p-q;    Double T = Cross (w, u)/Cross (V, W); return P + v * t;}    Point-to-line distance double distancetoline (points P, point-A, point B) {Vector V1 = b-a, v2 = p-a; Return Fabs (Cross (v1, v2))/Length (v1);}    Point-to-segment distance double distancetosegment (points P, point A, point B) {if (a = = B) return Length (P-A);    Vector v1 = b-a, v2 = p-a, V3 = p-b;    if (dcmp (Dot (v1, v2)) < 0) return Length (v2);    else if (dcmp (Dot (v1, v3)) > 0) return Length (V3); else return Fabs (cross (v1, v2))/Length (v1);}    Point on a straight line of the projection point getlineprojection (points P, A, point B) {Vector v = b-a; return A + v * (dot (v, p-a)/dot (V, v));} Segment intersection determination (canonical intersection) BOOL Segmentproperintersection (Point A1, Point A2, point B1, point B2) {Double C1 = Cross (A2-A1, B1- A1), C2 = Cross (A2-A1, b2-a1), C3 = Cross (B2- B1, a1-b1), C4 = Cross (b2-b1, A2-B1); Return DCMP (C1) * DCMP (C2) < 0 && dcmp (C3) * DCMP (C4) < 0;}  Can not standardize intersecting bool SegmentProperIntersection2 (Point A1, Point A2, point B1, point B2) {Double C1 = Cross (A2-A1, B1-A1),    C2 = Cross (A2-A1, b2-a1), C3 = Cross (B2-B1, a1-b1), C4 = Cross (b2-b1, A2-B1); Return Max (a1.x, a2.x) >= min (b1.x, b2.x) && Max (b1.x, b2.x) >= min (a1.x, a2.x) && Max (A1.y, a 2.Y) >= min (b1.y, b2.y) && Max (B1.Y, b2.y) >= min (a1.y, a2.y) && dcmp (C1) * DCMP (C2) <= 0 & amp;& dcmp (C3) * DCMP (C4) <= 0;} Judging points on a line segment, not including the endpoint bool Onsegment (point P, Dot A1, point A2) {return dcmp (cross (a1-p, a2-p)) = = 0 && dcmp (D OT (A1-p, a2-p)) < 0;}    The area of the N-side shape double Polygonarea (point *p, int n) {double areas = 0;    for (int i = 1; i < n-1; i++) area + = Cross (P[i]-p[0], P[i + 1]-p[0]); return AREA/2;} int t; Point P[4];d ouble solve () {if (LiNeparallel (P[1]-p[0], p[3]-p[2])) return 0.0; if (!    SegmentProperIntersection2 (P[0], p[1], p[2], p[3])) return 0.0;    Point x = Getlineintersection (p[0], p[1]-p[0], p[2], p[3]-p[2]); if (P[0].y > x.y && p[2].y > x.y) {if ((p[0].x-x.x) * (p[2].x-x.x) > 0) {if (dcmp (Fabs (p[0].x-x.x)-fabs (p[2].x-x.x)) >= 0) {Point sb = Getlineintersection (x, P[0]-X, p[2], Vect                or (0, 1));            if (Sb.y > P[2].y) return 0.0;        }} Point tmp = Getlineintersection (p[0], p[1]-p[0], p[2], Vector (1, 0));    Return Fabs (AREA2 (p[2], X, TMP)/2); } return 0.0;}    int main () {scanf ("%d", &t);        while (t--) {for (int i = 0; i < 4; i++) P[i].read ();        if (P[0].y < P[1].Y) swap (p[0], p[1]);        if (P[2].y < P[3].Y) swap (p[2], p[3]);            if (P[0].y < P[2].Y) {swap (p[0], p[2]);        Swap (p[1], p[3]); } prinTF ("%.2f\n", Solve () + EPS); } return 0;}


POJ 2826 an easy problem?! (Computational geometry)

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.