Skip to content

Commit 3f470d7

Browse files
authored
Merge pull request #63 from swhitty/fix-quadratic-curve
fix quadratic curve
2 parents e85abb3 + ce8c6e5 commit 3f470d7

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

SwiftDraw/LayerTree.Builder.Path.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,8 @@ extension LayerTree.Builder {
182182
let cp1 = Point(origin.x + (controlPoint.x - origin.x) * ratio,
183183
origin.y + (controlPoint.y - origin.y) * ratio)
184184

185-
let cpX = (final.x - origin.x)*Float(1.0/3.0)
186-
187-
let cp2 = Point(cp1.x + cpX,
188-
cp1.y)
185+
let cp2 = Point(final.x + (controlPoint.x - final.x) * ratio,
186+
final.y + (controlPoint.y - final.y) * ratio)
189187

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

202200
let final = space == .absolute ? Point(x, y) : Point(x, y).absolute(from: point)
203201
let cpX = (final.x - point.x)*Float(1.0/3.0)
202+
let cpY = (final.y - point.y)*Float(1.0/3.0)
203+
204204
let cp2 = Point(cp1.x + cpX,
205-
cp1.y)
205+
cp1.y + cpY)
206206

207207
return .cubic(to: final, control1: cp1, control2: cp2)
208208
}

SwiftDrawTests/LayerTree.PathTests.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,13 +181,13 @@ final class LayerTreePathTests: XCTestCase {
181181
x: 300, y: 50, space: .absolute)
182182
var c = LayerTree.Builder.createQuadratic(from: quad, last: Point(0, 50))
183183

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

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

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

209-
XCTAssertEqual(segment, .cubic(to: .init(20.0, 20.0), control1: .init(10, 10), control2: .init(13.333334, 10)))
209+
210+
XCTAssertEqual(segment, .cubic(to: .init(20.0, 20.0), control1: .init(10, 10), control2: .init(13.333334, 13.333334)))
210211
}
211212

212213
func testDOMCubicSmooth() {

0 commit comments

Comments
 (0)