avoid divide by zero
R=kjlubick@google.com
Bug: skia:7623
Change-Id: Ia9fb31201a39a4540d94f97e8d2f06eb290ab6e5
Reviewed-on: https://skia-review.googlesource.com/114082
Commit-Queue: Cary Clark <caryclark@skia.org>
Commit-Queue: Cary Clark <caryclark@google.com>
Reviewed-by: Kevin Lubick <kjlubick@google.com>
diff --git a/src/pathops/SkPathOpsQuad.cpp b/src/pathops/SkPathOpsQuad.cpp
index f410ea3..9a104bf 100644
--- a/src/pathops/SkPathOpsQuad.cpp
+++ b/src/pathops/SkPathOpsQuad.cpp
@@ -141,6 +141,15 @@
return foundRoots;
}
+static int handle_zero(const double B, const double C, double s[2]) {
+ if (approximately_zero(B)) {
+ s[0] = 0;
+ return C == 0;
+ }
+ s[0] = -C / B;
+ return 1;
+}
+
/*
Numeric Solutions (5.6) suggests to solve the quadratic by computing
Q = -1/2(B + sgn(B)Sqrt(B^2 - 4 A C))
@@ -150,16 +159,13 @@
*/
// this does not discard real roots <= 0 or >= 1
int SkDQuad::RootsReal(const double A, const double B, const double C, double s[2]) {
+ if (!A) {
+ return handle_zero(B, C, s);
+ }
const double p = B / (2 * A);
const double q = C / A;
- if (!A || (approximately_zero(A) && (approximately_zero_inverse(p)
- || approximately_zero_inverse(q)))) {
- if (approximately_zero(B)) {
- s[0] = 0;
- return C == 0;
- }
- s[0] = -C / B;
- return 1;
+ if (approximately_zero(A) && (approximately_zero_inverse(p) || approximately_zero_inverse(q))) {
+ return handle_zero(B, C, s);
}
/* normal form: x^2 + px + q = 0 */
const double p2 = p * p;