Skip to content

Commit 42d2e54

Browse files
committed
removing triangle
1 parent 4218849 commit 42d2e54

File tree

2 files changed

+16
-69
lines changed

2 files changed

+16
-69
lines changed

Sources/Shapes/RoundedRegularPolygons/RoundedRegularPolygon.swift

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -26,55 +26,6 @@ extension RoundedRegularPolygon: InsettableShape {
2626
}
2727
}
2828

29-
extension Path {
30-
static func roundedRegularPolygon(sides: UInt, in rect: CGRect, inset: CGFloat = 0, radius: CGFloat = 0) -> Path {
31-
let rect = rect.insetBy(dx: inset, dy: inset)
32-
let width = rect.width
33-
let height = rect.height
34-
let hypotenuse = Double(min(width, height)) / 2.0
35-
let centerPoint = CGPoint(x: rect.midX, y: rect.midY)
36-
var usableRadius: CGFloat = .zero
37-
38-
return Path { path in
39-
guard sides > 2 else { return }
40-
(0..<sides).forEach { index in
41-
42-
let angle = ((Double(index) * (360.0 / Double(sides))) - 90) * Double.pi / 180
43-
44-
let point = CGPoint(
45-
x: centerPoint.x + CGFloat(cos(angle) * hypotenuse),
46-
y: centerPoint.y + CGFloat(sin(angle) * hypotenuse)
47-
)
48-
49-
let viaAngle = ((Double(index + 1) * (360.0 / Double(sides))) - 90) * Double.pi / 180
50-
51-
let viaPoint = CGPoint(
52-
x: centerPoint.x + CGFloat(cos(viaAngle) * hypotenuse),
53-
y: centerPoint.y + CGFloat(sin(viaAngle) * hypotenuse)
54-
)
55-
56-
if usableRadius == 0 {
57-
let sideLength = sqrt((point.x - viaPoint.x) * (point.x - viaPoint.x) + (point.y - viaPoint.y) * (point.y - viaPoint.y))
58-
let inradius = sideLength / (2 * tan(.pi / CGFloat(sides)))
59-
60-
usableRadius = min(radius, inradius)
61-
}
62-
63-
let nextAngle = ((Double(index + 2) * (360.0 / Double(sides))) - 90) * Double.pi / 180
64-
65-
let nextPoint = CGPoint(
66-
x: centerPoint.x + CGFloat(cos(nextAngle) * hypotenuse),
67-
y: centerPoint.y + CGFloat(sin(nextAngle) * hypotenuse)
68-
)
69-
70-
path.addCircularCornerRadiusArc(from: point, via: viaPoint, to: nextPoint, radius: usableRadius, clockwise: false)
71-
}
72-
path.closeSubpath()
73-
}
74-
}
75-
}
76-
77-
7829
struct RoundedRegularPolygon_Previews: PreviewProvider {
7930
static var previews: some View {
8031
RoundedRegularPolygon(sides: 3, radius: 30)
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import SwiftUI
22

3-
struct Triangle: Shape {
4-
let radius: CGFloat
5-
6-
func path(in rect: CGRect) -> Path {
3+
extension Path {
4+
static func roundedRegularPolygon(sides: UInt, in rect: CGRect, inset: CGFloat = 0, radius: CGFloat = 0) -> Path {
5+
let rect = rect.insetBy(dx: inset, dy: inset)
76
let width = rect.width
87
let height = rect.height
9-
let sides = 3
108
let hypotenuse = Double(min(width, height)) / 2.0
119
let centerPoint = CGPoint(x: rect.midX, y: rect.midY)
1210
var usableRadius: CGFloat = .zero
@@ -16,23 +14,32 @@ struct Triangle: Shape {
1614
(0..<sides).forEach { index in
1715

1816
let angle = ((Double(index) * (360.0 / Double(sides))) - 90) * Double.pi / 180
19-
17+
2018
let point = CGPoint(
2119
x: centerPoint.x + CGFloat(cos(angle) * hypotenuse),
2220
y: centerPoint.y + CGFloat(sin(angle) * hypotenuse)
2321
)
2422

2523
let viaAngle = ((Double(index + 1) * (360.0 / Double(sides))) - 90) * Double.pi / 180
26-
24+
2725
let viaPoint = CGPoint(
2826
x: centerPoint.x + CGFloat(cos(viaAngle) * hypotenuse),
2927
y: centerPoint.y + CGFloat(sin(viaAngle) * hypotenuse)
3028
)
3129

3230
if usableRadius == 0 {
33-
let sideLength = sqrt((point.x - viaPoint.x) * (point.x - viaPoint.x) + (point.y - viaPoint.y) * (point.y - viaPoint.y))
34-
let inradius = sideLength / (2 * tan(.pi / CGFloat(sides)))
31+
// Step 1: Calculate the differences in coordinates
32+
let deltaX = point.x - viaPoint.x
33+
let deltaY = point.y - viaPoint.y
34+
35+
// Step 2: Calculate the side length
36+
let sideLength = sqrt(deltaX * deltaX + deltaY * deltaY)
3537

38+
// Step 3: Calculate the inradius
39+
let angle = .pi / CGFloat(sides)
40+
let inradius = sideLength / (2 * tan(angle))
41+
42+
// Step 4: Determine the usable radius
3643
usableRadius = min(radius, inradius)
3744
}
3845

@@ -49,14 +56,3 @@ struct Triangle: Shape {
4956
}
5057
}
5158
}
52-
53-
struct Triangle_Previews: PreviewProvider {
54-
static var previews: some View {
55-
Triangle(radius: 30)
56-
.stroke(lineWidth: 3)
57-
.foregroundColor(.blue)
58-
.background(Circle())
59-
.animation(.linear)
60-
.previewLayout(.fixed(width: 200, height: 200))
61-
}
62-
}

0 commit comments

Comments
 (0)