Skip to content

Commit

Permalink
fix case of single cubic curve closed path including unit test.
Browse files Browse the repository at this point in the history
  • Loading branch information
hfutrell committed Oct 21, 2024
1 parent 099bb1c commit 725d79b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
10 changes: 10 additions & 0 deletions BezierKit/BezierKitTests/Path+VectorBooleanTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,16 @@ class PathVectorBooleanTests: XCTestCase {
XCTAssertTrue(componentsEqualAsideFromElementOrdering(result.components[0], square.components[0]))
}

func testCrossingsRemovedSingleCurveLoop() {
let cgPath = CGMutablePath()
cgPath.move(to: CGPoint(x: 0, y: 0))
cgPath.addCurve(to: CGPoint(x: 0, y: 0),
control1: CGPoint(x: -1, y: 1),
control2: CGPoint(x: 1, y: 1))
let path = Path(cgPath: cgPath)
XCTAssertEqual(path.crossingsRemoved(), path)
}

func testCrossingsRemovedEdgeCase() {
// this is an edge cases which caused difficulty in practice
// the contour, which intersects at (1,1) creates two squares, one with -1 winding count
Expand Down
5 changes: 4 additions & 1 deletion BezierKit/Library/PathComponent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,10 @@ open class PathComponent: NSObject, Reversible, Transformable, @unchecked Sendab
if i1 == i2 {
// we are intersecting a path element against itself (only possible with cubic or higher order)
if self.order(at: i1) == 3 {
elementIntersections = self.cubic(at: i1).selfIntersections
elementIntersections = self.cubic(at: i1).selfIntersections.filter {
guard self.numberOfElements == 1 else { return true }
return $0.t1 != 0 || $0.t2 != 1 // exclude intersection of single curve path closing itself
}
}
} else if i1 < i2 {
// we are intersecting two distinct path elements
Expand Down

0 comments on commit 725d79b

Please sign in to comment.