Skip to content

fix quadratic curve #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions SwiftDraw/LayerTree.Builder.Path.swift
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,8 @@ extension LayerTree.Builder {
let cp1 = Point(origin.x + (controlPoint.x - origin.x) * ratio,
origin.y + (controlPoint.y - origin.y) * ratio)

let cpX = (final.x - origin.x)*Float(1.0/3.0)

let cp2 = Point(cp1.x + cpX,
cp1.y)
let cp2 = Point(final.x + (controlPoint.x - final.x) * ratio,
final.y + (controlPoint.y - final.y) * ratio)

return .cubic(to: final, control1: cp1, control2: cp2)
}
Expand All @@ -201,8 +199,10 @@ extension LayerTree.Builder {

let final = space == .absolute ? Point(x, y) : Point(x, y).absolute(from: point)
let cpX = (final.x - point.x)*Float(1.0/3.0)
let cpY = (final.y - point.y)*Float(1.0/3.0)

let cp2 = Point(cp1.x + cpX,
cp1.y)
cp1.y + cpY)

return .cubic(to: final, control1: cp1, control2: cp2)
}
Expand Down
7 changes: 4 additions & 3 deletions SwiftDrawTests/LayerTree.PathTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,13 @@ final class LayerTreePathTests: XCTestCase {
x: 300, y: 50, space: .absolute)
var c = LayerTree.Builder.createQuadratic(from: quad, last: Point(0, 50))

XCTAssertEqual(c, cubic(300, 50, 100*twoThirds, 16.6666641, 100*twoThirds+150*twoThirds, 16.6666641))
XCTAssertEqual(c, cubic(300, 50, 66.66667, 16.666664, 166.66666, 16.666664))

//quad with control point to the right
quad = .quadratic(x1: 200, y1: 0,
x: 300, y: 50, space: .absolute)
c = LayerTree.Builder.createQuadratic(from: quad, last: Point(0, 50))
XCTAssertEqual(c, cubic(300, 50, 200*twoThirds, 16.6666641, 200*twoThirds+150*twoThirds, 16.6666641))
XCTAssertEqual(c, cubic(300, 50, 133.33334, 16.666664, 233.33333, 16.666664))
}

func testQuadraticSmoothAbsolute() {
Expand All @@ -206,7 +206,8 @@ final class LayerTreePathTests: XCTestCase {
let domSegment = DOM.Path.Segment.quadraticSmooth(x: 10.0, y: 10.0, space: .relative)
let segment = LayerTree.Builder.makeSegment(from: domSegment, last: .init(10, 10), previous: nil)

XCTAssertEqual(segment, .cubic(to: .init(20.0, 20.0), control1: .init(10, 10), control2: .init(13.333334, 10)))

XCTAssertEqual(segment, .cubic(to: .init(20.0, 20.0), control1: .init(10, 10), control2: .init(13.333334, 13.333334)))
}

func testDOMCubicSmooth() {
Expand Down