diff options
author | 2012-08-21 13:13:52 +0000 | |
---|---|---|
committer | 2012-08-21 13:13:52 +0000 | |
commit | 03f970652e07c6832cae41fa374cb68ca80d472c (patch) | |
tree | 4518a4b05c261016f0545c5b31b0b2e4cda0606a /experimental/Intersection/QuadraticUtilities.cpp | |
parent | dd391b537e5362869f25f22d4e74be0501c1b98c (diff) |
shape ops work in progress
working demo of old vs. new
git-svn-id: http://skia.googlecode.com/svn/trunk@5209 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'experimental/Intersection/QuadraticUtilities.cpp')
-rw-r--r-- | experimental/Intersection/QuadraticUtilities.cpp | 36 |
1 files changed, 27 insertions, 9 deletions
diff --git a/experimental/Intersection/QuadraticUtilities.cpp b/experimental/Intersection/QuadraticUtilities.cpp index 4b695df7b6..cc15bc1444 100644 --- a/experimental/Intersection/QuadraticUtilities.cpp +++ b/experimental/Intersection/QuadraticUtilities.cpp @@ -1,6 +1,19 @@ #include "QuadraticUtilities.h" #include <math.h> +/* + +Numeric Solutions (5.6) suggests to solve the quadratic by computing + + Q = -1/2(B + sgn(B)Sqrt(B^2 - 4 A C)) + +and using the roots + + t1 = Q / A + t2 = C / Q + +*/ + int quadraticRoots(double A, double B, double C, double t[2]) { B *= 2; double square = B * B - 4 * A * C; @@ -9,19 +22,24 @@ int quadraticRoots(double A, double B, double C, double t[2]) { } double squareRt = sqrt(square); double Q = (B + (B < 0 ? -squareRt : squareRt)) / -2; - double ratio; int foundRoots = 0; - if ((Q <= A) ^ (Q < 0)) { - ratio = Q / A; - if (!isnan(ratio)) { - t[foundRoots++] = ratio; + double ratio = Q / A; + if (ratio > -FLT_EPSILON && ratio < 1 + FLT_EPSILON) { + if (ratio < FLT_EPSILON) { + ratio = 0; + } else if (ratio > 1 - FLT_EPSILON) { + ratio = 1; } + t[foundRoots++] = ratio; } - if ((C <= Q) ^ (C < 0)) { - ratio = C / Q; - if (!isnan(ratio)) { - t[foundRoots++] = ratio; + ratio = C / Q; + if (ratio > -FLT_EPSILON && ratio < 1 + FLT_EPSILON) { + if (ratio < FLT_EPSILON) { + ratio = 0; + } else if (ratio > 1 - FLT_EPSILON) { + ratio = 1; } + t[foundRoots++] = ratio; } return foundRoots; } |