blob: 8bf8acd8ff88dfdf1f291c9f5acee2d788485091 [file] [log] [blame]
caryclark@google.comc6825902012-02-03 22:07:47 +00001#include "CurveIntersection.h"
2
3/* This rejects coincidence with two muls, two adds, and one cmp.
4 Coincident candidates will take another four muls and two adds, but may still
5 fail if they don't overlap. (The overlap test isn't performed here.)
6 */
7bool implicit_matches(const _Line& one, const _Line& two) {
8 _Point oneD, twoD;
9 tangent(one, oneD);
10 tangent(two, twoD);
11 /* See if the slopes match, i.e.
12 dx1 / dy1 == dx2 / dy2
13 (dy1 * dy2) * dx1 / dy1 == (dy1 * dy2) * dx2 / dy2
14 dy2 * dx1 == dy1 * dx2
15 */
16 if (!approximately_equal(oneD.x * twoD.y, twoD.x * oneD.y)) {
17 return false;
18 }
19 /* See if the axis intercepts match, i.e.
20 y0 - x0 * dy / dx == y1 - x1 * dy / dx
21 dx * (y0 - x0 * dy / dx) == dx * (y1 - x1 * dy / dx)
22 dx * y0 - x0 * dy == dx * y1 - x1 * dy
23 */
24 if (!approximately_equal(oneD.x * one[0].y - oneD.y * one[0].x,
25 oneD.x * two[0].y - oneD.y * two[0].x)) {
26 return false;
27 }
28 return true;
29}
30
caryclark@google.com78e17132012-04-17 11:40:34 +000031bool implicit_matches_ulps(const _Line& one, const _Line& two, int ulps) {
32 _Point oneD, twoD;
33 tangent(one, oneD);
34 tangent(two, twoD);
35 /* See if the slopes match, i.e.
36 dx1 / dy1 == dx2 / dy2
37 (dy1 * dy2) * dx1 / dy1 == (dy1 * dy2) * dx2 / dy2
38 dy2 * dx1 == dy1 * dx2
39 */
caryclark@google.com1577e8f2012-05-22 17:01:14 +000040 int diff = UlpsDiff((float) (oneD.x * twoD.y), (float) (twoD.x * oneD.y));
caryclark@google.com78e17132012-04-17 11:40:34 +000041 if (diff < 0 || diff > ulps) {
42 return false;
43 }
44 /* See if the axis intercepts match, i.e.
45 y0 - x0 * dy / dx == y1 - x1 * dy / dx
46 dx * (y0 - x0 * dy / dx) == dx * (y1 - x1 * dy / dx)
47 dx * y0 - x0 * dy == dx * y1 - x1 * dy
48 */
caryclark@google.com1577e8f2012-05-22 17:01:14 +000049 diff = UlpsDiff((float) (oneD.x * one[0].y - oneD.y * one[0].x),
50 (float) (oneD.x * two[0].y - oneD.y * two[0].x));
caryclark@google.com78e17132012-04-17 11:40:34 +000051 return diff >= 0 && diff <= ulps;
52}
53
caryclark@google.comc6825902012-02-03 22:07:47 +000054void tangent(const _Line& line, _Point& result) {
55 result.x = line[0].x - line[1].x;
56 result.y = line[0].y - line[1].y;
57}