blob: b1e1783e575ca9e8ebe389964d1298664a455462 [file] [log] [blame]
caryclark@google.com07393ca2013-04-08 11:47:37 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7#include "SkIntersections.h"
8#include "SkPathOpsLine.h"
9
10/* Determine the intersection point of two lines. This assumes the lines are not parallel,
11 and that that the lines are infinite.
12 From http://en.wikipedia.org/wiki/Line-line_intersection
13 */
14SkDPoint SkIntersections::Line(const SkDLine& a, const SkDLine& b) {
15 double axLen = a[1].fX - a[0].fX;
16 double ayLen = a[1].fY - a[0].fY;
17 double bxLen = b[1].fX - b[0].fX;
18 double byLen = b[1].fY - b[0].fY;
19 double denom = byLen * axLen - ayLen * bxLen;
20 SkASSERT(denom);
21 double term1 = a[1].fX * a[0].fY - a[1].fY * a[0].fX;
22 double term2 = b[1].fX * b[0].fY - b[1].fY * b[0].fX;
23 SkDPoint p;
24 p.fX = (term1 * bxLen - axLen * term2) / denom;
25 p.fY = (term1 * byLen - ayLen * term2) / denom;
26 return p;
27}
28
29int SkIntersections::computePoints(const SkDLine& line, int used) {
30 fPt[0] = line.xyAtT(fT[0][0]);
31 if ((fUsed = used) == 2) {
32 fPt[1] = line.xyAtT(fT[0][1]);
33 }
34 return fUsed;
35}
36
37/*
38 Determine the intersection point of two line segments
39 Return FALSE if the lines don't intersect
40 from: http://paulbourke.net/geometry/lineline2d/
41 */
42
43int SkIntersections::intersect(const SkDLine& a, const SkDLine& b) {
44 double axLen = a[1].fX - a[0].fX;
45 double ayLen = a[1].fY - a[0].fY;
46 double bxLen = b[1].fX - b[0].fX;
47 double byLen = b[1].fY - b[0].fY;
48 /* Slopes match when denom goes to zero:
49 axLen / ayLen == bxLen / byLen
50 (ayLen * byLen) * axLen / ayLen == (ayLen * byLen) * bxLen / byLen
51 byLen * axLen == ayLen * bxLen
52 byLen * axLen - ayLen * bxLen == 0 ( == denom )
53 */
54 double denom = byLen * axLen - ayLen * bxLen;
55 double ab0y = a[0].fY - b[0].fY;
56 double ab0x = a[0].fX - b[0].fX;
57 double numerA = ab0y * bxLen - byLen * ab0x;
58 double numerB = ab0y * axLen - ayLen * ab0x;
59 bool mayNotOverlap = (numerA < 0 && denom > numerA) || (numerA > 0 && denom < numerA)
60 || (numerB < 0 && denom > numerB) || (numerB > 0 && denom < numerB);
61 numerA /= denom;
62 numerB /= denom;
63 if ((!approximately_zero(denom) || (!approximately_zero_inverse(numerA)
64 && !approximately_zero_inverse(numerB))) && !sk_double_isnan(numerA)
65 && !sk_double_isnan(numerB)) {
66 if (mayNotOverlap) {
67 return fUsed = 0;
68 }
69 fT[0][0] = numerA;
70 fT[1][0] = numerB;
71 fPt[0] = a.xyAtT(numerA);
72 return computePoints(a, 1);
73 }
74 /* See if the axis intercepts match:
75 ay - ax * ayLen / axLen == by - bx * ayLen / axLen
76 axLen * (ay - ax * ayLen / axLen) == axLen * (by - bx * ayLen / axLen)
77 axLen * ay - ax * ayLen == axLen * by - bx * ayLen
78 */
caryclark@google.comc3f63572013-04-23 12:04:05 +000079 if (!AlmostEqualUlps(axLen * a[0].fY - ayLen * a[0].fX,
caryclark@google.com07393ca2013-04-08 11:47:37 +000080 axLen * b[0].fY - ayLen * b[0].fX)) {
81 return fUsed = 0;
82 }
83 const double* aPtr;
84 const double* bPtr;
85 if (fabs(axLen) > fabs(ayLen) || fabs(bxLen) > fabs(byLen)) {
86 aPtr = &a[0].fX;
87 bPtr = &b[0].fX;
88 } else {
89 aPtr = &a[0].fY;
90 bPtr = &b[0].fY;
91 }
92 double a0 = aPtr[0];
93 double a1 = aPtr[2];
94 double b0 = bPtr[0];
95 double b1 = bPtr[2];
96 // OPTIMIZATION: restructure to reject before the divide
97 // e.g., if ((a0 - b0) * (a0 - a1) < 0 || abs(a0 - b0) > abs(a0 - a1))
98 // (except efficient)
99 double aDenom = a0 - a1;
100 if (approximately_zero(aDenom)) {
101 if (!between(b0, a0, b1)) {
102 return fUsed = 0;
103 }
104 fT[0][0] = fT[0][1] = 0;
105 } else {
106 double at0 = (a0 - b0) / aDenom;
107 double at1 = (a0 - b1) / aDenom;
108 if ((at0 < 0 && at1 < 0) || (at0 > 1 && at1 > 1)) {
109 return fUsed = 0;
110 }
caryclark@google.com3b97af52013-04-23 11:56:44 +0000111 fT[0][0] = SkTMax(SkTMin(at0, 1.0), 0.0);
112 fT[0][1] = SkTMax(SkTMin(at1, 1.0), 0.0);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000113 }
114 double bDenom = b0 - b1;
115 if (approximately_zero(bDenom)) {
116 fT[1][0] = fT[1][1] = 0;
117 } else {
118 int bIn = aDenom * bDenom < 0;
caryclark@google.com3b97af52013-04-23 11:56:44 +0000119 fT[1][bIn] = SkTMax(SkTMin((b0 - a0) / bDenom, 1.0), 0.0);
120 fT[1][!bIn] = SkTMax(SkTMin((b0 - a1) / bDenom, 1.0), 0.0);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000121 }
122 bool second = fabs(fT[0][0] - fT[0][1]) > FLT_EPSILON;
123 SkASSERT((fabs(fT[1][0] - fT[1][1]) <= FLT_EPSILON) ^ second);
124 return computePoints(a, 1 + second);
125}
126
127int SkIntersections::horizontal(const SkDLine& line, double y) {
128 double min = line[0].fY;
129 double max = line[1].fY;
130 if (min > max) {
131 SkTSwap(min, max);
132 }
133 if (min > y || max < y) {
134 return fUsed = 0;
135 }
136 if (AlmostEqualUlps(min, max)) {
137 fT[0][0] = 0;
138 fT[0][1] = 1;
139 return fUsed = 2;
140 }
141 fT[0][0] = (y - line[0].fY) / (line[1].fY - line[0].fY);
142 return fUsed = 1;
143}
144
caryclark@google.com07393ca2013-04-08 11:47:37 +0000145int SkIntersections::horizontal(const SkDLine& line, double left, double right,
146 double y, bool flipped) {
147 int result = horizontal(line, y);
148 switch (result) {
149 case 0:
150 break;
151 case 1: {
152 double xIntercept = line[0].fX + fT[0][0] * (line[1].fX - line[0].fX);
caryclark@google.com03610322013-04-18 15:58:21 +0000153 if (!precisely_between(left, xIntercept, right)) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000154 return fUsed = 0;
155 }
156 fT[1][0] = (xIntercept - left) / (right - left);
157 break;
158 }
159 case 2:
160 double a0 = line[0].fX;
161 double a1 = line[1].fX;
162 double b0 = flipped ? right : left;
163 double b1 = flipped ? left : right;
164 // FIXME: share common code below
165 double at0 = (a0 - b0) / (a0 - a1);
166 double at1 = (a0 - b1) / (a0 - a1);
167 if ((at0 < 0 && at1 < 0) || (at0 > 1 && at1 > 1)) {
168 return fUsed = 0;
169 }
caryclark@google.com3b97af52013-04-23 11:56:44 +0000170 fT[0][0] = SkTMax(SkTMin(at0, 1.0), 0.0);
171 fT[0][1] = SkTMax(SkTMin(at1, 1.0), 0.0);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000172 int bIn = (a0 - a1) * (b0 - b1) < 0;
caryclark@google.com3b97af52013-04-23 11:56:44 +0000173 fT[1][bIn] = SkTMax(SkTMin((b0 - a0) / (b0 - b1), 1.0), 0.0);
174 fT[1][!bIn] = SkTMax(SkTMin((b0 - a1) / (b0 - b1), 1.0), 0.0);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000175 bool second = fabs(fT[0][0] - fT[0][1]) > FLT_EPSILON;
176 SkASSERT((fabs(fT[1][0] - fT[1][1]) <= FLT_EPSILON) ^ second);
177 return computePoints(line, 1 + second);
178 }
179 if (flipped) {
180 // OPTIMIZATION: instead of swapping, pass original line, use [1].fX - [0].fX
181 for (int index = 0; index < result; ++index) {
182 fT[1][index] = 1 - fT[1][index];
183 }
184 }
185 return computePoints(line, result);
186}
187
188int SkIntersections::vertical(const SkDLine& line, double x) {
189 double min = line[0].fX;
190 double max = line[1].fX;
191 if (min > max) {
192 SkTSwap(min, max);
193 }
caryclark@google.com03610322013-04-18 15:58:21 +0000194 if (!precisely_between(min, x, max)) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000195 return fUsed = 0;
196 }
197 if (AlmostEqualUlps(min, max)) {
198 fT[0][0] = 0;
199 fT[0][1] = 1;
200 return fUsed = 2;
201 }
202 fT[0][0] = (x - line[0].fX) / (line[1].fX - line[0].fX);
203 return fUsed = 1;
204}
205
206int SkIntersections::vertical(const SkDLine& line, double top, double bottom,
207 double x, bool flipped) {
208 int result = vertical(line, x);
209 switch (result) {
210 case 0:
211 break;
212 case 1: {
213 double yIntercept = line[0].fY + fT[0][0] * (line[1].fY - line[0].fY);
caryclark@google.com03610322013-04-18 15:58:21 +0000214 if (!precisely_between(top, yIntercept, bottom)) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000215 return fUsed = 0;
216 }
217 fT[1][0] = (yIntercept - top) / (bottom - top);
218 break;
219 }
220 case 2:
221 double a0 = line[0].fY;
222 double a1 = line[1].fY;
223 double b0 = flipped ? bottom : top;
224 double b1 = flipped ? top : bottom;
225 // FIXME: share common code above
226 double at0 = (a0 - b0) / (a0 - a1);
227 double at1 = (a0 - b1) / (a0 - a1);
228 if ((at0 < 0 && at1 < 0) || (at0 > 1 && at1 > 1)) {
229 return fUsed = 0;
230 }
caryclark@google.com3b97af52013-04-23 11:56:44 +0000231 fT[0][0] = SkTMax(SkTMin(at0, 1.0), 0.0);
232 fT[0][1] = SkTMax(SkTMin(at1, 1.0), 0.0);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000233 int bIn = (a0 - a1) * (b0 - b1) < 0;
caryclark@google.com3b97af52013-04-23 11:56:44 +0000234 fT[1][bIn] = SkTMax(SkTMin((b0 - a0) / (b0 - b1), 1.0), 0.0);
235 fT[1][!bIn] = SkTMax(SkTMin((b0 - a1) / (b0 - b1), 1.0), 0.0);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000236 bool second = fabs(fT[0][0] - fT[0][1]) > FLT_EPSILON;
237 SkASSERT((fabs(fT[1][0] - fT[1][1]) <= FLT_EPSILON) ^ second);
238 return computePoints(line, 1 + second);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000239 }
240 if (flipped) {
241 // OPTIMIZATION: instead of swapping, pass original line, use [1].fY - [0].fY
242 for (int index = 0; index < result; ++index) {
243 fT[1][index] = 1 - fT[1][index];
244 }
245 }
246 return computePoints(line, result);
247}
248
249// from http://www.bryceboe.com/wordpress/wp-content/uploads/2006/10/intersect.py
250// 4 subs, 2 muls, 1 cmp
251static bool ccw(const SkDPoint& A, const SkDPoint& B, const SkDPoint& C) {
252 return (C.fY - A.fY) * (B.fX - A.fX) > (B.fY - A.fY) * (C.fX - A.fX);
253}
254
255// 16 subs, 8 muls, 6 cmps
256bool SkIntersections::Test(const SkDLine& a, const SkDLine& b) {
257 return ccw(a[0], b[0], b[1]) != ccw(a[1], b[0], b[1])
258 && ccw(a[0], a[1], b[0]) != ccw(a[0], a[1], b[1]);
259}