Skip to content

Commit

Permalink
Merge pull request #3 from momentumworks/master
Browse files Browse the repository at this point in the history
Use binary search in t2length() and explicitly check for null and undefined in Bezier function
  • Loading branch information
rveciana committed Feb 15, 2017
2 parents 8af8f32 + 3b00350 commit a7ee981
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 17 deletions.
27 changes: 19 additions & 8 deletions build/path-properties.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// http://geoexamples.com/path-properties/ Version 0.1.1. Copyright 2016 Roger Veciana i Rovira.
// http://geoexamples.com/path-properties/ Version 0.1.1. Copyright 2017 Roger Veciana i Rovira.
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
Expand Down Expand Up @@ -58,7 +58,7 @@ function Bezier$1(ax, ay, bx, by, cx, cy, dx, dy) {
this.c = {x:cx, y:cy};
this.d = {x:dx, y:dy};

if(dx && dy){
if(dx !== null && dx !== undefined && dy !== null && dy !== undefined){
this.getArcLength = getCubicArcLength;
this.getPoint = cubicPoint;
this.getDerivative = cubicDerivative;
Expand Down Expand Up @@ -145,13 +145,24 @@ function cubicDerivative(xs, ys, t){
function t2length(length, total_length, func, xs, ys){
var error = 1;
var t = length/total_length;

while (error > 0.008){

var calcLength = func(xs, ys, t);
error = Math.abs(length - calcLength)/total_length;
t = t + (length-calcLength)/total_length;
var step = (length - func(xs, ys, t))/total_length;

while (error > 0.001){
var increasedTLength = func(xs, ys, t + step);
var decreasedTLength = func(xs, ys, t - step);
var increasedTError = Math.abs(length - increasedTLength)/total_length;
var decreasedTError = Math.abs(length - decreasedTLength)/total_length;
if (increasedTError < error) {
error = increasedTError;
t += step;
} else if (decreasedTError < error) {
error = decreasedTError;
t -= step;
} else {
step /= 2;
}
}

return t;
}

Expand Down

0 comments on commit a7ee981

Please sign in to comment.