Skip to content

Commit

Permalink
Merge pull request #86 from hfutrell/issue-81-contour-disappears
Browse files Browse the repository at this point in the history
Issue 81 contour disappears
  • Loading branch information
hfutrell authored Jan 22, 2021
2 parents 38b45b3 + b57bff8 commit 00b7870
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
11 changes: 11 additions & 0 deletions BezierKit/BezierKitTests/UtilsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,17 @@ class UtilsTests: XCTestCase {
// XCTAssertEqual(filtered.first!, CGFloat(0.00149972), accuracy: 1.0e-4)
// }

func testDrootsCubicWorldIssue3() {
// this data causes issue #81 on GitHub
// discriminant is positive but very close to zero (8.46e-10)
// https://github.com/hfutrell/BezierKit/issues/81
let firstValue: CGFloat = -14.999127297400882
let otherValues: CGFloat = 0.00087270259911775838
let roots = drootsCubicTestHelper(firstValue, otherValues, otherValues, otherValues)
XCTAssertEqual(roots.count, 1)
XCTAssertEqual(roots[0], CGFloat(0.961251), accuracy: 1.0e-4)
}

func testDrootsQuadratic() {
let a: CGFloat = 0.36159566118413977
let b: CGFloat = -3.2979288390483816
Expand Down
5 changes: 3 additions & 2 deletions BezierKit/Library/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ internal class Utils {
let q = (2 * a * a * a - 9 * a * b + 27 * c) / 27
let q2 = q/2
let discriminant = q2 * q2 + p * p * p / 27
if discriminant < -smallValue {
let tinyValue = 1.0e-14
if discriminant < -tinyValue {
let r = sqrt(-p * p * p / 27)
let t = -q / (2 * r)
let cosphi = t < -1 ? -1 : t > 1 ? 1 : t
Expand All @@ -204,7 +205,7 @@ internal class Utils {
if root3 > root2 {
callback(root3)
}
} else if discriminant > smallValue {
} else if discriminant > tinyValue {
let sd = sqrt(discriminant)
let u1 = crt(-q2 + sd)
let v1 = crt(q2 + sd)
Expand Down

0 comments on commit 00b7870

Please sign in to comment.