-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from spacenation/rounded-regular-polygons
Rounded polygons
- Loading branch information
Showing
11 changed files
with
397 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import SwiftUI | ||
|
||
public extension Path { | ||
mutating func addCircularCornerRadiusArc(from point: CGPoint, via viaPoint: CGPoint, to nextPoint: CGPoint, radius: CGFloat, clockwise: Bool) { | ||
let lineAngle = atan2(viaPoint.y - point.y, viaPoint.x - point.x) | ||
let nextLineAngle = atan2(nextPoint.y - viaPoint.y, nextPoint.x - viaPoint.x) | ||
|
||
let lineVector = CGVector(dx: -sin(lineAngle) * radius, dy: cos(lineAngle) * radius) | ||
let nextLineVector = CGVector(dx: -sin(nextLineAngle) * radius, dy: cos(nextLineAngle) * radius) | ||
|
||
let offsetStart1 = CGPoint(x: point.x + lineVector.dx, y: point.y + lineVector.dy) | ||
let offsetEnd1 = CGPoint(x: viaPoint.x + lineVector.dx, y: viaPoint.y + lineVector.dy) | ||
|
||
let offsetStart2 = CGPoint(x: viaPoint.x + nextLineVector.dx, y: viaPoint.y + nextLineVector.dy) | ||
|
||
let offsetEnd2 = CGPoint(x: nextPoint.x + nextLineVector.dx, y: nextPoint.y + nextLineVector.dy) | ||
|
||
let intersection = CGPoint.intersection(start1: offsetStart1, end1: offsetEnd1, start2: offsetStart2, end2: offsetEnd2) | ||
|
||
let startAngle = lineAngle - (.pi / 2) | ||
let endAngle = nextLineAngle - (.pi / 2) | ||
|
||
self.addArc( | ||
center: intersection, | ||
radius: radius, | ||
startAngle: Angle(radians: startAngle), | ||
endAngle: Angle(radians:endAngle), | ||
clockwise: clockwise | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import SwiftUI | ||
|
||
struct Triangle: Shape { | ||
let radius: CGFloat | ||
|
||
func path(in rect: CGRect) -> Path { | ||
let width = rect.width | ||
let height = rect.height | ||
let sides = 3 | ||
let hypotenuse = Double(min(width, height)) / 2.0 | ||
let centerPoint = CGPoint(x: rect.midX, y: rect.midY) | ||
var usableRadius: CGFloat = .zero | ||
|
||
return Path { path in | ||
guard sides > 2 else { return } | ||
(0..<sides).forEach { index in | ||
|
||
let angle = ((Double(index) * (360.0 / Double(sides))) - 90) * Double.pi / 180 | ||
|
||
let point = CGPoint( | ||
x: centerPoint.x + CGFloat(cos(angle) * hypotenuse), | ||
y: centerPoint.y + CGFloat(sin(angle) * hypotenuse) | ||
) | ||
|
||
let viaAngle = ((Double(index + 1) * (360.0 / Double(sides))) - 90) * Double.pi / 180 | ||
|
||
let viaPoint = CGPoint( | ||
x: centerPoint.x + CGFloat(cos(viaAngle) * hypotenuse), | ||
y: centerPoint.y + CGFloat(sin(viaAngle) * hypotenuse) | ||
) | ||
|
||
if usableRadius == 0 { | ||
let sideLength = sqrt((point.x - viaPoint.x) * (point.x - viaPoint.x) + (point.y - viaPoint.y) * (point.y - viaPoint.y)) | ||
let inradius = sideLength / (2 * tan(.pi / CGFloat(sides))) | ||
|
||
usableRadius = min(radius, inradius) | ||
} | ||
|
||
let nextAngle = ((Double(index + 2) * (360.0 / Double(sides))) - 90) * Double.pi / 180 | ||
|
||
let nextPoint = CGPoint( | ||
x: centerPoint.x + CGFloat(cos(nextAngle) * hypotenuse), | ||
y: centerPoint.y + CGFloat(sin(nextAngle) * hypotenuse) | ||
) | ||
|
||
path.addCircularCornerRadiusArc(from: point, via: viaPoint, to: nextPoint, radius: usableRadius, clockwise: false) | ||
} | ||
path.closeSubpath() | ||
} | ||
} | ||
} | ||
|
||
struct Triangle_Previews: PreviewProvider { | ||
static var previews: some View { | ||
Triangle(radius: 30) | ||
.stroke(lineWidth: 3) | ||
.foregroundColor(.blue) | ||
.background(Circle()) | ||
.animation(.linear) | ||
.previewLayout(.fixed(width: 200, height: 200)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 0 additions & 84 deletions
84
Sources/Shapes/RoundedRegularPolygons/RoundedRegularPolygonPath.swift
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.