diff options
Diffstat (limited to 'geometry/lines.cpp')
| -rw-r--r-- | geometry/lines.cpp | 29 |
1 files changed, 15 insertions, 14 deletions
diff --git a/geometry/lines.cpp b/geometry/lines.cpp index 2594ab4..95536a4 100644 --- a/geometry/lines.cpp +++ b/geometry/lines.cpp @@ -1,32 +1,33 @@ -// Nicht complex<double> benutzen. Eigene struct schreiben. struct line { double a, b, c; // ax + by + c = 0; vertikale Line: b = 0, sonst: b = 1 + line(pt p, pt q) : a(-imag(q-p)), b(real(q-p)), c(cross({b, -a},p)) {} }; line pointsToLine(pt p1, pt p2) { line l; - if (fabs(p1.x - p2.x) < EPSILON) { - l.a = 1; l.b = 0.0; l.c = -p1.x; + if (abs(real(p1 - p2)) < EPS) { + l.a = 1; l.b = 0.0; l.c = -real(p1); } else { - l.a = -(double)(p1.y - p2.y) / (p1.x - p2.x); + l.a = -imag(p1 - p2) / real(p1 - p2); l.b = 1.0; - l.c = -(double)(l.a * p1.x) - p1.y; + l.c = -(l.a * real(p1)) - imag(p1); } return l; } -bool areParallel(line l1, line l2) { - return (fabs(l1.a - l2.a) < EPSILON) && (fabs(l1.b - l2.b) < EPSILON); +bool parallel(line l1, line l2) { + return (abs(l1.a - l2.a) < EPS) && (abs(l1.b - l2.b) < EPS); } -bool areSame(line l1, line l2) { - return areParallel(l1, l2) && (fabs(l1.c - l2.c) < EPSILON); +bool same(line l1, line l2) { + return parallel(l1, l2) && (abs(l1.c - l2.c) < EPS); } -bool areIntersect(line l1, line l2, pt &p) { - if (areParallel(l1, l2)) return false; - p.x = (l2.b * l1.c - l1.b * l2.c) / (l2.a * l1.b - l1.a * l2.b); - if (fabs(l1.b) > EPSILON) p.y = -(l1.a * p.x + l1.c); - else p.y = -(l2.a * p.x + l2.c); +bool intersect(line l1, line l2, pt& p) { + if (parallel(l1, l2)) return false; + double y, x = (l2.b * l1.c - l1.b * l2.c) / (l2.a * l1.b - l1.a * l2.b); + if (abs(l1.b) > EPS) y = -(l1.a * x + l1.c); + else y = -(l2.a * x + l2.c); + p = {x, y}; return true; } |
