mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-05 20:32:00 -05:00
Simplify Numerical.solveQuadratic()
This commit is contained in:
parent
a07538b205
commit
89704243b1
1 changed files with 11 additions and 10 deletions
|
@ -138,16 +138,17 @@ var Numerical = new function() {
|
|||
// If all the coefficients are 0, we have infinite solutions!
|
||||
return abs(c) < epsilon ? -1 : 0; // Infinite or 0 solutions
|
||||
}
|
||||
var q = b * b - 4 * a * c;
|
||||
if (q < 0)
|
||||
return 0; // 0 solutions
|
||||
q = sqrt(q);
|
||||
a *= 2; // Prepare division by (2 * a)
|
||||
var n = 0;
|
||||
roots[n++] = (-b - q) / a;
|
||||
if (q > 0)
|
||||
roots[n++] = (-b + q) / a;
|
||||
return n; // 1 or 2 solutions
|
||||
// Convert to normal form: x^2 + px + q = 0
|
||||
var p = b / (2 * a);
|
||||
var q = c / a;
|
||||
var p2 = p * p;
|
||||
if (!abs(p2 - q) < epsilon && p2 < q)
|
||||
return 0;
|
||||
var s = p2 > q ? sqrt(p2 - q) : 0;
|
||||
roots[0] = s - p;
|
||||
if (s > 0)
|
||||
roots[1] = -s - p;
|
||||
return s > 0 ? 2 : 1;
|
||||
},
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue