mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2024-12-29 09:22:22 -05:00
Some minor adjustments for #1530
This commit is contained in:
parent
c235d6a917
commit
f5366fb3cb
3 changed files with 23 additions and 25 deletions
|
@ -2,7 +2,7 @@
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<title>Path Tangents To Vector</title>
|
<title>Path Tangents</title>
|
||||||
<link rel="stylesheet" href="../css/style.css">
|
<link rel="stylesheet" href="../css/style.css">
|
||||||
<script type="text/javascript" src="../../dist/paper-full.js"></script>
|
<script type="text/javascript" src="../../dist/paper-full.js"></script>
|
||||||
<script type="text/paperscript" canvas="canvas">
|
<script type="text/paperscript" canvas="canvas">
|
|
@ -1156,12 +1156,10 @@ statics: /** @lends Curve */{
|
||||||
* tangential to the given tangent
|
* tangential to the given tangent
|
||||||
*/
|
*/
|
||||||
getTimesWithTangent: function (/* tangent */) {
|
getTimesWithTangent: function (/* tangent */) {
|
||||||
var vector = Point.read(arguments);
|
var tangent = Point.read(arguments);
|
||||||
if (vector.isZero()) {
|
return tangent.isZero()
|
||||||
return [];
|
? []
|
||||||
}
|
: Curve.getTimesWithTangent(this.getValues(), tangent);
|
||||||
|
|
||||||
return Curve.getTimesWithTangent(this.getValues(), vector);
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -2253,17 +2251,17 @@ new function() { // Scope for bezier intersection using fat-line clipping
|
||||||
* Tangents at the start or end are included.
|
* Tangents at the start or end are included.
|
||||||
*
|
*
|
||||||
* @param {Number[]} v curve values
|
* @param {Number[]} v curve values
|
||||||
* @param {Point} point the tangent to which the curve must be tangential
|
* @param {Point} tangent the tangent to which the curve must be tangential
|
||||||
* @return {Number[]} at most two curve-time parameters, where the curve is
|
* @return {Number[]} at most two curve-time parameters, where the curve is
|
||||||
* tangential to the given tangent
|
* tangential to the given tangent
|
||||||
*/
|
*/
|
||||||
function getTimesWithTangent(v, point) {
|
function getTimesWithTangent(v, tangent) {
|
||||||
// Algorithm adapted from: https://stackoverflow.com/a/34837312/7615922
|
// Algorithm adapted from: https://stackoverflow.com/a/34837312/7615922
|
||||||
var x0 = v[0], y0 = v[1],
|
var x0 = v[0], y0 = v[1],
|
||||||
x1 = v[2], y1 = v[3],
|
x1 = v[2], y1 = v[3],
|
||||||
x2 = v[4], y2 = v[5],
|
x2 = v[4], y2 = v[5],
|
||||||
x3 = v[6], y3 = v[7],
|
x3 = v[6], y3 = v[7],
|
||||||
normalized = point.normalize(),
|
normalized = tangent.normalize(),
|
||||||
tx = normalized.x,
|
tx = normalized.x,
|
||||||
ty = normalized.y,
|
ty = normalized.y,
|
||||||
ax = 3 * x3 - 9 * x2 + 9 * x1 - 3 * x0,
|
ax = 3 * x3 - 9 * x2 + 9 * x1 - 3 * x0,
|
||||||
|
@ -2275,8 +2273,8 @@ new function() { // Scope for bezier intersection using fat-line clipping
|
||||||
den = 2 * ax * ty - 2 * ay * tx,
|
den = 2 * ax * ty - 2 * ay * tx,
|
||||||
times = [];
|
times = [];
|
||||||
if (Math.abs(den) < Numerical.CURVETIME_EPSILON) {
|
if (Math.abs(den) < Numerical.CURVETIME_EPSILON) {
|
||||||
var num = ax * cy - ay * cx;
|
var num = ax * cy - ay * cx,
|
||||||
var den = ax * by - ay * bx;
|
den = ax * by - ay * bx;
|
||||||
if (den != 0) {
|
if (den != 0) {
|
||||||
var t = -num / den;
|
var t = -num / den;
|
||||||
if (t >= 0 && t <= 1) times.push(t);
|
if (t >= 0 && t <= 1) times.push(t);
|
||||||
|
@ -2284,12 +2282,12 @@ new function() { // Scope for bezier intersection using fat-line clipping
|
||||||
} else {
|
} else {
|
||||||
var delta = (bx * bx - 4 * ax * cx) * ty * ty +
|
var delta = (bx * bx - 4 * ax * cx) * ty * ty +
|
||||||
(-2 * bx * by + 4 * ay * cx + 4 * ax * cy) * tx * ty +
|
(-2 * bx * by + 4 * ay * cx + 4 * ax * cy) * tx * ty +
|
||||||
(by * by - 4 * ay * cy) * tx * tx;
|
(by * by - 4 * ay * cy) * tx * tx,
|
||||||
var k = bx * ty - by * tx;
|
k = bx * ty - by * tx;
|
||||||
if (delta >= 0 && den != 0) {
|
if (delta >= 0 && den != 0) {
|
||||||
var d = Math.sqrt(delta);
|
var d = Math.sqrt(delta),
|
||||||
var t0 = -(k + d) / den;
|
t0 = -(k + d) / den,
|
||||||
var t1 = (-k + d) / den;
|
t1 = (-k + d) / den;
|
||||||
if (t0 >= 0 && t0 <= 1) times.push(t0);
|
if (t0 >= 0 && t0 <= 1) times.push(t0);
|
||||||
if (t1 >= 0 && t1 <= 1) times.push(t1);
|
if (t1 >= 0 && t1 <= 1) times.push(t1);
|
||||||
}
|
}
|
||||||
|
|
|
@ -2125,9 +2125,9 @@ var Path = PathItem.extend(/** @lends Path# */{
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calculates path offsets where the path is tangential to provided tangent.
|
* Calculates path offsets where the path is tangential to the provided
|
||||||
* Note that tangent at start or end are included.
|
* tangent. Note that tangents at the start or end are included. Tangents at
|
||||||
* Tangent at segment point is returned even if only one of its handles is
|
* segment points are returned even if only one of their handles is
|
||||||
* collinear with the provided tangent.
|
* collinear with the provided tangent.
|
||||||
*
|
*
|
||||||
* @param {Point} tangent the tangent to which the path must be tangential
|
* @param {Point} tangent the tangent to which the path must be tangential
|
||||||
|
@ -2141,21 +2141,21 @@ var Path = PathItem.extend(/** @lends Path# */{
|
||||||
}
|
}
|
||||||
|
|
||||||
var offsets = [];
|
var offsets = [];
|
||||||
var offsetBeforeCurve = 0;
|
var curveStart = 0;
|
||||||
var curves = this.getCurves();
|
var curves = this.getCurves();
|
||||||
for (var i = 0; i < curves.length; i++) {
|
for (var i = 0, l = curves.length; i < l; i++) {
|
||||||
var curve = curves[i];
|
var curve = curves[i];
|
||||||
// Calculate curves times at vector tangent...
|
// Calculate curves times at vector tangent...
|
||||||
var curveTimes = curve.getTimesWithTangent(tangent);
|
var curveTimes = curve.getTimesWithTangent(tangent);
|
||||||
for (var j = 0; j < curveTimes.length; j++) {
|
for (var j = 0, m = curveTimes.length; j < m; j++) {
|
||||||
// ...and convert them to path offsets...
|
// ...and convert them to path offsets...
|
||||||
var offset = offsetBeforeCurve + curve.getOffsetAtTime(curveTimes[j]);
|
var offset = curveStart + curve.getOffsetAtTime(curveTimes[j]);
|
||||||
// ...avoiding duplicates.
|
// ...avoiding duplicates.
|
||||||
if (offsets.indexOf(offset) < 0) {
|
if (offsets.indexOf(offset) < 0) {
|
||||||
offsets.push(offset);
|
offsets.push(offset);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
offsetBeforeCurve += curve.length;
|
curveStart += curve.length;
|
||||||
}
|
}
|
||||||
return offsets;
|
return offsets;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue