caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 1 | #include "DataTypes.h" |
| 2 | #include "LineIntersection.h" |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 3 | #include <algorithm> // used for std::swap |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 4 | |
| 5 | |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 6 | /* |
| 7 | Determine the intersection point of two line segments |
| 8 | Return FALSE if the lines don't intersect |
| 9 | from: http://paulbourke.net/geometry/lineline2d/ |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 10 | */ |
| 11 | |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 12 | int intersect(const _Line& a, const _Line& b, double aRange[2], double bRange[2]) { |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 13 | double axLen = a[1].x - a[0].x; |
| 14 | double ayLen = a[1].y - a[0].y; |
| 15 | double bxLen = b[1].x - b[0].x; |
| 16 | double byLen = b[1].y - b[0].y; |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 17 | /* Slopes match when denom goes to zero: |
| 18 | axLen / ayLen == bxLen / byLen |
| 19 | (ayLen * byLen) * axLen / ayLen == (ayLen * byLen) * bxLen / byLen |
| 20 | byLen * axLen == ayLen * bxLen |
| 21 | byLen * axLen - ayLen * bxLen == 0 ( == denom ) |
| 22 | */ |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 23 | double denom = byLen * axLen - ayLen * bxLen; |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 24 | if (approximately_zero_squared(denom)) { |
| 25 | /* See if the axis intercepts match: |
| 26 | ay - ax * ayLen / axLen == by - bx * ayLen / axLen |
| 27 | axLen * (ay - ax * ayLen / axLen) == axLen * (by - bx * ayLen / axLen) |
| 28 | axLen * ay - ax * ayLen == axLen * by - bx * ayLen |
| 29 | */ |
| 30 | if (approximately_equal_squared(axLen * a[0].y - ayLen * a[0].x, |
| 31 | axLen * b[0].y - ayLen * b[0].x)) { |
| 32 | const double* aPtr; |
| 33 | const double* bPtr; |
| 34 | if (fabs(axLen) > fabs(ayLen) || fabs(bxLen) > fabs(byLen)) { |
| 35 | aPtr = &a[0].x; |
| 36 | bPtr = &b[0].x; |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 37 | } else { |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 38 | aPtr = &a[0].y; |
| 39 | bPtr = &b[0].y; |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 40 | } |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 41 | double aMin = aPtr[0]; |
| 42 | double aMax = aPtr[2]; |
| 43 | double bMin = bPtr[0]; |
| 44 | double bMax = bPtr[2]; |
| 45 | if (aMin > aMax) { |
| 46 | std::swap(aMin, aMax); |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 47 | } |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 48 | if (bMin > bMax) { |
| 49 | std::swap(bMin, bMax); |
| 50 | } |
| 51 | if (aMax < bMin || bMax < aMin) { |
| 52 | return 0; |
| 53 | } |
| 54 | if (aRange) { |
| 55 | aRange[0] = bMin <= aMin ? 0 : (bMin - aMin) / (aMax - aMin); |
| 56 | aRange[1] = bMax >= aMax ? 1 : (bMax - aMin) / (aMax - aMin); |
| 57 | } |
| 58 | if (bRange) { |
| 59 | bRange[0] = aMin <= bMin ? 0 : (aMin - bMin) / (bMax - bMin); |
| 60 | bRange[1] = aMax >= bMax ? 1 : (aMax - bMin) / (bMax - bMin); |
| 61 | } |
| 62 | return 2; |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 63 | } |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 64 | } |
| 65 | double ab0y = a[0].y - b[0].y; |
| 66 | double ab0x = a[0].x - b[0].x; |
| 67 | double numerA = ab0y * bxLen - byLen * ab0x; |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 68 | if (numerA < 0 && denom > numerA || numerA > 0 && denom < numerA) { |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 69 | return 0; |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 70 | } |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 71 | double numerB = ab0y * axLen - ayLen * ab0x; |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 72 | if (numerB < 0 && denom > numerB || numerB > 0 && denom < numerB) { |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 73 | return 0; |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 74 | } |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 75 | /* Is the intersection along the the segments */ |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 76 | if (aRange) { |
| 77 | aRange[0] = numerA / denom; |
| 78 | } |
| 79 | if (bRange) { |
| 80 | bRange[0] = numerB / denom; |
| 81 | } |
| 82 | return 1; |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 83 | } |
| 84 | |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 85 | int horizontalIntersect(const _Line& line, double y, double tRange[2]) { |
| 86 | double min = line[0].y; |
| 87 | double max = line[1].y; |
| 88 | if (min > max) { |
| 89 | std::swap(min, max); |
| 90 | } |
| 91 | if (min > y || max < y) { |
| 92 | return 0; |
| 93 | } |
| 94 | if (approximately_equal(min, max)) { |
| 95 | tRange[0] = 0; |
| 96 | tRange[1] = 1; |
| 97 | return 2; |
| 98 | } |
| 99 | tRange[0] = (y - line[0].y) / (line[1].y - line[0].y); |
| 100 | return 1; |
| 101 | } |
caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame] | 102 | |
| 103 | // from http://www.bryceboe.com/wordpress/wp-content/uploads/2006/10/intersect.py |
| 104 | // 4 subs, 2 muls, 1 cmp |
| 105 | static bool ccw(const _Point& A, const _Point& B, const _Point& C) { |
| 106 | return (C.y - A.y) * (B.x - A.x) > (B.y - A.y) * (C.x - A.x); |
| 107 | } |
| 108 | |
| 109 | // 16 subs, 8 muls, 6 cmps |
| 110 | bool testIntersect(const _Line& a, const _Line& b) { |
| 111 | return ccw(a[0], b[0], b[1]) != ccw(a[1], b[0], b[1]) |
| 112 | && ccw(a[0], a[1], b[0]) != ccw(a[0], a[1], b[1]); |
| 113 | } |