caryclark@google.com | 9e49fb6 | 2012-08-27 14:11:33 +0000 | [diff] [blame] | 1 | /* |
| 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.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 7 | #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 | */ |
| 13 | bool implicit_matches(const _Line& one, const _Line& two) { |
| 14 | _Point oneD, twoD; |
| 15 | tangent(one, oneD); |
| 16 | tangent(two, twoD); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 17 | /* See if the slopes match, i.e. |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 18 | dx1 / dy1 == dx2 / dy2 |
| 19 | (dy1 * dy2) * dx1 / dy1 == (dy1 * dy2) * dx2 / dy2 |
| 20 | dy2 * dx1 == dy1 * dx2 |
| 21 | */ |
caryclark@google.com | 6d0032a | 2013-01-04 19:41:13 +0000 | [diff] [blame^] | 22 | if (!AlmostEqualUlps(oneD.x * twoD.y, twoD.x * oneD.y)) { |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 23 | 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.com | 6d0032a | 2013-01-04 19:41:13 +0000 | [diff] [blame^] | 30 | if (!AlmostEqualUlps(oneD.x * one[0].y - oneD.y * one[0].x, |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 31 | oneD.x * two[0].y - oneD.y * two[0].x)) { |
| 32 | return false; |
| 33 | } |
| 34 | return true; |
| 35 | } |
| 36 | |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 37 | bool 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.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 41 | /* See if the slopes match, i.e. |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 42 | dx1 / dy1 == dx2 / dy2 |
| 43 | (dy1 * dy2) * dx1 / dy1 == (dy1 * dy2) * dx2 / dy2 |
| 44 | dy2 * dx1 == dy1 * dx2 |
| 45 | */ |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 46 | int diff = UlpsDiff((float) (oneD.x * twoD.y), (float) (twoD.x * oneD.y)); |
caryclark@google.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 47 | 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.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 55 | 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.com | 78e1713 | 2012-04-17 11:40:34 +0000 | [diff] [blame] | 57 | return diff >= 0 && diff <= ulps; |
| 58 | } |
| 59 | |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 60 | void 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 | } |