Swift: Clock Layout Update*

While yesterday’s code solved the ‘problem’, today I took a few minutes to do some abstraction and write an alternate version that computes the positions of n points evenly spaced in a circle:


//Constant size of each node on the clock face
let kSize:CGFloat = 4

//Place a ClockFace node in the center
let clockFace:SKShapeNode = nodeGenerator(size:r, point: CGPoint( x:midPoint.x, y:midPoint.y), scene:scene)
clockFace.fillColor = SKColor.gray

//Place a normal node in the center
let centerNode:SKShapeNode = nodeGenerator(size:kSize, point: CGPoint( x:midPoint.x, y:midPoint.y), scene:scene)

//Determine radian spacing...
let nodesOnTheClock = 12
let interval:Double = 2 * M_PI / Double(nodesOnTheClock) //Radians

for index in 1...nodesOnTheClock {
    let angle:Double = M_PI_2 - Double(index) * interval
    let node:SKShapeNode = nodeGenerator(size: kSize,
                                           point: CGPoint(
                                            x:midPoint.x + r * cos(CGFloat(angle)),
                                            y:midPoint.y + r * sin(CGFloat(angle))),
                                           scene:scene)
    
}

screen-shot-2016-11-01-at-3-38-46-pm

You can grab the Gist at: https://gist.github.com/Zbeyer/038b672ba619ea93a06c1ed49608731c

The Full Code Snippet will be below the following screenshots:

Continue reading

Bookmark and Share

Clock Points — Swift Playground

Another Quick Exercise

For my birthday, I decided to solve a display exercise in Swift that inevitably will be necessary in my personal Towne of Klock project. The problem is very simple:

Given a 2D cartesian plane (in this case a SpriteKit Scene in Swift) determine where the points for One O’Clock through Twelve O’Clock must be placed. Not a very original or difficult problem to solve, per say. However, I do so love playing with geometry!

If interested in my solution you can do one of the following…

screen-shot-2016-10-31-at-4-09-29-pm

Continue reading

Bookmark and Share