Skip to content

Instantly share code, notes, and snippets.

@kaunteya
Created March 24, 2015 11:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaunteya/c6aec423d5f36558fd7d to your computer and use it in GitHub Desktop.
Save kaunteya/c6aec423d5f36558fd7d to your computer and use it in GitHub Desktop.
Xcode playground to draw a circle
// Playground - noun: a place where people can play
import Cocoa
import XCPlayground
let graphWidth: CGFloat = 400.0;
let graphHeight: CGFloat = 400.0
var frameRect = NSRect(x: 0, y: 0, width: graphWidth, height: graphHeight)
var view = NSImageView(frame: frameRect)
var size = NSMakeSize( graphWidth, graphHeight )
var image = NSImage(size: size)
image.lockFocus()
let foregroundColor = NSColor.blackColor()
foregroundColor.setFill()
image.unlockFocus()
let radius :Double = 150
let center = NSMakePoint(graphWidth / 2, graphHeight / 2)
var x : Double
var y : Double
var p : NSPoint
var oldPoint : NSPoint!
var path = NSBezierPath()
for var i : Double = 0.0; i < 390; i += 10.00 {
image.lockFocus()
let angle : Double = i * (M_PI / 180)
x = cos(angle) * radius + Double(center.x)
y = sin(angle) * radius + Double(center.y)
p = NSPoint(x: x , y: y)
if oldPoint == nil {
oldPoint = p
}
path.moveToPoint(oldPoint)
path.lineToPoint(p)
path.stroke()
image.unlockFocus()
oldPoint = p
}
var view2 = NSImageView(frame: frameRect)
view2.image = image
XCPShowView("Circle", view2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment