|
| 1 | +// The MIT License (MIT) |
| 2 | +// Copyright © 2022 Sparrow Code (hello@sparrowcode.io) |
| 3 | +// |
| 4 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +// of this software and associated documentation files (the "Software"), to deal |
| 6 | +// in the Software without restriction, including without limitation the rights |
| 7 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | +// copies of the Software, and to permit persons to whom the Software is |
| 9 | +// furnished to do so, subject to the following conditions: |
| 10 | +// |
| 11 | +// The above copyright notice and this permission notice shall be included in all |
| 12 | +// copies or substantial portions of the Software. |
| 13 | +// |
| 14 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | +// SOFTWARE. |
| 21 | + |
| 22 | + |
| 23 | +import Foundation |
| 24 | +import CoreGraphics |
| 25 | + |
| 26 | +struct SPQRCorner: Equatable { |
| 27 | + enum Kind: Int, CaseIterable { |
| 28 | + case topLeft = 0 |
| 29 | + case bottomLeft = 1 |
| 30 | + case bottomRight = 2 |
| 31 | + case topRight = 3 |
| 32 | + |
| 33 | + var verticalNeighbor: Kind { |
| 34 | + switch self { |
| 35 | + case .topLeft: |
| 36 | + return .bottomLeft |
| 37 | + case .bottomLeft: |
| 38 | + return .topLeft |
| 39 | + case .bottomRight: |
| 40 | + return .topRight |
| 41 | + case .topRight: |
| 42 | + return .bottomRight |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + var horizontalNeighbor: Kind { |
| 47 | + switch self { |
| 48 | + case .topLeft: |
| 49 | + return .topRight |
| 50 | + case .bottomLeft: |
| 51 | + return .bottomRight |
| 52 | + case .bottomRight: |
| 53 | + return .bottomLeft |
| 54 | + case .topRight: |
| 55 | + return .topLeft |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + let kind: Kind |
| 61 | + let point: CGPoint |
| 62 | + |
| 63 | + let length: CGFloat |
| 64 | + let radius: CGFloat |
| 65 | + |
| 66 | + func startPoint(using corners: [SPQRCorner]) -> CGPoint? { |
| 67 | + guard let neighbor = corners.first(where: { self.kind.verticalNeighbor == $0.kind }) else { |
| 68 | + return nil |
| 69 | + } |
| 70 | + |
| 71 | + return pointOnLine( |
| 72 | + startPoint: point, |
| 73 | + endPoint: neighbor.point, |
| 74 | + distance: (length + radius) |
| 75 | + ) |
| 76 | + } |
| 77 | + |
| 78 | + func preCurvePoint(using corners: [SPQRCorner]) -> CGPoint? { |
| 79 | + guard let neighbor = corners.first(where: { self.kind.verticalNeighbor == $0.kind }) else { |
| 80 | + return nil |
| 81 | + } |
| 82 | + |
| 83 | + return pointOnLine( |
| 84 | + startPoint: point, |
| 85 | + endPoint: neighbor.point, |
| 86 | + distance: radius |
| 87 | + ) |
| 88 | + } |
| 89 | + |
| 90 | + func postCurvePoint(using corners: [SPQRCorner]) -> CGPoint? { |
| 91 | + guard let neighbor = corners.first(where: { self.kind.horizontalNeighbor == $0.kind }) else { |
| 92 | + return nil |
| 93 | + } |
| 94 | + |
| 95 | + return pointOnLine( |
| 96 | + startPoint: point, |
| 97 | + endPoint: neighbor.point, |
| 98 | + distance: radius |
| 99 | + ) |
| 100 | + } |
| 101 | + |
| 102 | + func endPoint(using corners: [SPQRCorner]) -> CGPoint? { |
| 103 | + guard let neighbor = corners.first(where: { self.kind.horizontalNeighbor == $0.kind }) else { |
| 104 | + return nil |
| 105 | + } |
| 106 | + |
| 107 | + return pointOnLine( |
| 108 | + startPoint: point, |
| 109 | + endPoint: neighbor.point, |
| 110 | + distance: (length + radius) |
| 111 | + ) |
| 112 | + } |
| 113 | + |
| 114 | + private func pointOnLine(startPoint: CGPoint, endPoint: CGPoint, distance: CGFloat = 0.0) -> CGPoint { |
| 115 | + let lDistance = endPoint.distance(from: startPoint) |
| 116 | + let vector = CGPoint( |
| 117 | + x: (endPoint.x - startPoint.x) / lDistance, |
| 118 | + y: (endPoint.y - startPoint.y) / lDistance |
| 119 | + ) |
| 120 | + |
| 121 | + return .init( |
| 122 | + x: startPoint.x + distance * vector.x, |
| 123 | + y: startPoint.y + distance * vector.y |
| 124 | + ) |
| 125 | + } |
| 126 | +} |
0 commit comments