-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPolygon.swift
48 lines (39 loc) · 1.25 KB
/
Polygon.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
45
46
47
48
//
// Polygon.swift
//
//
// Created by phimage on 17/07/2019.
//
import SwiftUI
/// Create a Polygon shape.
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
public struct Polygon: Shape {
/// Number of polygon side
public let sides: Int
/**
Create a polygon shape with provided number of sides.
- Parameter sides: number of sides
*/
public init(sides: Int) {
self.sides = sides
}
/// 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 center = CGPoint(x: rect.width / 2.0, y: rect.height / 2.0)
var angle: CGFloat = -.pi / 2
let angleIncrement = .pi * 2 / CGFloat(sides)
let length = min(rect.width, rect.height)
let radius = length / 2.0
path.move(to: point(from: angle, radius: radius, offset: center))
for _ in 1...sides - 1 {
angle += angleIncrement
path.addLine(to: point(from: angle, radius: radius, offset: center))
}
path.closeSubpath()
return path
}
}