caryclark@google.com | 639df89 | 2012-01-10 21:46:10 +0000 | [diff] [blame^] | 1 | #include "DataTypes.h" |
| 2 | |
| 3 | // Sources |
| 4 | // computer-aided design - volume 22 number 9 november 1990 pp 538 - 549 |
| 5 | // online at http://cagd.cs.byu.edu/~tom/papers/bezclip.pdf |
| 6 | |
| 7 | class LineParameters { |
| 8 | public: |
| 9 | void cubicEndPoints(const Cubic& pts) { |
| 10 | a = pts[0].y - pts[3].y; |
| 11 | b = pts[3].x - pts[0].x; |
| 12 | c = pts[0].x * pts[3].y - pts[3].x * pts[0].y; |
| 13 | } |
| 14 | |
| 15 | void cubicEndPoints(const Cubic& pts, int s, int e) { |
| 16 | a = pts[s].y - pts[e].y; |
| 17 | b = pts[e].x - pts[s].x; |
| 18 | c = pts[s].x * pts[e].y - pts[e].x * pts[s].y; |
| 19 | } |
| 20 | |
| 21 | void lineEndPoints(const _Line& pts) { |
| 22 | a = pts[0].y - pts[1].y; |
| 23 | b = pts[1].x - pts[0].x; |
| 24 | c = pts[0].x * pts[1].y - pts[1].x * pts[0].y; |
| 25 | } |
| 26 | |
| 27 | void quadEndPoints(const Quadratic& pts) { |
| 28 | a = pts[0].y - pts[2].y; |
| 29 | b = pts[2].x - pts[0].x; |
| 30 | c = pts[0].x * pts[2].y - pts[2].x * pts[0].y; |
| 31 | } |
| 32 | |
| 33 | void quadEndPoints(const Quadratic& pts, int s, int e) { |
| 34 | a = pts[s].y - pts[e].y; |
| 35 | b = pts[e].x - pts[s].x; |
| 36 | c = pts[s].x * pts[e].y - pts[e].x * pts[s].y; |
| 37 | } |
| 38 | |
| 39 | double normalSquared() { |
| 40 | return a * a + b * b; |
| 41 | } |
| 42 | |
| 43 | bool normalize() { |
| 44 | double normal = sqrt(normalSquared()); |
| 45 | if (normal < SquaredEpsilon) { |
| 46 | a = b = c = 0; |
| 47 | return false; |
| 48 | } |
| 49 | double reciprocal = 1 / normal; |
| 50 | a *= reciprocal; |
| 51 | b *= reciprocal; |
| 52 | c *= reciprocal; |
| 53 | return true; |
| 54 | } |
| 55 | |
| 56 | void cubicDistanceY(const Cubic& pts, Cubic& distance) { |
| 57 | double oneThird = 1 / 3.0; |
| 58 | for (int index = 0; index < 4; ++index) { |
| 59 | distance[index].x = index * oneThird; |
| 60 | distance[index].y = a * pts[index].x + b * pts[index].y + c; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | void quadDistanceY(const Quadratic& pts, Quadratic& distance) { |
| 65 | double oneHalf = 1 / 2.0; |
| 66 | for (int index = 0; index < 3; ++index) { |
| 67 | distance[index].x = index * oneHalf; |
| 68 | distance[index].y = a * pts[index].x + b * pts[index].y + c; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | void controlPtDistance(const Cubic& pts, double distance[2]) { |
| 73 | for (int index = 0; index < 2; ++index) { |
| 74 | distance[index] = a * pts[index + 1].x + b * pts[index + 1].y + c; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | void controlPtDistance(const Cubic& pts, int i, int j, double distance[2]) { |
| 79 | distance[0] = a * pts[i].x + b * pts[i].y + c; |
| 80 | distance[1] = a * pts[j].x + b * pts[j].y + c; |
| 81 | } |
| 82 | |
| 83 | double controlPtDistance(const Quadratic& pts) { |
| 84 | return a * pts[1].x + b * pts[1].y + c; |
| 85 | } |
| 86 | |
| 87 | double pointDistance(const _Point& pt) { |
| 88 | return a * pt.x + b * pt.y + c; |
| 89 | } |
| 90 | private: |
| 91 | double a; |
| 92 | double b; |
| 93 | double c; |
| 94 | }; |