// 定義兩個示範需要用到的結構
struct Size {
var width = 0.0, height = 0.0
}
struct Point {
var x = 0.0, y = 0.0
}
// 定義一個方形的結構 Rect
struct Rect {
// 使用上面兩個定義的結構來儲存這個方形的原點及尺寸
var origin = Point()
var size = Size()
// 三個建構器
init() {}
init(origin: Point, size: Size) {
self.origin = origin
self.size = size
}
init(center: Point, size: Size) {
let originX = center.x - (size.width / 2)
let originY = center.y - (size.height / 2)
self.init(
origin: Point(x: originX, y: originY), size: size
)
}
}
let centerRect = Rect(center: Point(x: 4.0, y: 4.0),size: Size(width: 3.0, height: 3.0))
// centerRect 內的屬性的值分別為
// origin 為 (2.5, 2.5)
// size 為 (3.0, 3.0)
网友评论