Created
November 18, 2014 10:07
-
-
Save sketchytech/74727d13a2a6176914a1 to your computer and use it in GitHub Desktop.
Draw a cube in Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import QuartzCore | |
class ViewController: UIViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view, typically from a nib. | |
// select cube rotation | |
let adjustment:CGFloat = 90 | |
// draw cube | |
let cube = drawCube(100, 250, 50, adjustment, UIColor(hue: 60/360, saturation: 80/100, brightness: 100/100, alpha: 1.0)) | |
for c in cube { | |
self.view.layer.addSublayer(c) | |
} | |
} | |
} | |
func degree2radian(a:CGFloat)->CGFloat { | |
let b = CGFloat(M_PI) * a/180 | |
return b | |
} | |
func polygonPointArray(sides:Int,x:CGFloat,y:CGFloat,radius:CGFloat,adjustment:CGFloat=0)->[CGPoint] { | |
let angle = degree2radian(360/CGFloat(sides)) | |
let cx = x // x origin | |
let cy = y // y origin | |
let r = radius // radius of circle | |
var i = sides | |
var points = [CGPoint]() | |
while points.count <= sides { | |
let xpo = cx - r * cos(angle * CGFloat(i)+degree2radian(adjustment)) | |
let ypo = cy - r * sin(angle * CGFloat(i)+degree2radian(adjustment)) | |
points.append(CGPoint(x: xpo, y: ypo)) | |
i--; | |
} | |
return points | |
} | |
func drawCube (x:CGFloat,y:CGFloat,radius:CGFloat, adjustment:CGFloat, color:UIColor)->[CAShapeLayer] { | |
// --- Color is shaded with light coming from above and to the right --- // | |
var h:CGFloat = 0 | |
var s:CGFloat = 0 | |
var b:CGFloat = 0 | |
color.getHue(&h, saturation: &s, brightness: &b, alpha: nil) | |
// --- Left side of cube (visible) --- // | |
let leftSide = CAShapeLayer() | |
let leftSidePath = cubeLeft(x: x, y: y, radius: radius, sides: 6, adjustment:adjustment) | |
leftSide.path = leftSidePath | |
leftSide.fillColor = UIColor(hue: h, saturation: s, brightness: b*0.7, alpha: 1.0).CGColor | |
// --- Front side of cube (visible) --- // | |
let frontSide = CAShapeLayer() | |
let frontSidePath = cubeFront(x: x, y: y, radius: radius, sides: 6, adjustment:adjustment) | |
frontSide.path = frontSidePath | |
frontSide.fillColor = UIColor(hue: h, saturation: s, brightness: b*0.85, alpha: 1.0).CGColor | |
let topSide = CAShapeLayer() | |
let topSidePath = cubeTop(x: x, y: y, radius: radius, sides: 6, adjustment:adjustment) | |
topSide.path = topSidePath | |
topSide.fillColor = UIColor(hue: h, saturation: s, brightness: b*0.95, alpha: 1.0).CGColor | |
return [leftSide,frontSide,topSide] | |
} | |
func cubeTop(#x:CGFloat, #y:CGFloat, #radius:CGFloat, #sides:Int, #adjustment:CGFloat) -> CGPathRef { | |
let path = CGPathCreateMutable() | |
let points = polygonPointArray(sides,x,y,radius,adjustment: adjustment) | |
var cpg = points[0] | |
CGPathMoveToPoint(path, nil, cpg.x, cpg.y) | |
CGPathAddLineToPoint(path, nil, points[1].x, points[1].y) | |
CGPathAddLineToPoint(path, nil, x, y) | |
CGPathAddLineToPoint(path, nil, points[5].x, points[5].y) | |
CGPathCloseSubpath(path) | |
return path | |
} | |
func cubeFront(#x:CGFloat, #y:CGFloat, #radius:CGFloat, #sides:Int, #adjustment:CGFloat) -> CGPathRef { | |
let path = CGPathCreateMutable() | |
let points = polygonPointArray(sides,x,y,radius,adjustment: adjustment) | |
var cpg = points[5] | |
CGPathMoveToPoint(path, nil, cpg.x, cpg.y) | |
CGPathAddLineToPoint(path, nil, points[4].x, points[4].y) | |
CGPathAddLineToPoint(path, nil, points[3].x, points[3].y) | |
CGPathAddLineToPoint(path, nil, x, y) | |
CGPathCloseSubpath(path) | |
return path | |
} | |
func cubeLeft(#x:CGFloat, #y:CGFloat, #radius:CGFloat, #sides:Int, #adjustment:CGFloat) -> CGPathRef { | |
let path = CGPathCreateMutable() | |
let points = polygonPointArray(sides,x,y,radius,adjustment: adjustment) | |
var cpg = points[5] | |
CGPathMoveToPoint(path, nil, x, y) | |
CGPathAddLineToPoint(path, nil, points[1].x, points[1].y) | |
CGPathAddLineToPoint(path, nil, points[2].x, points[2].y) | |
CGPathAddLineToPoint(path, nil, points[3].x, points[3].y) | |
CGPathCloseSubpath(path) | |
return path | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment