Created
November 18, 2014 11:25
-
-
Save sketchytech/b05ee27cf9c4a6543ce2 to your computer and use it in GitHub Desktop.
Swift: Prism and cube
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 | |
let radius:CGFloat = 50 | |
let cubePoint = CGPoint(x: 100, y: 250) | |
// draw cube | |
let cube = drawCube(cubePoint.x, cubePoint.y, radius, adjustment, UIColor(hue: 60/360, saturation: 80/100, brightness: 100/100, alpha: 1.0)) | |
for c in cube { | |
self.view.layer.addSublayer(c) | |
} | |
let pyramid = drawPyramid(cubePoint.x, cubePoint.y-radius, radius, adjustment, UIColor(hue: 197/360, saturation: 65/100, brightness: 100/100, alpha: 1.0)) | |
for p in pyramid { | |
self.view.layer.addSublayer(p) | |
} | |
} | |
} | |
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 | |
} | |
func pyramidFront(#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[4] | |
CGPathMoveToPoint(path, nil, cpg.x, cpg.y) | |
CGPathAddLineToPoint(path, nil, points[5].x/2+points[1].x/2, points[5].y/2+points[1].y/2) | |
CGPathAddLineToPoint(path, nil, points[3].x, points[3].y) | |
CGPathCloseSubpath(path) | |
return path | |
} | |
func pyramidLeft(#x:CGFloat, #y:CGFloat, #radius:CGFloat, #sides:Int, #adjustment:CGFloat) -> (path:CGPathRef,point:CGPoint) { | |
let path = CGPathCreateMutable() | |
let points = polygonPointArray(sides,x,y,radius,adjustment: adjustment) | |
var cpg = points[3] | |
CGPathMoveToPoint(path, nil, cpg.x, cpg.y) | |
CGPathAddLineToPoint(path, nil, points[5].x/2+points[1].x/2, points[5].y/2+points[1].y/2) | |
CGPathAddLineToPoint(path, nil, points[2].x, points[2].y) | |
CGPathCloseSubpath(path) | |
let topOfPyramid = CGPoint(x:points[5].x/2+points[1].x/2, y:points[5].y/2+points[1].y/2) | |
return (path,topOfPyramid) | |
} | |
func drawPyramid(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) | |
let pyramidFrontLayer = CAShapeLayer() | |
let pyramidFrontPath = pyramidFront(x: x, y: y, radius: radius, sides: 6, adjustment: adjustment) | |
pyramidFrontLayer.path = pyramidFrontPath | |
pyramidFrontLayer.fillColor = UIColor(hue: h, saturation: s, brightness: b*0.85, alpha: 1.0).CGColor | |
let pyramidLeftLayer = CAShapeLayer() | |
let pyramidLeftPath = pyramidLeft(x: x, y: y, radius: radius, sides: 6, adjustment: adjustment) | |
pyramidLeftLayer.path = pyramidLeftPath.path | |
pyramidLeftLayer.fillColor = UIColor(hue: h, saturation: s, brightness: b*0.7, alpha: 1.0).CGColor | |
return [pyramidFrontLayer,pyramidLeftLayer] | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment