Skip to content

Commit

Permalink
test data from a real world case where roots was failing.
Browse files Browse the repository at this point in the history
  • Loading branch information
hfutrell committed Jun 4, 2019
1 parent 9cb2e51 commit 80f01d6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
15 changes: 15 additions & 0 deletions BezierKit/BezierKitTests/UtilsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,19 @@ class UtilsTests: XCTestCase {
XCTAssertTrue(Utils.clamp(CGFloat.nan, -1.0, 1.0).isNaN)
}

func testRootsRealWorldIssue() {
let points: [CGPoint] = [
CGPoint(x:523.4257521858988, y: 691.8949684622992),
CGPoint(x:523.1393916834338, y: 691.8714265856051),
CGPoint(x:522.8595588275791, y: 691.7501129962762),
CGPoint(x:522.6404735257349, y: 691.531027694432)
]
let curve = CubicBezierCurve(points: points)
let y: CGFloat = 691.87778055040201
let line = LineSegment(p0: CGPoint(x: 0, y: y), p1: CGPoint(x: 1, y: y))
let r = Utils.roots(points: points, line: line)
let filtered = r.filter { $0 >= 0 && $0 <= 1 }
XCTAssertEqual(curve.compute(CGFloat(filtered[0])).y, y, accuracy: CGFloat(1.0e-5))
}

}
8 changes: 4 additions & 4 deletions BezierKit/Library/PathComponent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -402,11 +402,11 @@ import Foundation
let d0 = p1.y - p0.y
let d1 = p2.y - p1.y
var last: CGFloat = 0.0
Utils.droots(d0, d1, callback: { t in
Utils.droots(d0, d1) { t in
guard t > 0, t < 1 else { return }
callback(curve.split(from: last, to: t))
last = t
})
}
if last < 1.0 {
callback(curve.split(from: last, to: 1.0))
}
Expand All @@ -422,11 +422,11 @@ import Foundation
let d1 = p2.y - p1.y
let d2 = p3.y - p2.y
var last: CGFloat = 0.0
Utils.droots(d0, d1, d2, callback: { t in
Utils.droots(d0, d1, d2) { t in
guard t > 0, t < 1 else { return }
callback(curve.split(from: last, to: t))
last = t
})
}
if last < 1.0 {
callback(curve.split(from: last, to: 1.0))
}
Expand Down
2 changes: 1 addition & 1 deletion BezierKit/Library/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ internal class Utils {
let temp2 = 3*pb
let temp3 = -3*pc
let d = temp1 + temp2 + temp3 + pd
let smallValue = 1.0e-10
let smallValue = 1.0e-8
if abs(d) < smallValue {
let temp1 = 3*points[0]
let temp2 = -6*points[1]
Expand Down

0 comments on commit 80f01d6

Please sign in to comment.