mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-06 04:42:15 -05:00
Implement Curve.getNearestParameter()
This commit is contained in:
parent
8c3d9df06c
commit
43cf20096a
1 changed files with 31 additions and 27 deletions
|
@ -645,6 +645,34 @@ statics: {
|
||||||
: null;
|
: null;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
getNearestParameter: function(v, point) {
|
||||||
|
var count = 100,
|
||||||
|
minDist = Infinity,
|
||||||
|
minT = 0;
|
||||||
|
|
||||||
|
function refine(t) {
|
||||||
|
if (t >= 0 && t <= 1) {
|
||||||
|
var dist = point.getDistance(Curve.getPoint(v, t), true);
|
||||||
|
if (dist < minDist) {
|
||||||
|
minDist = dist;
|
||||||
|
minT = t;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (var i = 0; i <= count; i++)
|
||||||
|
refine(i / count);
|
||||||
|
|
||||||
|
// Now iteratively refine solution until we reach desired precision.
|
||||||
|
var step = 1 / (count * 2);
|
||||||
|
while (step > /*#=*/Numerical.CURVETIME_EPSILON) {
|
||||||
|
if (!refine(minT - step) && !refine(minT + step))
|
||||||
|
step /= 2;
|
||||||
|
}
|
||||||
|
return minT;
|
||||||
|
},
|
||||||
|
|
||||||
// TODO: Find better name
|
// TODO: Find better name
|
||||||
getPart: function(v, from, to) {
|
getPart: function(v, from, to) {
|
||||||
var flip = from > to;
|
var flip = from > to;
|
||||||
|
@ -1015,32 +1043,9 @@ statics: {
|
||||||
getNearestLocation: function(/* point */) {
|
getNearestLocation: function(/* point */) {
|
||||||
var point = Point.read(arguments),
|
var point = Point.read(arguments),
|
||||||
values = this.getValues(),
|
values = this.getValues(),
|
||||||
count = 100,
|
t = Curve.getNearestParameter(values, point),
|
||||||
minDist = Infinity,
|
pt = Curve.getPoint(values, t);
|
||||||
minT = 0;
|
return new CurveLocation(this, t, pt, null, point.getDistance(pt));
|
||||||
|
|
||||||
function refine(t) {
|
|
||||||
if (t >= 0 && t <= 1) {
|
|
||||||
var dist = point.getDistance(Curve.getPoint(values, t), true);
|
|
||||||
if (dist < minDist) {
|
|
||||||
minDist = dist;
|
|
||||||
minT = t;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (var i = 0; i <= count; i++)
|
|
||||||
refine(i / count);
|
|
||||||
|
|
||||||
// Now iteratively refine solution until we reach desired precision.
|
|
||||||
var step = 1 / (count * 2);
|
|
||||||
while (step > /*#=*/Numerical.CURVETIME_EPSILON) {
|
|
||||||
if (!refine(minT - step) && !refine(minT + step))
|
|
||||||
step /= 2;
|
|
||||||
}
|
|
||||||
var pt = Curve.getPoint(values, minT);
|
|
||||||
return new CurveLocation(this, minT, pt, null, point.getDistance(pt));
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1861,7 +1866,6 @@ new function() { // Scope for intersection using bezier fat-line clipping
|
||||||
for (var i = 0, t1 = 0;
|
for (var i = 0, t1 = 0;
|
||||||
i < 2 && pairs.length < 2;
|
i < 2 && pairs.length < 2;
|
||||||
i += t1 === 0 ? 0 : 1, t1 = t1 ^ 1) {
|
i += t1 === 0 ? 0 : 1, t1 = t1 ^ 1) {
|
||||||
// TODO: Try with getNearestLocation() instead
|
|
||||||
var t2 = Curve.getParameterOf(v[i ^ 1], new Point(
|
var t2 = Curve.getParameterOf(v[i ^ 1], new Point(
|
||||||
v[i][t1 === 0 ? 0 : 6],
|
v[i][t1 === 0 ? 0 : 6],
|
||||||
v[i][t1 === 0 ? 1 : 7]));
|
v[i][t1 === 0 ? 1 : 7]));
|
||||||
|
|
Loading…
Reference in a new issue