
- SwiftUI - Home
- SwiftUI - Overview
- SwiftUI vs UIkit
- SwiftUI Environment
- SwiftUI - Environment Setup
- SwiftUI - Basic Components
- SwiftUI - Building First Application
- SwiftUI Views
- SwiftUI - Views
- SwiftUI - Customize Text View
- SwiftUI - Custom Image View
- SwiftUI - Stacks
- SwiftUI Drawing Shapes
- SwiftUI - Shapes
- SwiftUI - Drawing line
- SwiftUI - Drawing Rectangle
- SwiftUI - Drawing Rounded Rectangle
- SwiftUI - Drawing Triangle
- SwiftUI - Drawing Circle
- SwiftUI - Drawing Star
- SwiftUI - Drawing Polygon
- SwiftUI - Drawing Pie chart
- SwiftUI - Using built-in shapes
- SwiftUI - Text
- SwiftUI - Text View
- SwiftUI - Text Input and Output
- SwiftUI - Color
- SwiftUI - Color
- SwiftUI - Colorpicker
- SwiftUI - Gradients
- SwiftUI - Adjust Color
- SwiftUI - Effects
- SwiftUI - Effects
- SwiftUI - Blend Effect
- SwiftUI - BLur Effect
- SwiftUI - Shadow Effect
- SwiftUI - Hover Effect
- SwiftUI - Animations
- SwiftUI - Animations
- SwiftUI - Creating Animations
- SwiftUI - Creating an Explicit Animation
- SwiftUI - Multiple Animations
- SwiftUI - Transitions
- SwiftUI - Asymmetric Transition
- SwiftUI - Custom Transition
- SwiftUI - Image
- SwiftUI - Images
- SwiftUI - Image as Background
- SwiftUI - Rotating Image
- SwiftUI - Media
- SwiftUI - View Layout
- SwiftUI - View Layout
- SwiftUI - View Size
- SwiftUI - View Spacing
- SwiftUI - View Padding
- SwiftUI - UI Controls
- SwiftUI - UI Controls
- SwiftUI - Button
- SwiftUI - CheckBox
- SwiftUI - Menubar
- SwiftUI - Toolbar
- SwiftUI - Search Bar
- SwiftUI - TextField
- SwiftUI - Slider
- SwiftUI - Toggle
- SwiftUI - Pickers
- SwiftUI - Menus
- SwiftUI - List & Tables
- SwiftUI - Lists
- SwiftUI - Static List
- SwiftUI - Dynamic List
- SwiftUI - Customize List
- SwiftUI - Tables
- SwiftUI - Forms
- SwiftUI - Forms
- SwiftUI - Breaking Forms in Sections
- SwiftUI - Event Handling
- SwiftUI - Event Handling
- SwiftUI - Gesture
- SwiftUI - Clipboard
- SwiftUI - Drag and Drop
- SwiftUI - Focus
- SwiftUI - Alert
- SwiftUI - Miscellaneous
- SwiftUI - Containers
- SwiftUI - Navigation
- SwiftUI - Notifications
- SwiftUI - Cross-Platform UI
- SwiftUI - Data
- SwiftUI - Accessibility
- SwiftUI - Framework Integration
- SwiftUI - Framework Integration
- SwiftUI - Interfacing with UIKit
- SwiftUI - Creating macOS App
- SwiftUI Useful Resources
- SwiftUI - Useful Resources
- SwiftUI - Discussion
SwiftUI-Drawing Polygon
A Polygon is a closed shape with a finite number of straight sides. They are classified according to the number of sides they have such as Triangle(3 sides), Pentagon(5 sides), Quadrilateral(4 sides), Hexagon(6 sides), Octagon(8 sides), Nonagone(9 sides)etc.

SwiftUI does not provide any built-in method to create a polygon. So we will create a custom polygon with the help of the addLine() method. This method adds a straight line segment from the current point to the given point(endpoint).
Syntax
Following is the syntax−
addLine(to end: CGPoint)
It takes only one parameter that is, end to represent the location of endpoint of the line segment in the user space coordinates
Steps to Draw a Polygon in SwiftUI
Follow the following steps to draw a polygon in SwiftUI −
Step 1: Define the Custom Shape
To draw a polygon, we need to create a struct that conforms to Shape protocol. Here we will define how the polygon will be created inside the path(in:) method.
struct Polygon: Shape { var side: Int func path(in rect: CGRect) -> Path { guard side > 2 else { return Path() } let Pcenter = CGPoint(x: rect.width / 2, y: rect.height / 2) let Pangle = (2 * .pi) / CGFloat(side) let Pradius = min(rect.width, rect.height) / 2 var xpath = Path() for a in 0..<side { let x = Pcenter.x + Pradius * cos(CGFloat(a) * Pangle - .pi / 2) let y = Pcenter.y + Pradius * sin(CGFloat(a) * Pangle - .pi / 2) if a == 0 { xpath.move(to: CGPoint(x: x, y: y)) } else { xpath.addLine(to: CGPoint(x: x, y: y)) } } xpath.closeSubpath() return xpath }
Step 2: Create a View
Now we use the custom polygon in a SwiftUI view. It will show how the custom polygon will look.
struct ContentView: View { var body: some View { Polygon(side: 6) } }
Step 3: Adding Additional Customization
We can also include additional customization like stroke, fill, shadow, overlay text etc. in the polygon.
struct ContentView: View { var body: some View { Polygon(side: 6) .stroke(Color.purple, lineWidth: 5) .frame(width: 300, height: 300) .padding() } }
Example
The Following SwiftUI program is used to draw a simple polygon.
import SwiftUI struct Polygon: Shape { var side: Int func path(in rect: CGRect) -> Path { guard side > 2 else {return Path()} let Pcenter = CGPoint(x: rect.width / 2, y: rect.height / 2) let Pangle = (2 * .pi) / CGFloat(side) let Pradius = min(rect.width, rect.height) / 2 var xpath = Path() for a in 0..<side { let x = Pcenter.x + Pradius * cos(CGFloat(a) * Pangle - .pi / 2) let y = Pcenter.y + Pradius * sin(CGFloat(a) * Pangle - .pi / 2) if a == 0 { xpath.move(to: CGPoint(x: x, y: y)) } else { xpath.addLine(to: CGPoint(x: x, y: y)) } } xpath.closeSubpath() return xpath } } struct ContentView: View { var body: some View { Polygon(side: 6) .stroke(Color.purple, lineWidth: 5) .frame(width: 300, height: 300) .padding() } } #Preview { ContentView() }
Output

We can also customize our polygon by using various modifiers such as .fill(), frame(), .shadow(), .overlay, etc to make it more attractive. Here we draw a solid polygon with shadow so we will use the .fill() modifier to make a solid polygon and shadow() for applying the shadow effect
struct ContentView: View { var body: some View { Polygon(side: 7) .fill(Color.mint) .frame(width: 300, height: 300) .shadow(radius: 10) } }
Output
