Skip to content

Commit

Permalink
Maker Path <-> BezeriPath easier
Browse files Browse the repository at this point in the history
  • Loading branch information
emorydunn committed Feb 21, 2021
1 parent 6ce26ec commit f930a86
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 57 deletions.
6 changes: 5 additions & 1 deletion Sources/SwiftGraphics/Shapes/BezierPath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,12 @@ public class BezierPath: Shape {
}

/// Instantiate a new Bezier from a `Path`
///
/// From: [Smooth a Svg path with cubic bezier curves][bezier]
///
/// [bezier]: https://medium.com/@francoisromain/smooth-a-svg-path-with-cubic-bezier-curves-e37b49d46c74
/// - Parameter path: The path to transform into a Bézier curve
public init(path: Path, smoothing: Double = 0.2) {
public init(_ path: Path, smoothing: Double = 0.2) {
self.start = path.points[0]

var bezPoints = path.points
Expand Down
61 changes: 8 additions & 53 deletions Sources/SwiftGraphics/Shapes/Path.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,6 @@ import AppKit
/// Represents a multi-point path
public class Path: Shape {

/// Drawing style of the Path
@available(*, deprecated)
public enum Style {

/// Draw straight lines connecting each point
case sharp

/// Draw a cubic Bézier curve between points
case smooth
}

/// A Rectangle that contains the receiver
public var boundingBox: Rectangle {
// FIXME: This needs to be implemented
Expand All @@ -31,9 +20,6 @@ public class Path: Shape {
/// Points that make up the path
public var points: [Vector]

// /// Drawing style of the Path
// public var style: Style = .smooth

/// Whether to close the path
///
/// This is done by appending the first point to the end of `points` while drawing
Expand All @@ -50,6 +36,12 @@ public class Path: Shape {
public convenience init(point: Vector) {
self.init(points: [point])
}

/// Instantiate a new Path from a Bézier curve
/// - Parameter bezier: The curve it convert to sharp lines
public convenience init(_ bezier: BezierPath) {
self.init(points: bezier.points.map(\.point))
}

/// Append a point to the path
/// - Parameter point: New Vector
Expand All @@ -74,31 +66,15 @@ public class Path: Shape {
/// From: [Smooth a Svg path with cubic bezier curves][bezier]
///
/// [bezier]: https://medium.com/@francoisromain/smooth-a-svg-path-with-cubic-bezier-curves-e37b49d46c74
@available(*, deprecated, message: "Use BezierPath(_:smoothing:) instead.")
public func smoothLine() -> BezierPath {
return BezierPath(path: self)
return BezierPath(self)
}

}

extension Path: CGDrawable {

// /// Create a `NSBezierPath` drawn with straight lines
// func sharpLine() -> NSBezierPath {
// let path = NSBezierPath()
//
// path.move(to: CGPoint(x: points[0].x, y: points[0].y))
//
// points.forEach { point in
// path.line(to: point.nsPoint())
// }
//
// if close {
// path.line(to: CGPoint(x: points[0].x, y: points[0].y))
// }
//
// return path
// }

/// Draw the receiver in the specified context
/// - Parameter context: Context in which to draw
public func draw(in context: CGContext) {
Expand Down Expand Up @@ -135,27 +111,6 @@ extension Path: CGDrawable {

extension Path: SVGDrawable {

// /// Create a `XMLElement` drawn with straight lines
// func sharpLine() -> XMLElement {
// let element = XMLElement(name: "path")
//
// let lines = points.map({
// "L \($0.x) \($0.y)"
// }).joined(separator: " ")
//
// let command = "M \(points[0].x),\(points[0].y) \(lines)"
//
// element.addAttribute(command, forKey: "d")
//
// return element
// }

// /// Create a `XMLElement` drawn with a Bézier curve
// func smoothLine() -> XMLElement {
// let bezPath: BezierPath = smoothLine()
// return bezPath.svgElement()
// }

/// Create an `XMLElement` for the Path in its drawing style
public func svgElement() -> XMLElement {
let element = XMLElement(name: "path")
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftGraphics/Shapes/Vector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public class Vector: Shape {
/// - previous: The previous point in the path
/// - next: The next point in the path
/// - reverse: Whether to rotate the control point 180º
func controlPoint(previous: Vector?, next: Vector?, reverse: Bool = false, smoothing: Double = 0.2) -> Vector {
func controlPoint(previous: Vector?, next: Vector?, reverse: Bool = false, smoothing: Double) -> Vector {
// When 'current' is the first or last point of the array
// 'previous' or 'next' don't exist.
// Replace with 'current'
Expand Down
2 changes: 1 addition & 1 deletion Tests/SwiftGraphicsTests/SVGTests/SVGDrawableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ final class SVGDrawableTests: XCTestCase {
func testBezierPath() {
setTestContext()

let path = BezierPath(path: Path(points: [Vector(100.0, 100.0), Vector(200.0, 200.0), Vector(300.0, 100.0)]))
let path = BezierPath(Path(points: [Vector(100.0, 100.0), Vector(200.0, 200.0), Vector(300.0, 100.0)]))

let smoothXML: XMLElement = path.svgElement()
XCTAssertEqual(
Expand Down
2 changes: 1 addition & 1 deletion Tests/SwiftGraphicsTests/Shape tests/PathTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ final class PathTests: XCTestCase {
func testSmoothLine() {
let path = Path(points: [Vector(100.0, 100.0), Vector(200.0, 200.0), Vector(300.0, 100.0)])

let bezier: [BezierPath.Point] = path.smoothLine().points
let bezier: [BezierPath.Point] = BezierPath(path).points

XCTAssertEqual(
bezier,
Expand Down

0 comments on commit f930a86

Please sign in to comment.