Test instructions: Given some line segments, ask if you can find a line that crosses all segments
Idea: If there is a straight line, then there must be a straight line is over the existing two points, then enumerate two points, and then to determine whether the intersection with all the segments can be
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);}//with a direction of area//vector rotation vectors Rotate (vector A, double rad) {return Vector (a.x * cos (RAD)-A.y * sin (rad), a.x * sin (rad) + a.y * cos (RAD)); Determine 3 points collinear bool Linecoincide (point p1, dot P2, point p3) {return dcmp (cross (P2-P1, p3-p1)) = = 0;} Judgment 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); DCMP (C1) * DCMP (C2) = = 0 | | DCMP (C3) * DCMP (C4) = = 0 for non-canonical intersection return DCMP (C1) * DCMP (C2) <= 0;//&& 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;} const int N = 105;int T, n;struct Line {point A, B; void Read () {a.read (); B.read (); }} Line[n];bool judge (Point A, point B) {if (dcmp (a.x-b.x) = = 0 && dcmp (a.y-b.y) = = 0) return false; for (int i = 0; i < n; i++) if (! Segmentproperintersection (A, B, line[i].a, line[i].b)) return false; return true;} BOOL Gao () {for (int i = 0; i < n; i++) {if (Judge (LINE[I].A, line[i].b)) return true; for (int j = 0; J < i; J + +) {if (Judge (LINE[I].A, LINE[J].A)) return true; if (judge (LINE[I].A, line[j].b)) return true; if (judge (line[i].b, LINE[J].A)) return true; if (judge (line[i].b, line[j].b)) return true; }} return false;} int main () {scanf ("%d", &t); while (t--) { scanf ("%d", &n); for (int i = 0; i < n; i++) Line[i].read (); if (Gao ()) printf ("yes!\n"); else printf ("no!\n"); } return 0;}
POJ 3304 Segments (computational geometry)