mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-03-15 17:29:52 -04:00
Improve CurveLocation#equals().
This commit is contained in:
parent
1f476c2107
commit
eb62530958
1 changed files with 16 additions and 14 deletions
|
@ -301,31 +301,33 @@ var CurveLocation = Base.extend(/** @lends CurveLocation# */{
|
||||||
* @return {Boolean} {@true if the locations are equal}
|
* @return {Boolean} {@true if the locations are equal}
|
||||||
*/
|
*/
|
||||||
equals: function(loc, _ignoreOther) {
|
equals: function(loc, _ignoreOther) {
|
||||||
var res = this === loc;
|
var res = this === loc,
|
||||||
|
epsilon = /*#=*/Numerical.GEOMETRIC_EPSILON;
|
||||||
|
// NOTE: We need to compare both by (index + parameter) and by proximity
|
||||||
|
// of points. See:
|
||||||
|
// https://github.com/paperjs/paper.js/issues/784#issuecomment-143161586
|
||||||
if (!res && loc instanceof CurveLocation
|
if (!res && loc instanceof CurveLocation
|
||||||
&& this.getPath() === loc.getPath()) {
|
&& this.getPath() === loc.getPath()
|
||||||
// NOTE: We need to compare both by (index + parameter) and by
|
&& this.getPoint().isClose(loc.getPoint(), epsilon)) {
|
||||||
// proximity of points, see:
|
// The position is the same, but it could still be in a different
|
||||||
// https://github.com/paperjs/paper.js/issues/784#issuecomment-143161586
|
// location on the path. Perform more thorough checks now:
|
||||||
// We need to wrap the diff value around the path's beginning / end.
|
|
||||||
var c1 = this.getCurve(),
|
var c1 = this.getCurve(),
|
||||||
c2 = loc.getCurve(),
|
c2 = loc.getCurve(),
|
||||||
abs = Math.abs,
|
abs = Math.abs,
|
||||||
|
// We need to wrap diff around the path's beginning / end:
|
||||||
diff = abs(
|
diff = abs(
|
||||||
((c1.isLast() && c2.isFirst() ? -1 : c1.getIndex())
|
((c1.isLast() && c2.isFirst() ? -1 : c1.getIndex())
|
||||||
+ this.getParameter()) -
|
+ this.getParameter()) -
|
||||||
((c2.isLast() && c1.isFirst() ? -1 : c2.getIndex())
|
((c2.isLast() && c1.isFirst() ? -1 : c2.getIndex())
|
||||||
+ loc.getParameter())),
|
+ loc.getParameter()));
|
||||||
eps = /*#=*/Numerical.GEOMETRIC_EPSILON;
|
|
||||||
res = (diff < /*#=*/Numerical.CURVETIME_EPSILON
|
res = (diff < /*#=*/Numerical.CURVETIME_EPSILON
|
||||||
// When the location is close enough, compare the offsets of
|
// If diff isn't close enough, compare the actual offsets of
|
||||||
// both locations to determine if they're in the same spot,
|
// both locations to determine if they're in the same spot,
|
||||||
// taking into account the wrapping around path ends too.
|
// taking into account the wrapping around path ends too.
|
||||||
|| this.getPoint().isClose(loc.getPoint(), eps)
|
// This is necessary in order to handle very short consecutive
|
||||||
// Use GEOMETRIC_EPSILON * 2 when comparing offsets, to
|
// curves (length ~< 1e-7), which would lead to diff > 1.
|
||||||
// slightly increase tolerance when in the same spot.
|
|| ((diff = abs(this.getOffset() - loc.getOffset())) < epsilon
|
||||||
&& ((diff = abs(this.getOffset() - loc.getOffset())) < eps * 2
|
|| abs(this.getPath().getLength() - diff) < epsilon))
|
||||||
|| abs(this.getPath().getLength() - diff) < eps * 2))
|
|
||||||
&& (_ignoreOther
|
&& (_ignoreOther
|
||||||
|| (!this._intersection && !loc._intersection
|
|| (!this._intersection && !loc._intersection
|
||||||
|| this._intersection && this._intersection.equals(
|
|| this._intersection && this._intersection.equals(
|
||||||
|
|
Loading…
Reference in a new issue