Advertisement
mmayoub

canvas, ex12.02.2018

Feb 12th, 2018
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.26 KB | None | 0 0
  1. class MyDrawings: UIView {
  2.    
  3.     /*
  4.     // Only override draw() if you perform custom drawing.
  5.     // An empty implementation adversely affects performance during animation.
  6.     override func draw(_ rect: CGRect) {
  7.         // Drawing code
  8.     }
  9.     */
  10.    
  11.     override func draw(_ rect: CGRect) {
  12.          let context=UIGraphicsGetCurrentContext()
  13.         context?.setLineWidth(2.5)
  14.         context?.setStrokeColor(UIColor.purple.cgColor)
  15.        
  16.         //drawTriangle(context: context,p1: CGPoint(x: 0, y: 0), p2: CGPoint(x: rect.width, y: rect.height/2), p3: CGPoint(x: rect.width/2, y: rect.height))
  17.         var p1=CGPoint(x: rect.width/2, y: 10)
  18.         var p2=CGPoint(x: 10, y: rect.height-10)
  19.         var p3=CGPoint(x: rect.width-10, y: rect.height-10)
  20.        
  21.         for _ in 1...5 {
  22.             context?.move(to: p1)
  23.             context?.addLine(to: p2)
  24.             context?.addLine(to: p3 )
  25.             context?.addLine(to: p1)
  26.             context?.strokePath()
  27.        
  28.             let temp = p1
  29.             p1=mid(p1:p1,p2:p2)
  30.             p2=mid(p1:p2,p2:p3)
  31.             p3=mid(p1:p3, p2:temp)
  32.         }
  33.     }
  34.    
  35.     func mid(p1:CGPoint, p2:CGPoint) -> CGPoint {
  36.         return CGPoint(x: (p1.x+p2.x)/2, y: (p1.y+p2.y)/2)
  37.     }
  38.  
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement