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" |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 8 | #include "LineUtilities.h" |
| 9 | |
| 10 | bool implicitLine(const _Line& line, double& slope, double& axisIntercept) { |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 11 | _Point delta; |
| 12 | tangent(line, delta); |
| 13 | bool moreHorizontal = fabs(delta.x) > fabs(delta.y); |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 14 | if (moreHorizontal) { |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 15 | slope = delta.y / delta.x; |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 16 | axisIntercept = line[0].y - slope * line[0].x; |
| 17 | } else { |
caryclark@google.com | c682590 | 2012-02-03 22:07:47 +0000 | [diff] [blame] | 18 | slope = delta.x / delta.y; |
caryclark@google.com | 27accef | 2012-01-25 18:57:23 +0000 | [diff] [blame] | 19 | axisIntercept = line[0].x - slope * line[0].y; |
| 20 | } |
| 21 | return moreHorizontal; |
| 22 | } |
| 23 | |
| 24 | int reduceOrder(const _Line& line, _Line& reduced) { |
| 25 | reduced[0] = line[0]; |
| 26 | int different = line[0] != line[1]; |
| 27 | reduced[1] = line[different]; |
| 28 | return 1 + different; |
| 29 | } |
caryclark@google.com | 6680fb1 | 2012-02-07 22:10:51 +0000 | [diff] [blame] | 30 | |
| 31 | void sub_divide(const _Line& line, double t1, double t2, _Line& dst) { |
| 32 | _Point delta; |
| 33 | tangent(line, delta); |
| 34 | dst[0].x = line[0].x - t1 * delta.x; |
| 35 | dst[0].y = line[0].y - t1 * delta.y; |
| 36 | dst[1].x = line[0].x - t2 * delta.x; |
| 37 | dst[1].y = line[0].y - t2 * delta.y; |
| 38 | } |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 39 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 40 | // may have this below somewhere else already: |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 41 | // copying here because I thought it was clever |
| 42 | |
| 43 | // Copyright 2001, softSurfer (www.softsurfer.com) |
| 44 | // This code may be freely used and modified for any purpose |
| 45 | // providing that this copyright notice is included with it. |
| 46 | // SoftSurfer makes no warranty for this code, and cannot be held |
| 47 | // liable for any real or imagined damage resulting from its use. |
| 48 | // Users of this code must verify correctness for their application. |
| 49 | |
| 50 | // Assume that a class is already given for the object: |
| 51 | // Point with coordinates {float x, y;} |
| 52 | //=================================================================== |
| 53 | |
| 54 | // isLeft(): tests if a point is Left|On|Right of an infinite line. |
| 55 | // Input: three points P0, P1, and P2 |
| 56 | // Return: >0 for P2 left of the line through P0 and P1 |
| 57 | // =0 for P2 on the line |
| 58 | // <0 for P2 right of the line |
| 59 | // See: the January 2001 Algorithm on Area of Triangles |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 60 | #if 0 |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 61 | float isLeft( _Point P0, _Point P1, _Point P2 ) |
| 62 | { |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 63 | return (float) ((P1.x - P0.x)*(P2.y - P0.y) - (P2.x - P0.x)*(P1.y - P0.y)); |
caryclark@google.com | fa0588f | 2012-04-26 21:01:06 +0000 | [diff] [blame] | 64 | } |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 65 | #endif |
| 66 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 67 | double t_at(const _Line& line, const _Point& pt) { |
| 68 | double dx = line[1].x - line[0].x; |
| 69 | double dy = line[1].y - line[0].y; |
| 70 | if (fabs(dx) > fabs(dy)) { |
| 71 | if (approximately_zero(dx)) { |
| 72 | return 0; |
| 73 | } |
| 74 | return (pt.x - line[0].x) / dx; |
| 75 | } |
| 76 | if (approximately_zero(dy)) { |
| 77 | return 0; |
| 78 | } |
| 79 | return (pt.y - line[0].y) / dy; |
| 80 | } |
| 81 | |
| 82 | static void setMinMax(double x, int flags, double& minX, double& maxX) { |
| 83 | if (minX > x && (flags & (kFindTopMin | kFindBottomMin))) { |
| 84 | minX = x; |
| 85 | } |
| 86 | if (maxX < x && (flags & (kFindTopMax | kFindBottomMax))) { |
| 87 | maxX = x; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | void x_at(const _Point& p1, const _Point& p2, double top, double bottom, |
| 92 | int flags, double& minX, double& maxX) { |
caryclark@google.com | 6d0032a | 2013-01-04 19:41:13 +0000 | [diff] [blame] | 93 | if (AlmostEqualUlps(p1.y, p2.y)) { |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 94 | // It should be OK to bail early in this case. There's another edge |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 95 | // which shares this end point which can intersect without failing to |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 96 | // have a slope ... maybe |
| 97 | return; |
| 98 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 99 | |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 100 | // p2.x is always greater than p1.x -- the part of points (p1, p2) are |
| 101 | // moving from the start of the cubic towards its end. |
| 102 | // if p1.y < p2.y, minX can be affected |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 103 | // if p1.y > p2.y, maxX can be affected |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 104 | double slope = (p2.x - p1.x) / (p2.y - p1.y); |
| 105 | int topFlags = flags & (kFindTopMin | kFindTopMax); |
caryclark@google.com | 9f3e9a5 | 2012-12-10 12:50:53 +0000 | [diff] [blame] | 106 | if (topFlags && ((top <= p1.y && top >= p2.y) |
| 107 | || (top >= p1.y && top <= p2.y))) { |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 108 | double x = p1.x + (top - p1.y) * slope; |
| 109 | setMinMax(x, topFlags, minX, maxX); |
| 110 | } |
| 111 | int bottomFlags = flags & (kFindBottomMin | kFindBottomMax); |
caryclark@google.com | 9f3e9a5 | 2012-12-10 12:50:53 +0000 | [diff] [blame] | 112 | if (bottomFlags && ((bottom <= p1.y && bottom >= p2.y) |
| 113 | || (bottom >= p1.y && bottom <= p2.y))) { |
caryclark@google.com | 8dcf114 | 2012-07-02 20:27:02 +0000 | [diff] [blame] | 114 | double x = p1.x + (bottom - p1.y) * slope; |
| 115 | setMinMax(x, bottomFlags, minX, maxX); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | void xy_at_t(const _Line& line, double t, double& x, double& y) { |
| 120 | double one_t = 1 - t; |
| 121 | if (&x) { |
| 122 | x = one_t * line[0].x + t * line[1].x; |
| 123 | } |
| 124 | if (&y) { |
| 125 | y = one_t * line[0].y + t * line[1].y; |
| 126 | } |
| 127 | } |