|
| 1 | +// |
| 2 | +// Path+LinesAndShapes.swift |
| 3 | +// |
| 4 | +// Created by Adam Fordyce on 23/08/2021. |
| 5 | +// Copyright © 2021 Adam Fordyce. All rights reserved. |
| 6 | +// |
| 7 | + |
| 8 | +import SwiftUI |
| 9 | + |
| 10 | +public typealias PointAndRadius = (point: CGPoint, radius: CGFloat) |
| 11 | + |
| 12 | +public extension Path { |
| 13 | + |
| 14 | + mutating func lines(_ points: CGPoint..., cornerRadius: CGFloat = 0) { |
| 15 | + lines(points, cornerRadius: cornerRadius) |
| 16 | + } |
| 17 | + |
| 18 | + mutating func lines(_ points: [CGPoint], cornerRadius: CGFloat = 0) { |
| 19 | + |
| 20 | + for index in 0..<(points.count - 1) { |
| 21 | + arc(points[index], points[index + 1], radius: cornerRadius) |
| 22 | + } |
| 23 | + line(points.last!) |
| 24 | + } |
| 25 | + |
| 26 | + mutating func lines(_ points: PointAndRadius...) { |
| 27 | + lines(points) |
| 28 | + } |
| 29 | + |
| 30 | + mutating func lines(_ points: [PointAndRadius]) { |
| 31 | + guard points.count > 0 else { return } |
| 32 | + |
| 33 | + for index in 0..<(points.count - 1) { |
| 34 | + arc(points[index].point, points[index + 1].point, radius: points[index].radius) |
| 35 | + } |
| 36 | + line(points.last?.point ?? .zero) |
| 37 | + } |
| 38 | + |
| 39 | + mutating func shape(_ points: CGPoint..., cornerRadius: CGFloat = 0) { |
| 40 | + shape(points, cornerRadius: cornerRadius) |
| 41 | + } |
| 42 | + |
| 43 | + mutating func shape(_ points: [CGPoint], cornerRadius: CGFloat = 0) { |
| 44 | + |
| 45 | + guard points.count > 0 else { return } |
| 46 | + |
| 47 | + if points.count == 1 { |
| 48 | + line(points.first!) |
| 49 | + } else { |
| 50 | + let startingPoint = points.first!.to(points.last!, 0.5) |
| 51 | + |
| 52 | + move(startingPoint) |
| 53 | + |
| 54 | + for index in 0..<(points.count - 1) { |
| 55 | + arc(points[index], points[index + 1], radius: cornerRadius) |
| 56 | + } |
| 57 | + arc(points.last!, startingPoint, radius: cornerRadius) |
| 58 | + line(startingPoint) |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + mutating func shape(_ points: PointAndRadius...) { |
| 63 | + shape(points) |
| 64 | + } |
| 65 | + |
| 66 | + mutating func shape(_ points: [PointAndRadius]) { |
| 67 | + guard points.count > 0 else { return } |
| 68 | + |
| 69 | + if points.count == 1 { |
| 70 | + line(points.first!.point) |
| 71 | + } else { |
| 72 | + |
| 73 | + let startingPoint = points.first!.point.to(points.last!.point, 0.5) |
| 74 | + |
| 75 | + move(startingPoint) |
| 76 | + |
| 77 | + for index in 0..<(points.count - 1) { |
| 78 | + arc(points[index].point, points[index + 1].point, radius: points[index].radius) |
| 79 | + } |
| 80 | + arc(points.last!.point, startingPoint, radius: points.last!.radius) |
| 81 | + line(startingPoint) |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments