blob: 99b9e827881657cfe1ee1873a8eaec9617f9d787 [file] [log] [blame]
caryclark@google.com639df892012-01-10 21:46:10 +00001#include "DataTypes.h"
2#include "LineIntersection.h"
caryclark@google.comc6825902012-02-03 22:07:47 +00003#include <algorithm> // used for std::swap
caryclark@google.com639df892012-01-10 21:46:10 +00004
5
caryclark@google.com639df892012-01-10 21:46:10 +00006/*
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.com639df892012-01-10 21:46:10 +000010*/
11
caryclark@google.comc6825902012-02-03 22:07:47 +000012int intersect(const _Line& a, const _Line& b, double aRange[2], double bRange[2]) {
caryclark@google.com639df892012-01-10 21:46:10 +000013 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.comc6825902012-02-03 22:07:47 +000017 /* 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.com639df892012-01-10 21:46:10 +000023 double denom = byLen * axLen - ayLen * bxLen;
caryclark@google.comc6825902012-02-03 22:07:47 +000024 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.com639df892012-01-10 21:46:10 +000037 } else {
caryclark@google.comc6825902012-02-03 22:07:47 +000038 aPtr = &a[0].y;
39 bPtr = &b[0].y;
caryclark@google.com639df892012-01-10 21:46:10 +000040 }
caryclark@google.comc6825902012-02-03 22:07:47 +000041 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.com639df892012-01-10 21:46:10 +000047 }
caryclark@google.comc6825902012-02-03 22:07:47 +000048 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.com639df892012-01-10 21:46:10 +000063 }
caryclark@google.com639df892012-01-10 21:46:10 +000064 }
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.com639df892012-01-10 21:46:10 +000068 if (numerA < 0 && denom > numerA || numerA > 0 && denom < numerA) {
caryclark@google.comc6825902012-02-03 22:07:47 +000069 return 0;
caryclark@google.com639df892012-01-10 21:46:10 +000070 }
caryclark@google.comc6825902012-02-03 22:07:47 +000071 double numerB = ab0y * axLen - ayLen * ab0x;
caryclark@google.com639df892012-01-10 21:46:10 +000072 if (numerB < 0 && denom > numerB || numerB > 0 && denom < numerB) {
caryclark@google.comc6825902012-02-03 22:07:47 +000073 return 0;
caryclark@google.com639df892012-01-10 21:46:10 +000074 }
caryclark@google.com639df892012-01-10 21:46:10 +000075 /* Is the intersection along the the segments */
caryclark@google.comc6825902012-02-03 22:07:47 +000076 if (aRange) {
77 aRange[0] = numerA / denom;
78 }
79 if (bRange) {
80 bRange[0] = numerB / denom;
81 }
82 return 1;
caryclark@google.com639df892012-01-10 21:46:10 +000083}
84
caryclark@google.comc6825902012-02-03 22:07:47 +000085int 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.com639df892012-01-10 21:46:10 +0000102
103// from http://www.bryceboe.com/wordpress/wp-content/uploads/2006/10/intersect.py
104// 4 subs, 2 muls, 1 cmp
105static 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
110bool 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}