blob: 197394530bcf455c51b94e6a39688851bf313062 [file] [log] [blame]
caryclark@google.com9e49fb62012-08-27 14:11:33 +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 */
caryclark@google.comc6825902012-02-03 22:07:47 +00007#include "CurveIntersection.h"
8
9/* This rejects coincidence with two muls, two adds, and one cmp.
10 Coincident candidates will take another four muls and two adds, but may still
11 fail if they don't overlap. (The overlap test isn't performed here.)
12 */
13bool implicit_matches(const _Line& one, const _Line& two) {
14 _Point oneD, twoD;
15 tangent(one, oneD);
16 tangent(two, twoD);
rmistry@google.comd6176b02012-08-23 18:14:13 +000017 /* See if the slopes match, i.e.
caryclark@google.comc6825902012-02-03 22:07:47 +000018 dx1 / dy1 == dx2 / dy2
19 (dy1 * dy2) * dx1 / dy1 == (dy1 * dy2) * dx2 / dy2
20 dy2 * dx1 == dy1 * dx2
21 */
caryclark@google.com6d0032a2013-01-04 19:41:13 +000022 if (!AlmostEqualUlps(oneD.x * twoD.y, twoD.x * oneD.y)) {
caryclark@google.comc6825902012-02-03 22:07:47 +000023 return false;
24 }
25 /* See if the axis intercepts match, i.e.
26 y0 - x0 * dy / dx == y1 - x1 * dy / dx
27 dx * (y0 - x0 * dy / dx) == dx * (y1 - x1 * dy / dx)
28 dx * y0 - x0 * dy == dx * y1 - x1 * dy
29 */
caryclark@google.com6d0032a2013-01-04 19:41:13 +000030 if (!AlmostEqualUlps(oneD.x * one[0].y - oneD.y * one[0].x,
caryclark@google.comc6825902012-02-03 22:07:47 +000031 oneD.x * two[0].y - oneD.y * two[0].x)) {
32 return false;
33 }
34 return true;
35}
36
caryclark@google.com78e17132012-04-17 11:40:34 +000037bool implicit_matches_ulps(const _Line& one, const _Line& two, int ulps) {
38 _Point oneD, twoD;
39 tangent(one, oneD);
40 tangent(two, twoD);
rmistry@google.comd6176b02012-08-23 18:14:13 +000041 /* See if the slopes match, i.e.
caryclark@google.com78e17132012-04-17 11:40:34 +000042 dx1 / dy1 == dx2 / dy2
43 (dy1 * dy2) * dx1 / dy1 == (dy1 * dy2) * dx2 / dy2
44 dy2 * dx1 == dy1 * dx2
45 */
caryclark@google.com1577e8f2012-05-22 17:01:14 +000046 int diff = UlpsDiff((float) (oneD.x * twoD.y), (float) (twoD.x * oneD.y));
caryclark@google.com78e17132012-04-17 11:40:34 +000047 if (diff < 0 || diff > ulps) {
48 return false;
49 }
50 /* See if the axis intercepts match, i.e.
51 y0 - x0 * dy / dx == y1 - x1 * dy / dx
52 dx * (y0 - x0 * dy / dx) == dx * (y1 - x1 * dy / dx)
53 dx * y0 - x0 * dy == dx * y1 - x1 * dy
54 */
caryclark@google.com1577e8f2012-05-22 17:01:14 +000055 diff = UlpsDiff((float) (oneD.x * one[0].y - oneD.y * one[0].x),
56 (float) (oneD.x * two[0].y - oneD.y * two[0].x));
caryclark@google.com78e17132012-04-17 11:40:34 +000057 return diff >= 0 && diff <= ulps;
58}
59
caryclark@google.comc6825902012-02-03 22:07:47 +000060void tangent(const _Line& line, _Point& result) {
61 result.x = line[0].x - line[1].x;
62 result.y = line[0].y - line[1].y;
63}