-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathParallelogram.swift
44 lines (36 loc) · 1.32 KB
/
Parallelogram.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//
// Parallelogram.swift
//
//
// Created by phimage on 17/07/2019.
//
import SwiftUI
/// Create a parallelogram shape.
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
public struct Parallelogram: Shape {
public var topLeftAngle: Angle
public init(topLeftAngle: Angle) {
self.topLeftAngle = topLeftAngle
}
/// Describes this shape as a path within a rectangular frame of reference.
///
/// - Parameter rect: The frame of reference for describing this shape.
/// - Returns: A path that describes this shape.
public func path(in rect: CGRect) -> Path {
var path = Path()
let offset = abs(CGFloat(tan(topLeftAngle.radians - .pi / 2)) * rect.height)
if topLeftAngle.degrees <= 90 {
path.move(to: CGPoint(x: 0, y: 0))
path.addLine(to: CGPoint(x: rect.width - offset, y: 0))
path.addLine(to: CGPoint(x: rect.width, y: rect.height))
path.addLine(to: CGPoint(x: offset, y: rect.height))
} else {
path.move(to: CGPoint(x: offset, y: 0))
path.addLine(to: CGPoint(x: rect.width, y: 0))
path.addLine(to: CGPoint(x: rect.width - offset, y: rect.height))
path.addLine(to: CGPoint(x: 0, y: rect.height))
}
path.closeSubpath()
return path
}
}