Skip to content

Instantly share code, notes, and snippets.

@RyanCopley
Created June 4, 2014 21:38
Show Gist options
  • Save RyanCopley/698aabd5ffa2c7366cb1 to your computer and use it in GitHub Desktop.
Save RyanCopley/698aabd5ffa2c7366cb1 to your computer and use it in GitHub Desktop.
“struct Point {
var x = 0.0, y = 0.0
}
struct Size {
var width = 0.0, height = 0.0
}
struct Rect {
var origin = Point()
var size = Size()
var center: Point {
get {
let centerX = origin.x + (size.width / 2)
let centerY = origin.y + (size.height / 2)
return Point(x: centerX, y: centerY)
}
set(newCenter) {
origin.x = newCenter.x - (size.width / 2)
origin.y = newCenter.y - (size.height / 2)
}
}
}
var square = Rect(origin: Point(x: 0.0, y: 0.0),
size: Size(width: 10.0, height: 10.0))
let initialSquareCenter = square.center
square.center = Point(x: 15.0, y: 15.0)
println("square.origin is now at (\(square.origin.x), \(square.origin.y))")
// prints "square.origin is now at (10.0, 10.0)”
Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/us/jEUH0.l
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment