Swiftui Polygon

[Solved] Swiftui Polygon | Swift - Code Explorer | yomemimo.com
Question : swiftui polygon

Answered by : tony-sacco

struct Polygon : Shape { var sides : Int = 5 func path(in rect : CGRect ) -> Path{ // get the center point and the radius let center = CGPoint(x: rect.width / 2, y: rect.height / 2) let radius = rect.width / 2 // get the angle in radian, // 2 pi divided by the number of sides let angle = Double.pi * 2 / Double(sides) var path = Path() var startPoint = CGPoint(x: 0, y: 0) for side in 0 ..< sides { let x = center.x + CGFloat(cos(Double(side) * angle)) * CGFloat (radius) let y = center.y + CGFloat(sin(Double(side) * angle)) * CGFloat(radius) let vertexPoint = CGPoint( x: x, y: y) if (side == 0) { startPoint = vertexPoint path.move(to: startPoint ) } else { path.addLine(to: vertexPoint) } // move back to starting point // needed for stroke if ( side == (sides - 1) ){ path.addLine(to: startPoint) } } return path }
}

Source : https://blog.techchee.com/how-to-create-custom-shapes-in-swiftui/ | Last Update : Sun, 24 Apr 22

Answers related to swiftui polygon

Code Explorer Popular Question For Swift