Search
 
SCRIPT & CODE EXAMPLE
 

SWIFT

swiftui polygon

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
  }
}
Comment

PREVIOUS NEXT
Code Example
Swift :: fullscreencover swiftui 
Swift :: swift constants 
Swift :: Swift break and continue Inside Nested Loop 
Swift :: Computed Property In Extension Swift 
Swift :: Iterate Over enum Cases Swift 
Swift :: swift concurrency datatask before ios 15 
Swift :: Swift Loop Statements 
Swift :: check and uncheck cells in uitableview swift 5 
Swift :: Notification Service Extension vs Content Extension 
Swift :: Swift Variables names must start with either a letter 
Swift :: Swift Left Shift Operator 
Swift :: xamarin get textview by id 
Swift :: swift get all cases starting with 
Swift :: swiftui show custom loading spinner 
Swift :: swift closure 
Swift :: how to figure out ios vesion swift 
Ruby :: base64 decode ruby 
Ruby :: rails benchmark 
Ruby :: convert string to hash ruby 
Ruby :: ruby reference a file in a gem 
Ruby :: rails excep 
Ruby :: rails change column type string to integer 
Ruby :: how to get ip address of client using rails 
Ruby :: rails array sort 
Ruby :: ruby array append vs push 
Ruby :: iterate over array ruby 
Ruby :: how to find even number in an array ruby 
Ruby :: rails get random record 
Ruby :: active admin with friendly_id 
Ruby :: filter through array of arrays ruby 
ADD CONTENT
Topic
Content
Source link
Name
9+3 =