mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-20 22:39:50 -05:00
Inline EPSILON and TOLERANCE for better performance in Numerical.
This commit is contained in:
parent
6b4917f4a8
commit
76ea7ef066
1 changed files with 18 additions and 18 deletions
|
@ -56,12 +56,14 @@ var Numerical = new function() {
|
||||||
sqrt = Math.sqrt,
|
sqrt = Math.sqrt,
|
||||||
pow = Math.pow,
|
pow = Math.pow,
|
||||||
cos = Math.cos,
|
cos = Math.cos,
|
||||||
PI = Math.PI;
|
PI = Math.PI,
|
||||||
|
TOLERANCE = 10e-6,
|
||||||
|
EPSILON = 10e-12;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
TOLERANCE: 10e-6,
|
TOLERANCE: TOLERANCE,
|
||||||
// Precision when comparing against 0
|
// Precision when comparing against 0
|
||||||
EPSILON: 10e-12,
|
EPSILON: EPSILON,
|
||||||
// Kappa, see: http://www.whizkidtech.redprince.net/bezier/circle/kappa/
|
// Kappa, see: http://www.whizkidtech.redprince.net/bezier/circle/kappa/
|
||||||
KAPPA: 4 * (sqrt(2) - 1) / 3,
|
KAPPA: 4 * (sqrt(2) - 1) / 3,
|
||||||
|
|
||||||
|
@ -70,7 +72,7 @@ var Numerical = new function() {
|
||||||
* Numerical.EPSILON.
|
* Numerical.EPSILON.
|
||||||
*/
|
*/
|
||||||
isZero: function(val) {
|
isZero: function(val) {
|
||||||
return abs(val) <= Numerical.EPSILON;
|
return abs(val) <= EPSILON;
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -127,10 +129,9 @@ var Numerical = new function() {
|
||||||
* a*x^2 + b*x + c = 0
|
* a*x^2 + b*x + c = 0
|
||||||
*/
|
*/
|
||||||
solveQuadratic: function(a, b, c, roots, min, max) {
|
solveQuadratic: function(a, b, c, roots, min, max) {
|
||||||
var epsilon = Numerical.EPSILON,
|
var unbound = min === undefined,
|
||||||
unbound = min === undefined,
|
minE = min - EPSILON,
|
||||||
minE = min - epsilon,
|
maxE = max + EPSILON,
|
||||||
maxE = max + epsilon,
|
|
||||||
count = 0;
|
count = 0;
|
||||||
|
|
||||||
function add(root) {
|
function add(root) {
|
||||||
|
@ -141,17 +142,17 @@ var Numerical = new function() {
|
||||||
|
|
||||||
// Code ported over and adapted from Uintah library (MIT license).
|
// Code ported over and adapted from Uintah library (MIT license).
|
||||||
// If a is 0, equation is actually linear, return 0 or 1 easy roots.
|
// If a is 0, equation is actually linear, return 0 or 1 easy roots.
|
||||||
if (abs(a) < epsilon) {
|
if (abs(a) < EPSILON) {
|
||||||
if (abs(b) >= epsilon)
|
if (abs(b) >= EPSILON)
|
||||||
return add(-c / b);
|
return add(-c / b);
|
||||||
// If all the coefficients are 0, we have infinite solutions!
|
// If all the coefficients are 0, we have infinite solutions!
|
||||||
return abs(c) < epsilon ? -1 : 0; // Infinite or 0 solutions
|
return abs(c) < EPSILON ? -1 : 0; // Infinite or 0 solutions
|
||||||
}
|
}
|
||||||
// Convert to normal form: x^2 + px + q = 0
|
// Convert to normal form: x^2 + px + q = 0
|
||||||
var p = b / (2 * a);
|
var p = b / (2 * a);
|
||||||
var q = c / a;
|
var q = c / a;
|
||||||
var p2 = p * p;
|
var p2 = p * p;
|
||||||
if (p2 < q - epsilon)
|
if (p2 < q - EPSILON)
|
||||||
return 0;
|
return 0;
|
||||||
var s = p2 > q ? sqrt(p2 - q) : 0;
|
var s = p2 > q ? sqrt(p2 - q) : 0;
|
||||||
add (s - p);
|
add (s - p);
|
||||||
|
@ -167,14 +168,13 @@ var Numerical = new function() {
|
||||||
* a*x^3 + b*x^2 + c*x + d = 0
|
* a*x^3 + b*x^2 + c*x + d = 0
|
||||||
*/
|
*/
|
||||||
solveCubic: function(a, b, c, d, roots, min, max) {
|
solveCubic: function(a, b, c, d, roots, min, max) {
|
||||||
var epsilon = Numerical.EPSILON;
|
|
||||||
// If a is 0, equation is actually quadratic.
|
// If a is 0, equation is actually quadratic.
|
||||||
if (abs(a) < epsilon)
|
if (abs(a) < EPSILON)
|
||||||
return Numerical.solveQuadratic(b, c, d, roots, min, max);
|
return Numerical.solveQuadratic(b, c, d, roots, min, max);
|
||||||
|
|
||||||
var unbound = min === undefined,
|
var unbound = min === undefined,
|
||||||
minE = min - epsilon,
|
minE = min - EPSILON,
|
||||||
maxE = max + epsilon,
|
maxE = max + EPSILON,
|
||||||
count = 0;
|
count = 0;
|
||||||
|
|
||||||
function add(root) {
|
function add(root) {
|
||||||
|
@ -197,8 +197,8 @@ var Numerical = new function() {
|
||||||
D = q * q - ppp;
|
D = q * q - ppp;
|
||||||
// Substitute x = y - b/3 to eliminate quadric term: x^3 +px + q = 0
|
// Substitute x = y - b/3 to eliminate quadric term: x^3 +px + q = 0
|
||||||
b /= 3;
|
b /= 3;
|
||||||
if (abs(D) < epsilon) {
|
if (abs(D) < EPSILON) {
|
||||||
if (abs(q) < epsilon) // One triple solution.
|
if (abs(q) < EPSILON) // One triple solution.
|
||||||
return add(-b);
|
return add(-b);
|
||||||
// One single and one double solution.
|
// One single and one double solution.
|
||||||
var sqp = sqrt(p),
|
var sqp = sqrt(p),
|
||||||
|
|
Loading…
Reference in a new issue