1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
|
// Copyright 2007 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview Represents a cubic Bezier curve.
*
* Uses the deCasteljau algorithm to compute points on the curve.
* http://en.wikipedia.org/wiki/De_Casteljau's_algorithm
*
* Currently it uses an unrolled version of the algorithm for speed. Eventually
* it may be useful to use the loop form of the algorithm in order to support
* curves of arbitrary degree.
*
* @author robbyw@google.com (Robby Walker)
*/
goog.provide('goog.math.Bezier');
goog.require('goog.math');
goog.require('goog.math.Coordinate');
/**
* Object representing a cubic bezier curve.
* @param {number} x0 X coordinate of the start point.
* @param {number} y0 Y coordinate of the start point.
* @param {number} x1 X coordinate of the first control point.
* @param {number} y1 Y coordinate of the first control point.
* @param {number} x2 X coordinate of the second control point.
* @param {number} y2 Y coordinate of the second control point.
* @param {number} x3 X coordinate of the end point.
* @param {number} y3 Y coordinate of the end point.
* @struct
* @constructor
* @final
*/
goog.math.Bezier = function(x0, y0, x1, y1, x2, y2, x3, y3) {
/**
* X coordinate of the first point.
* @type {number}
*/
this.x0 = x0;
/**
* Y coordinate of the first point.
* @type {number}
*/
this.y0 = y0;
/**
* X coordinate of the first control point.
* @type {number}
*/
this.x1 = x1;
/**
* Y coordinate of the first control point.
* @type {number}
*/
this.y1 = y1;
/**
* X coordinate of the second control point.
* @type {number}
*/
this.x2 = x2;
/**
* Y coordinate of the second control point.
* @type {number}
*/
this.y2 = y2;
/**
* X coordinate of the end point.
* @type {number}
*/
this.x3 = x3;
/**
* Y coordinate of the end point.
* @type {number}
*/
this.y3 = y3;
};
/**
* Constant used to approximate ellipses.
* See: http://canvaspaint.org/blog/2006/12/ellipse/
* @type {number}
*/
goog.math.Bezier.KAPPA = 4 * (Math.sqrt(2) - 1) / 3;
/**
* @return {!goog.math.Bezier} A copy of this curve.
*/
goog.math.Bezier.prototype.clone = function() {
return new goog.math.Bezier(
this.x0, this.y0, this.x1, this.y1, this.x2, this.y2, this.x3, this.y3);
};
/**
* Test if the given curve is exactly the same as this one.
* @param {goog.math.Bezier} other The other curve.
* @return {boolean} Whether the given curve is the same as this one.
*/
goog.math.Bezier.prototype.equals = function(other) {
return this.x0 == other.x0 && this.y0 == other.y0 && this.x1 == other.x1 &&
this.y1 == other.y1 && this.x2 == other.x2 && this.y2 == other.y2 &&
this.x3 == other.x3 && this.y3 == other.y3;
};
/**
* Modifies the curve in place to progress in the opposite direction.
*/
goog.math.Bezier.prototype.flip = function() {
var temp = this.x0;
this.x0 = this.x3;
this.x3 = temp;
temp = this.y0;
this.y0 = this.y3;
this.y3 = temp;
temp = this.x1;
this.x1 = this.x2;
this.x2 = temp;
temp = this.y1;
this.y1 = this.y2;
this.y2 = temp;
};
/**
* Computes the curve's X coordinate at a point between 0 and 1.
* @param {number} t The point on the curve to find.
* @return {number} The computed coordinate.
*/
goog.math.Bezier.prototype.getPointX = function(t) {
// Special case start and end.
if (t == 0) {
return this.x0;
} else if (t == 1) {
return this.x3;
}
// Step one - from 4 points to 3
var ix0 = goog.math.lerp(this.x0, this.x1, t);
var ix1 = goog.math.lerp(this.x1, this.x2, t);
var ix2 = goog.math.lerp(this.x2, this.x3, t);
// Step two - from 3 points to 2
ix0 = goog.math.lerp(ix0, ix1, t);
ix1 = goog.math.lerp(ix1, ix2, t);
// Final step - last point
return goog.math.lerp(ix0, ix1, t);
};
/**
* Computes the curve's Y coordinate at a point between 0 and 1.
* @param {number} t The point on the curve to find.
* @return {number} The computed coordinate.
*/
goog.math.Bezier.prototype.getPointY = function(t) {
// Special case start and end.
if (t == 0) {
return this.y0;
} else if (t == 1) {
return this.y3;
}
// Step one - from 4 points to 3
var iy0 = goog.math.lerp(this.y0, this.y1, t);
var iy1 = goog.math.lerp(this.y1, this.y2, t);
var iy2 = goog.math.lerp(this.y2, this.y3, t);
// Step two - from 3 points to 2
iy0 = goog.math.lerp(iy0, iy1, t);
iy1 = goog.math.lerp(iy1, iy2, t);
// Final step - last point
return goog.math.lerp(iy0, iy1, t);
};
/**
* Computes the curve at a point between 0 and 1.
* @param {number} t The point on the curve to find.
* @return {!goog.math.Coordinate} The computed coordinate.
*/
goog.math.Bezier.prototype.getPoint = function(t) {
return new goog.math.Coordinate(this.getPointX(t), this.getPointY(t));
};
/**
* Changes this curve in place to be the portion of itself from [t, 1].
* @param {number} t The start of the desired portion of the curve.
*/
goog.math.Bezier.prototype.subdivideLeft = function(t) {
if (t == 1) {
return;
}
// Step one - from 4 points to 3
var ix0 = goog.math.lerp(this.x0, this.x1, t);
var iy0 = goog.math.lerp(this.y0, this.y1, t);
var ix1 = goog.math.lerp(this.x1, this.x2, t);
var iy1 = goog.math.lerp(this.y1, this.y2, t);
var ix2 = goog.math.lerp(this.x2, this.x3, t);
var iy2 = goog.math.lerp(this.y2, this.y3, t);
// Collect our new x1 and y1
this.x1 = ix0;
this.y1 = iy0;
// Step two - from 3 points to 2
ix0 = goog.math.lerp(ix0, ix1, t);
iy0 = goog.math.lerp(iy0, iy1, t);
ix1 = goog.math.lerp(ix1, ix2, t);
iy1 = goog.math.lerp(iy1, iy2, t);
// Collect our new x2 and y2
this.x2 = ix0;
this.y2 = iy0;
// Final step - last point
this.x3 = goog.math.lerp(ix0, ix1, t);
this.y3 = goog.math.lerp(iy0, iy1, t);
};
/**
* Changes this curve in place to be the portion of itself from [0, t].
* @param {number} t The end of the desired portion of the curve.
*/
goog.math.Bezier.prototype.subdivideRight = function(t) {
this.flip();
this.subdivideLeft(1 - t);
this.flip();
};
/**
* Changes this curve in place to be the portion of itself from [s, t].
* @param {number} s The start of the desired portion of the curve.
* @param {number} t The end of the desired portion of the curve.
*/
goog.math.Bezier.prototype.subdivide = function(s, t) {
this.subdivideRight(s);
this.subdivideLeft((t - s) / (1 - s));
};
/**
* Computes the position t of a point on the curve given its x coordinate.
* That is, for an input xVal, finds t s.t. getPointX(t) = xVal.
* As such, the following should always be true up to some small epsilon:
* t ~ solvePositionFromXValue(getPointX(t)) for t in [0, 1].
* @param {number} xVal The x coordinate of the point to find on the curve.
* @return {number} The position t.
*/
goog.math.Bezier.prototype.solvePositionFromXValue = function(xVal) {
// Desired precision on the computation.
var epsilon = 1e-6;
// Initial estimate of t using linear interpolation.
var t = (xVal - this.x0) / (this.x3 - this.x0);
if (t <= 0) {
return 0;
} else if (t >= 1) {
return 1;
}
// Try gradient descent to solve for t. If it works, it is very fast.
var tMin = 0;
var tMax = 1;
var value = 0;
for (var i = 0; i < 8; i++) {
value = this.getPointX(t);
var derivative = (this.getPointX(t + epsilon) - value) / epsilon;
if (Math.abs(value - xVal) < epsilon) {
return t;
} else if (Math.abs(derivative) < epsilon) {
break;
} else {
if (value < xVal) {
tMin = t;
} else {
tMax = t;
}
t -= (value - xVal) / derivative;
}
}
// If the gradient descent got stuck in a local minimum, e.g. because
// the derivative was close to 0, use a Dichotomy refinement instead.
// We limit the number of interations to 8.
for (var i = 0; Math.abs(value - xVal) > epsilon && i < 8; i++) {
if (value < xVal) {
tMin = t;
t = (t + tMax) / 2;
} else {
tMax = t;
t = (t + tMin) / 2;
}
value = this.getPointX(t);
}
return t;
};
/**
* Computes the y coordinate of a point on the curve given its x coordinate.
* @param {number} xVal The x coordinate of the point on the curve.
* @return {number} The y coordinate of the point on the curve.
*/
goog.math.Bezier.prototype.solveYValueFromXValue = function(xVal) {
return this.getPointY(this.solvePositionFromXValue(xVal));
};
|