1
1
mirror of https://github.com/exyte/Macaw.git synced 2024-10-26 04:49:57 +03:00

Override toPath() for Arc. Darwin is needed for sin/cos.

This commit is contained in:
Eduard Sergeev 2019-03-14 11:43:36 +07:00
parent 65b658dd39
commit 6bebe3ba70
3 changed files with 29 additions and 29 deletions

View File

@ -1,3 +1,5 @@
import Darwin
open class Arc: Locus {
public let ellipse: Ellipse
@ -17,4 +19,30 @@ open class Arc: Locus {
w: ellipse.rx * 2.0,
h: ellipse.ry * 2.0)
}
override open func toPath() -> Path {
let rx = ellipse.rx
let ry = ellipse.ry
let cx = ellipse.cx
let cy = ellipse.cy
var delta = extent
if shift == 0.0 && abs(extent - .pi * 2.0) < 0.00001 {
delta = .pi * 2.0 - 0.001
}
let theta1 = shift
let theta2 = theta1 + delta
let x1 = cx + rx * cos(theta1)
let y1 = cy + ry * sin(theta1)
let x2 = cx + rx * cos(theta2)
let y2 = cy + ry * sin(theta2)
let largeArcFlag = abs(delta) > .pi ? true : false
let sweepFlag = delta > 0.0 ? true : false
return PathBuilder(segment: PathSegment(type: .M, data: [x1, y1])).A(rx, ry, 0.0, largeArcFlag, sweepFlag, x2, y2).build()
}
}

View File

@ -2,32 +2,6 @@ import Foundation
extension Locus {
internal func arcToPath(_ arc: Arc) -> Path {
let rx = arc.ellipse.rx
let ry = arc.ellipse.ry
let cx = arc.ellipse.cx
let cy = arc.ellipse.cy
var delta = arc.extent
if arc.shift == 0.0 && abs(arc.extent - .pi * 2.0) < 0.00001 {
delta = .pi * 2.0 - 0.001
}
let theta1 = arc.shift
let theta2 = theta1 + delta
let x1 = cx + rx * cos(theta1)
let y1 = cy + ry * sin(theta1)
let x2 = cx + rx * cos(theta2)
let y2 = cy + ry * sin(theta2)
let largeArcFlag = abs(delta) > .pi ? true : false
let sweepFlag = delta > 0.0 ? true : false
return PathBuilder(segment: PathSegment(type: .M, data: [x1, y1])).A(rx, ry, 0.0, largeArcFlag, sweepFlag, x2, y2).build()
}
internal func pointToPath(_ point: Point) -> Path {
return MoveTo(x: point.x, y: point.y).lineTo(x: point.x, y: point.y).build()
}

View File

@ -20,9 +20,7 @@ open class Locus {
}
open func toPath() -> Path {
if let arc = self as? Arc {
return arcToPath(arc)
} else if let point = self as? Point {
if let point = self as? Point {
return MoveTo(x: point.x, y: point.y).lineTo(x: point.x, y: point.y).build()
} else if let line = self as? Line {
return MoveTo(x: line.x1, y: line.y1).lineTo(x: line.x2, y: line.y2).build()