Skip to content

Instantly share code, notes, and snippets.

@gurgeous
Created November 15, 2014 00:11
Show Gist options
  • Save gurgeous/adfc281f52f3206a1689 to your computer and use it in GitHub Desktop.
Save gurgeous/adfc281f52f3206a1689 to your computer and use it in GitHub Desktop.
missing operators for MKMapPoint, MKMapSize and MKMapRect
//
// MARK: MKMapPoint op point/size/float
//
func -(l: MKMapPoint, r: MKMapPoint) -> MKMapPoint { return MKMapPoint(x: l.x - r.x, y: l.y - r.y) }
func +(l: MKMapPoint, r: MKMapPoint) -> MKMapPoint { return MKMapPoint(x: l.x + r.x, y: l.y + r.y) }
func *(l: MKMapPoint, r: MKMapPoint) -> MKMapPoint { return MKMapPoint(x: l.x * r.x, y: l.y * r.y) }
func /(l: MKMapPoint, r: MKMapPoint) -> MKMapPoint { return MKMapPoint(x: l.x / r.x, y: l.y / r.y) }
func -(l: MKMapPoint, r: MKMapSize) -> MKMapPoint { return MKMapPoint(x: l.x - r.width, y: l.y - r.height) }
func +(l: MKMapPoint, r: MKMapSize) -> MKMapPoint { return MKMapPoint(x: l.x + r.width, y: l.y + r.height) }
func *(l: MKMapPoint, r: MKMapSize) -> MKMapPoint { return MKMapPoint(x: l.x * r.width, y: l.y * r.height) }
func /(l: MKMapPoint, r: MKMapSize) -> MKMapPoint { return MKMapPoint(x: l.x / r.width, y: l.y / r.height) }
func -(l: MKMapPoint, r: Double) -> MKMapPoint { return MKMapPoint(x: l.x - r, y: l.y - r) }
func +(l: MKMapPoint, r: Double) -> MKMapPoint { return MKMapPoint(x: l.x + r, y: l.y + r) }
func *(l: MKMapPoint, r: Double) -> MKMapPoint { return MKMapPoint(x: l.x * r, y: l.y * r) }
func /(l: MKMapPoint, r: Double) -> MKMapPoint { return MKMapPoint(x: l.x / r, y: l.y / r) }
func -=(inout l: MKMapPoint, r: MKMapPoint) { l = l - r }
func +=(inout l: MKMapPoint, r: MKMapPoint) { l = l + r }
func *=(inout l: MKMapPoint, r: MKMapPoint) { l = l * r }
func /=(inout l: MKMapPoint, r: MKMapPoint) { l = l / r }
func -=(inout l: MKMapPoint, r: MKMapSize) { l = l - r }
func +=(inout l: MKMapPoint, r: MKMapSize) { l = l + r }
func *=(inout l: MKMapPoint, r: MKMapSize) { l = l * r }
func /=(inout l: MKMapPoint, r: MKMapSize) { l = l / r }
func -=(inout l: MKMapPoint, r: Double) { l = l - r }
func +=(inout l: MKMapPoint, r: Double) { l = l + r }
func *=(inout l: MKMapPoint, r: Double) { l = l * r }
func /=(inout l: MKMapPoint, r: Double) { l = l / r }
extension MKMapPoint {
init() {
x = 0
y = 0
}
init(x: Double) {
self.x = x
self.y = 0
}
init(y: Double) {
self.x = 0
self.y = y
}
func with(#x: Double) -> MKMapPoint {
return MKMapPoint(x: x, y: y)
}
func with(#y: Double) -> MKMapPoint {
return MKMapPoint(x: x, y: y)
}
func coordinate() -> CLLocationCoordinate2D {
return MKCoordinateForMapPoint(self)
}
func size() -> MKMapSize {
return MKMapSize(width: x, height: y)
}
}
//
// MARK: MKMapSize op size/float
//
func -(l: MKMapSize, r: MKMapSize) -> MKMapSize { return MKMapSize(width: l.width - r.width, height: l.height - r.height) }
func +(l: MKMapSize, r: MKMapSize) -> MKMapSize { return MKMapSize(width: l.width + r.width, height: l.height + r.height) }
func *(l: MKMapSize, r: MKMapSize) -> MKMapSize { return MKMapSize(width: l.width * r.width, height: l.height * r.height) }
func /(l: MKMapSize, r: MKMapSize) -> MKMapSize { return MKMapSize(width: l.width / r.width, height: l.height / r.height) }
func -(l: MKMapSize, r: Double) -> MKMapSize { return MKMapSize(width: l.width - r, height: l.height - r) }
func +(l: MKMapSize, r: Double) -> MKMapSize { return MKMapSize(width: l.width + r, height: l.height + r) }
func *(l: MKMapSize, r: Double) -> MKMapSize { return MKMapSize(width: l.width * r, height: l.height * r) }
func /(l: MKMapSize, r: Double) -> MKMapSize { return MKMapSize(width: l.width / r, height: l.height / r) }
func -=(inout l: MKMapSize, r: MKMapSize) { l = l - r }
func +=(inout l: MKMapSize, r: MKMapSize) { l = l + r }
func *=(inout l: MKMapSize, r: MKMapSize) { l = l * r }
func /=(inout l: MKMapSize, r: MKMapSize) { l = l / r }
func -=(inout l: MKMapSize, r: Double) { l = l - r }
func +=(inout l: MKMapSize, r: Double) { l = l + r }
func *=(inout l: MKMapSize, r: Double) { l = l * r }
func /=(inout l: MKMapSize, r: Double) { l = l / r }
extension MKMapSize {
init() {
self.width = 0
self.height = 0
}
init(width: Double) {
self.width = width
self.height = 0
}
init(height: Double) {
self.width = 0
self.height = height
}
func with(#width: Double) -> MKMapSize { return MKMapSize(width: width, height: height) }
func with(#height: Double) -> MKMapSize { return MKMapSize(width: width, height: height) }
func point() -> MKMapPoint {
return MKMapPoint(x: width, y: height)
}
}
//
// MARK: MKMapRect accessors
//
extension MKMapRect {
init() {
self.origin = MKMapPoint()
self.size = MKMapSize()
}
init(origin: MKMapPoint) {
self.origin = origin
self.size = MKMapSize()
}
init(size: MKMapSize) {
self.origin = MKMapPoint()
self.size = size
}
init(x: Double, y: Double) {
self.origin = MKMapPoint(x: x, y: y)
self.size = MKMapSize()
}
init(x: Double, y: Double, size: MKMapSize) {
self.origin = MKMapPoint(x: x, y: y)
self.size = size
}
init(width: Double, height: Double) {
self.origin = MKMapPoint()
self.size = MKMapSize(width: width, height: height)
}
init(origin: MKMapPoint, width: Double, height: Double) {
self.origin = origin
self.size = MKMapSize(width: width, height: height)
}
init(x: Double, y: Double, width: Double, height: Double) {
self.origin = MKMapPoint(x: x, y: y)
self.size = MKMapSize(width: width, height: height)
}
func with(#x: Double) -> MKMapRect { return MKMapRect(x: x, y: y, width: width, height: height) }
func with(#y: Double) -> MKMapRect { return MKMapRect(x: x, y: y, width: width, height: height) }
func with(#width: Double) -> MKMapRect { return MKMapRect(x: x, y: y, width: width, height: height) }
func with(#height: Double) -> MKMapRect { return MKMapRect(x: x, y: y, width: width, height: height) }
func contains(pt: MKMapPoint) -> Bool {
return MKMapRectContainsPoint(self, pt)
}
func rectByInsetting(#dx: Double, dy: Double) -> MKMapRect {
return MKMapRectInset(self, dx, dy)
}
func rectByExpanding(percent: Double) -> MKMapRect {
return rectByInsetting(dx: -width * percent, dy: -height * percent)
}
var x: Double {
get { return origin.x }
set { origin.x = newValue }
}
var y: Double {
get { return origin.y }
set { origin.y = newValue }
}
var width: Double {
get { return size.width }
set { size.width = newValue }
}
var height: Double {
get { return size.height }
set { size.height = newValue }
}
var centerX: Double {
get { return x + width / 2 }
set { x = newValue - width / 2 }
}
var centerY: Double {
get { return y + height / 2 }
set { y = newValue - height / 2 }
}
var center: MKMapPoint {
get { return MKMapPoint(x: centerX, y: centerY) }
set {
centerX = newValue.x
centerY = newValue.y
}
}
var left: Double {
get { return x }
set { x = newValue }
}
var top: Double {
get { return y }
set { y = newValue }
}
var right: Double {
get { return x + width }
set { x = newValue - width }
}
var bottom: Double {
get { return y + height }
set { y = newValue - height }
}
}
//
// MARK: Equatable
//
extension MKMapPoint: Equatable { }
extension MKMapSize: Equatable { }
extension MKMapRect: Equatable { }
public func ==(l: MKMapPoint, r: MKMapPoint) -> Bool { return MKMapPointEqualToPoint(l, r) }
public func ==(l: MKMapSize, r: MKMapSize) -> Bool { return MKMapSizeEqualToSize(l, r) }
public func ==(l: MKMapRect, r: MKMapRect) -> Bool { return MKMapRectEqualToRect(l, r) }
//
// MARK: Printable
//
extension MKMapPoint: Printable {
public var description: String { get { return MKStringFromMapPoint(self) } }
}
extension MKMapSize: Printable {
public var description: String { get { return MKStringFromMapSize(self) } }
}
extension MKMapRect: Printable {
public var description: String { get { return MKStringFromMapRect(self) } }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment