Skip to content

Instantly share code, notes, and snippets.

@PaulWoodIII
Last active August 24, 2019 15:07
Show Gist options
  • Select an option

  • Save PaulWoodIII/492c1c0e25ead00d65e37ca69fc9a776 to your computer and use it in GitHub Desktop.

Select an option

Save PaulWoodIII/492c1c0e25ead00d65e37ca69fc9a776 to your computer and use it in GitHub Desktop.
import SwiftUI
struct TriangleView: View {
var body: some View {
GeometryReader { proxy in
return ZStack {
Path { path in
let fromCenter = min(proxy.size.width, proxy.size.height) * 0.67
let center = CGPoint(x: proxy.size.width / 2,
y: proxy.size.width / 2)
let top = CGPoint(x: center.x, y: center.y - fromCenter / 2.0)
let angle: CGFloat = 120.0
let bottomRight = top.rotate(around: center, with: angle)
let bottomLeft = top.rotate(around: center, with: -angle)
path.addLines([
top,
bottomRight,
bottomLeft,
top,
])
}.stroke().fill(Color.white)
}
}
}
}
// SO: https://stackoverflow.com/a/40752728/283460
extension CGPoint {
func rotate(around center: CGPoint, with degrees: CGFloat) -> CGPoint {
let dx = self.x - center.x
let dy = self.y - center.y
let radius = sqrt(dx * dx + dy * dy)
let azimuth = atan2(dy, dx) // in radians
let newAzimuth = azimuth + degrees * CGFloat(.pi / 180.0) // convert it to radians
let x = center.x + radius * cos(newAzimuth)
let y = center.y + radius * sin(newAzimuth)
return CGPoint(x: x, y: y)
}
}
struct TriangleView_Previews: PreviewProvider {
static var previews: some View {
TriangleView()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment