diff options
| author | pjungeblut <paul.jungeblut@gmail.com> | 2014-11-18 17:41:52 +0100 |
|---|---|---|
| committer | pjungeblut <paul.jungeblut@gmail.com> | 2014-11-18 17:41:52 +0100 |
| commit | 1d7b4ee73eae28b9c0c2d6f865e5bf9470d2ad60 (patch) | |
| tree | cd27e9f04613c39a08d874e5b7fa6dbbcfa2e2f2 /geometry/lines.cpp | |
| parent | 8ceb0e35278563a3b315f04fc60fc9086d6c7a8a (diff) | |
lines
Diffstat (limited to 'geometry/lines.cpp')
| -rw-r--r-- | geometry/lines.cpp | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/geometry/lines.cpp b/geometry/lines.cpp new file mode 100644 index 0000000..ec6fdde --- /dev/null +++ b/geometry/lines.cpp @@ -0,0 +1,37 @@ +struct pt { //complex<double> does not work here, becuase we need to set pt.x and pt.y + double x, y; + pt() {}; + pt(double x, double y) : x(x), y(y) {}; +}; + +struct line { + double a, b, c; //a*x+b*y+c, b=0 <=> vertical line, b=1 <=> otherwise +}; + +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; + } else { + l.a = -(double)(p1.y - p2.y) / (p1.x - p2.x); + l.b = 1.0; + l.c = -(double)(l.a * p1.x) - p1.y; + } + return l; +} + +bool areParallel(line l1, line l2) { + return (fabs(l1.a - l2.a) < EPSILON) && (fabs(l1.b - l2.b) < EPSILON); +} + +bool areSame(line l1, line l2) { + return areParallel(l1, l2) && (fabs(l1.c - l2.c) < EPSILON); +} + +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); + return true; +}
\ No newline at end of file |
