Skip to content

Commit 77ecb73

Browse files
committed
rounded regular polygon struct
1 parent 78c1101 commit 77ecb73

File tree

12 files changed

+155
-335
lines changed

12 files changed

+155
-335
lines changed

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version:5.1
1+
// swift-tools-version:5.3
22
// The swift-tools-version declares the minimum version of Swift required to build this package.
33

44
import PackageDescription

README.md

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ Collection of custom shapes
77
</center>
88

99
```swift
10-
Pentagon()
11-
Hexagon()
1210
RegularPolygon(sides: 32)
11+
RoundedRegularPolygon(sides: 6, radius: 20)
1312
```
1413

1514
## Lines and Curves
@@ -42,18 +41,10 @@ GridPattern(horizontalLines: 20, verticalLines: 40)
4241

4342
## How to use
4443

45-
Add this swift package to your project
44+
Add this swift package to your project with SPM
4645
```
47-
git@github.com:swift-extensions/swiftui-shapes.git
46+
git@github.com:spacenation/swiftui-shapes.git
4847
```
4948

50-
For more examples open `/Examples/ShapesExamples.xcodeproj`
51-
52-
## Roadmap
53-
- Rounded regular polygons
54-
5549
## Code Contributions
5650
Feel free to contribute via fork/pull request to master branch. If you want to request a feature or report a bug please start a new issue.
57-
58-
## Coffee Contributions
59-
If you find this project useful please consider becoming my GitHub sponsor.

Sources/Shapes/RegularPolygons/Decagon.swift

Lines changed: 0 additions & 37 deletions
This file was deleted.

Sources/Shapes/RegularPolygons/Heptagon.swift

Lines changed: 0 additions & 37 deletions
This file was deleted.

Sources/Shapes/RegularPolygons/Hexagon.swift

Lines changed: 0 additions & 37 deletions
This file was deleted.

Sources/Shapes/RegularPolygons/Nonagon.swift

Lines changed: 0 additions & 37 deletions
This file was deleted.

Sources/Shapes/RegularPolygons/Octagon.swift

Lines changed: 0 additions & 37 deletions
This file was deleted.

Sources/Shapes/RegularPolygons/Pentagon.swift

Lines changed: 0 additions & 37 deletions
This file was deleted.
Lines changed: 18 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,43 @@
11
import SwiftUI
22

3-
public struct RegularPolygon: InsettableShape {
4-
let sides: Int
5-
let inset: CGFloat
6-
let radius: CGFloat
7-
8-
public func inset(by amount: CGFloat) -> RegularPolygon {
9-
RegularPolygon(sides: self.sides, inset: self.inset + amount, radius: radius)
3+
public struct RegularPolygon: Shape {
4+
let sides: UInt
5+
private let inset: CGFloat
6+
7+
public func path(in rect: CGRect) -> Path {
8+
Path.regularPolygon(sides: self.sides, in: rect, inset: inset)
109
}
1110

12-
public func path(in rect: CGRect) -> Path {
13-
Path.regularPolygon(sides: self.sides, in: rect, inset: inset, radius: radius)
11+
public init(sides: UInt) {
12+
self.init(sides: sides, inset: 0)
1413
}
1514

16-
public init(sides: Int, radius: CGFloat = 0) {
15+
init(sides: UInt, inset: CGFloat) {
1716
self.sides = sides
18-
self.inset = 0
19-
self.radius = radius
20-
}
21-
22-
public init(sides: Double, radius: CGFloat = 0) {
23-
self.sides = Int(sides.rounded(.down))
24-
self.inset = 0
25-
self.radius = radius
17+
self.inset = inset
2618
}
2719
}
2820

29-
extension RegularPolygon {
30-
init(sides: Int, inset: CGFloat, radius: CGFloat = 0) {
31-
self.sides = sides
32-
self.inset = inset
33-
self.radius = radius
21+
extension RegularPolygon: InsettableShape {
22+
public func inset(by amount: CGFloat) -> RegularPolygon {
23+
RegularPolygon(sides: self.sides, inset: self.inset + amount)
3424
}
3525
}
3626

3727
struct RegularPolygon_Previews: PreviewProvider {
3828
static var previews: some View {
3929
Group {
40-
RegularPolygon(sides: 4, radius: 5)
30+
RegularPolygon(sides: 4)
4131
.strokeBorder(lineWidth: 20)
4232
.foregroundColor(.blue)
4333

44-
Pentagon(radius: 5)
34+
RegularPolygon(sides: 6)
4535
.strokeBorder(lineWidth: 20)
46-
.foregroundColor(.yellow)
47-
48-
Hexagon()
49-
.foregroundColor(.orange)
50-
51-
Heptagon(radius: 5)
52-
.foregroundColor(.blue)
53-
54-
Octagon()
55-
.foregroundColor(.pink)
56-
57-
Nonagon()
5836
.foregroundColor(.red)
5937

60-
Decagon()
61-
.foregroundColor(.green)
38+
RegularPolygon(sides: 16)
39+
.strokeBorder(lineWidth: 10)
40+
.foregroundColor(.purple)
6241
}
6342
}
6443
}

0 commit comments

Comments
 (0)