Given two straight lines, determine intersection, overlap, or find the intersection
The topic of the test template
Code:
#include <cstdio> #include <cstring> #include <cmath> #include <algorithm>using namespace std; int t;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);} Const DOUBLE EPS = 1e-8;int dcmp (double x) {if (Fabs (x) < EPS) return 0; else return x < 0? -1:1;} Double Cross (vector A, vector B) {return a.x * b.y-a.y * b.x;}/Fork product bool Linecoincide (Point P1, point P2, point p3) { Return dcmp (Cross (P2-P1, p3-p1)) = = 0;} BOOL Lineparallel (point P, Vector v, point Q, Vector w) {return dcmp (v.x * W.Y-V.Y * w.x) = = 0;} 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;} int main () {scanf ("%d", &t); printf ("Intersecting LINES output\n"); while (t--) {point P1, p2, P3, P4; P1.read (); P2.read (); P3.read (); P4.read (); if (Linecoincide (P1, p2, p3) && linecoincide (P1, P2, p4)) printf ("line\n"); else if (Lineparallel (P1, P2-p1, p3, P4-P3)) printf ("none\n"); else {point ans = getlineintersection (P1, P2-p1, p3, P4-P3); printf ("Point%.2f%.2f\n", ans.x, Ans.y); }} printf ("END of output\n"); return 0;}
POJ 1269 intersecting Lines (computational geometry)