// UVa 378 - Intersecting Lines #include <iostream> #include <cmath> #include <stdio.h> using namespace std; struct point { double x, y; }; struct line { double a, b, c; }; line points_to_line(point p, point q) { line l; l.a = p.y - q.y; l.b = q.x - p.x; l.c = -l.a * p.x - l.b * p.y; return l; } bool are_parallel(line l1, line l2) { return l1.b * l2.a == l2.b * l1.a; } point intersection(line l1, line l2) { point p; p.y = (l2.c * l1.a - l1.c * l2.a) / (l1.b * l2.a - l2.b * l1.a); p.x = (l2.c * l1.b - l1.c * l2.b) / (l1.a * l2.b - l2.a * l1.b); return p; } bool are_same(line l1, line l2) { if ((l1.b == 0) ^ (l2.b == 0)) return false; else if (l1.b == 0 && l2.b == 0) return l1.c / l1.a == l2.c / l2.a; else return l1.a / l1.b == l2.a / l2.b && l1.c / l1.a == l2.c / l2.a; } int main() { cout << "INTERSECTING LINES OUTPUT\n"; int n; for (cin >> n; n; n--) { point p1, p2, p3, p4; cin >> p1.x >> p1.y >> p2.x >> p2.y >> p3.x >> p3.y >> p4.x >> p4.y; line l1 = points_to_line(p1, p2); line l2 = points_to_line(p3, p4); if (are_same(l1, l2)) cout << "LINE" << endl; else if (are_parallel(l1, l2)) { cout << "NONE" << endl; } else { point inter = intersection(l1, l2); printf("POINT %.2f %.2f\n", inter.x, inter.y); } } cout << "END OF OUTPUT\n"; return 0; }
Sunday, June 7, 2015
UVa 378 - Intersecting Lines
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment