Skip to content

Instantly share code, notes, and snippets.

@jashmenn
Last active August 29, 2015 14:02
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 jashmenn/baafdba188d9a8bfddfd to your computer and use it in GitHub Desktop.
Save jashmenn/baafdba188d9a8bfddfd to your computer and use it in GitHub Desktop.
This gist is an OSX Playground to draw parabola-like lines. If you'd like to get lessons like this in your inbox, sign up at https://fullstackedu.com
// This gist is an OSX Playground to draw parabola-like lines.
// If you'd like to get lessons like this in your inbox, sign up at https://fullstackedu.com
// Preview results image: http://i.imgur.com/h2VeRDv.png
import Cocoa
import XCPlayground
// create a place to view the image we're going to draw
var view = NSImageView(frame:
NSRect(x: 0, y: 0, width: 300, height: 300))
// create a size, specifying a width of 300 and a height of 300
var size = NSMakeSize(300, 300)
// create a blank image using our size
var img = NSImage(size: size)
// tell our view to show our image
view.image = img
// tell playground to show our view
XCPShowView("Here", view)
// setup shift
var shift = 0
while shift <= 200 {
// we use lockFocus() to say "draw on this image"
img.lockFocus()
// we want to draw a straight line so we create a line object
var path = NSBezierPath()
// to draw a line we need to specify a starting point and an ending point
// so move our "path" to the starting point
path.moveToPoint(NSPoint(x: shift, y: 200))
// and connect a line to the ending point
path.lineToPoint(NSPoint(x: 0, y: shift))
// but the two commands above don't actually *draw* the line. we have to call .stroke() if we want to actually see the line
path.stroke()
// move our shift a little bit
shift = shift + 14 // <-- click the + symbol on this line
// if we lock the image, we need to unlock it
img.unlockFocus() // <--- click the + symbol on this line too
// tell our view to display our image
view.needsDisplay = true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment