OLD | NEW |
(Empty) | |
| 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 */ |
| 7 #include "SkPathOpsCubic.h" |
| 8 #include "SkPathOpsLine.h" |
| 9 #include "SkPathOpsQuad.h" |
| 10 |
| 11 // Sources |
| 12 // computer-aided design - volume 22 number 9 november 1990 pp 538 - 549 |
| 13 // online at http://cagd.cs.byu.edu/~tom/papers/bezclip.pdf |
| 14 |
| 15 // This turns a line segment into a parameterized line, of the form |
| 16 // ax + by + c = 0 |
| 17 // When a^2 + b^2 == 1, the line is normalized. |
| 18 // The distance to the line for (x, y) is d(x,y) = ax + by + c |
| 19 // |
| 20 // Note that the distances below are not necessarily normalized. To get the true |
| 21 // distance, it's necessary to either call normalize() after xxxEndPoints(), or |
| 22 // divide the result of xxxDistance() by sqrt(normalSquared()) |
| 23 |
| 24 class SkLineParameters { |
| 25 public: |
| 26 void cubicEndPoints(const SkDCubic& pts) { |
| 27 cubicEndPoints(pts, 0, 3); |
| 28 } |
| 29 |
| 30 void cubicEndPoints(const SkDCubic& pts, int s, int e) { |
| 31 a = approximately_pin(pts[s].fY - pts[e].fY); |
| 32 b = approximately_pin(pts[e].fX - pts[s].fX); |
| 33 c = pts[s].fX * pts[e].fY - pts[e].fX * pts[s].fY; |
| 34 } |
| 35 |
| 36 void lineEndPoints(const SkDLine& pts) { |
| 37 a = approximately_pin(pts[0].fY - pts[1].fY); |
| 38 b = approximately_pin(pts[1].fX - pts[0].fX); |
| 39 c = pts[0].fX * pts[1].fY - pts[1].fX * pts[0].fY; |
| 40 } |
| 41 |
| 42 void quadEndPoints(const SkDQuad& pts) { |
| 43 quadEndPoints(pts, 0, 2); |
| 44 } |
| 45 |
| 46 void quadEndPoints(const SkDQuad& pts, int s, int e) { |
| 47 a = approximately_pin(pts[s].fY - pts[e].fY); |
| 48 b = approximately_pin(pts[e].fX - pts[s].fX); |
| 49 c = pts[s].fX * pts[e].fY - pts[e].fX * pts[s].fY; |
| 50 } |
| 51 |
| 52 double normalSquared() const { |
| 53 return a * a + b * b; |
| 54 } |
| 55 |
| 56 bool normalize() { |
| 57 double normal = sqrt(normalSquared()); |
| 58 if (approximately_zero(normal)) { |
| 59 a = b = c = 0; |
| 60 return false; |
| 61 } |
| 62 double reciprocal = 1 / normal; |
| 63 a *= reciprocal; |
| 64 b *= reciprocal; |
| 65 c *= reciprocal; |
| 66 return true; |
| 67 } |
| 68 |
| 69 void cubicDistanceY(const SkDCubic& pts, SkDCubic& distance) const { |
| 70 double oneThird = 1 / 3.0; |
| 71 for (int index = 0; index < 4; ++index) { |
| 72 distance[index].fX = index * oneThird; |
| 73 distance[index].fY = a * pts[index].fX + b * pts[index].fY + c; |
| 74 } |
| 75 } |
| 76 |
| 77 void quadDistanceY(const SkDQuad& pts, SkDQuad& distance) const { |
| 78 double oneHalf = 1 / 2.0; |
| 79 for (int index = 0; index < 3; ++index) { |
| 80 distance[index].fX = index * oneHalf; |
| 81 distance[index].fY = a * pts[index].fX + b * pts[index].fY + c; |
| 82 } |
| 83 } |
| 84 |
| 85 double controlPtDistance(const SkDCubic& pts, int index) const { |
| 86 SkASSERT(index == 1 || index == 2); |
| 87 return a * pts[index].fX + b * pts[index].fY + c; |
| 88 } |
| 89 |
| 90 double controlPtDistance(const SkDQuad& pts) const { |
| 91 return a * pts[1].fX + b * pts[1].fY + c; |
| 92 } |
| 93 |
| 94 double pointDistance(const SkDPoint& pt) const { |
| 95 return a * pt.fX + b * pt.fY + c; |
| 96 } |
| 97 |
| 98 double dx() const { |
| 99 return b; |
| 100 } |
| 101 |
| 102 double dy() const { |
| 103 return -a; |
| 104 } |
| 105 |
| 106 private: |
| 107 double a; |
| 108 double b; |
| 109 double c; |
| 110 }; |
OLD | NEW |