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