| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2.1 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Lesser General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Lesser General Public |
| 15 | * License along with this library; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | */ |
| 18 |
| 19 | #ifndef VBEZIER_H |
| 20 | #define VBEZIER_H |
| 21 |
| 22 | #include <vpoint.h> |
| 23 |
| 24 | V_BEGIN_NAMESPACE |
| 25 |
| 26 | class VBezier { |
| 27 | public: |
| 28 | VBezier() = default; |
| 29 | VPointF pointAt(float t) const; |
| 30 | float angleAt(float t) const; |
| 31 | VBezier onInterval(float t0, float t1) const; |
| 32 | float length() const; |
| 33 | static void coefficients(float t, float &a, float &b, float &c, float &d); |
| 34 | static VBezier fromPoints(const VPointF &start, const VPointF &cp1, |
| 35 | const VPointF &cp2, const VPointF &end); |
| 36 | inline void parameterSplitLeft(float t, VBezier *left); |
| 37 | inline void split(VBezier *firstHalf, VBezier *secondHalf) const; |
| 38 | float tAtLength(float len) const; |
| 39 | void splitAtLength(float len, VBezier *left, VBezier *right); |
| 40 | VPointF pt1() const { return {x1, y1}; } |
| 41 | VPointF pt2() const { return {x2, y2}; } |
| 42 | VPointF pt3() const { return {x3, y3}; } |
| 43 | VPointF pt4() const { return {x4, y4}; } |
| 44 |
| 45 | private: |
| 46 | VPointF derivative(float t) const; |
| 47 | float x1, y1, x2, y2, x3, y3, x4, y4; |
| 48 | }; |
| 49 |
| 50 | inline void VBezier::coefficients(float t, float &a, float &b, float &c, |
| 51 | float &d) |
| 52 | { |
| 53 | float m_t = 1. - t; |
| 54 | b = m_t * m_t; |
| 55 | c = t * t; |
| 56 | d = c * t; |
| 57 | a = b * m_t; |
| 58 | b *= 3. * t; |
| 59 | c *= 3. * m_t; |
| 60 | } |
| 61 |
| 62 | inline VPointF VBezier::pointAt(float t) const |
| 63 | { |
| 64 | // numerically more stable: |
| 65 | float x, y; |
| 66 |
| 67 | float m_t = 1. - t; |
| 68 | { |
| 69 | float a = x1 * m_t + x2 * t; |
| 70 | float b = x2 * m_t + x3 * t; |
| 71 | float c = x3 * m_t + x4 * t; |
| 72 | a = a * m_t + b * t; |
| 73 | b = b * m_t + c * t; |
| 74 | x = a * m_t + b * t; |
| 75 | } |
| 76 | { |
| 77 | float a = y1 * m_t + y2 * t; |
| 78 | float b = y2 * m_t + y3 * t; |
| 79 | float c = y3 * m_t + y4 * t; |
| 80 | a = a * m_t + b * t; |
| 81 | b = b * m_t + c * t; |
| 82 | y = a * m_t + b * t; |
| 83 | } |
| 84 | return {x, y}; |
| 85 | } |
| 86 |
| 87 | inline void VBezier::parameterSplitLeft(float t, VBezier *left) |
| 88 | { |
| 89 | left->x1 = x1; |
| 90 | left->y1 = y1; |
| 91 |
| 92 | left->x2 = x1 + t * (x2 - x1); |
| 93 | left->y2 = y1 + t * (y2 - y1); |
| 94 |
| 95 | left->x3 = x2 + t * (x3 - x2); // temporary holding spot |
| 96 | left->y3 = y2 + t * (y3 - y2); // temporary holding spot |
| 97 |
| 98 | x3 = x3 + t * (x4 - x3); |
| 99 | y3 = y3 + t * (y4 - y3); |
| 100 |
| 101 | x2 = left->x3 + t * (x3 - left->x3); |
| 102 | y2 = left->y3 + t * (y3 - left->y3); |
| 103 |
| 104 | left->x3 = left->x2 + t * (left->x3 - left->x2); |
| 105 | left->y3 = left->y2 + t * (left->y3 - left->y2); |
| 106 |
| 107 | left->x4 = x1 = left->x3 + t * (x2 - left->x3); |
| 108 | left->y4 = y1 = left->y3 + t * (y2 - left->y3); |
| 109 | } |
| 110 |
| 111 | inline void VBezier::split(VBezier *firstHalf, VBezier *secondHalf) const |
| 112 | { |
| 113 | float c = (x2 + x3) * .5; |
| 114 | firstHalf->x2 = (x1 + x2) * .5; |
| 115 | secondHalf->x3 = (x3 + x4) * .5; |
| 116 | firstHalf->x1 = x1; |
| 117 | secondHalf->x4 = x4; |
| 118 | firstHalf->x3 = (firstHalf->x2 + c) * .5; |
| 119 | secondHalf->x2 = (secondHalf->x3 + c) * .5; |
| 120 | firstHalf->x4 = secondHalf->x1 = (firstHalf->x3 + secondHalf->x2) * .5; |
| 121 |
| 122 | c = (y2 + y3) / 2; |
| 123 | firstHalf->y2 = (y1 + y2) * .5; |
| 124 | secondHalf->y3 = (y3 + y4) * .5; |
| 125 | firstHalf->y1 = y1; |
| 126 | secondHalf->y4 = y4; |
| 127 | firstHalf->y3 = (firstHalf->y2 + c) * .5; |
| 128 | secondHalf->y2 = (secondHalf->y3 + c) * .5; |
| 129 | firstHalf->y4 = secondHalf->y1 = (firstHalf->y3 + secondHalf->y2) * .5; |
| 130 | } |
| 131 |
| 132 | V_END_NAMESPACE |
| 133 |
| 134 | #endif // VBEZIER_H |
| 135 |