From 6f41a395f993db0723120969037b143ea1630360 Mon Sep 17 00:00:00 2001 From: Finn Voorhees Date: Mon, 12 Aug 2024 17:56:42 +0100 Subject: [PATCH] Add math operators to Point (#79) --- Sources/PlaydateKit/Geometry/Point.swift | 54 ++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/Sources/PlaydateKit/Geometry/Point.swift b/Sources/PlaydateKit/Geometry/Point.swift index 2ca5fd48..27e42880 100644 --- a/Sources/PlaydateKit/Geometry/Point.swift +++ b/Sources/PlaydateKit/Geometry/Point.swift @@ -22,6 +22,60 @@ public struct Point: Equatable { public var x, y: Float } +public extension Point { + static func + (lhs: Point, rhs: Point) -> Point { + Point(x: lhs.x + rhs.x, y: lhs.y + rhs.y) + } + + static func += (lhs: inout Point, rhs: Point) { + lhs = lhs + rhs + } + + static func - (lhs: Point, rhs: Point) -> Point { + Point(x: lhs.x - rhs.x, y: lhs.y - rhs.y) + } + + static func -= (lhs: inout Point, rhs: Point) { + lhs = lhs - rhs + } + + static func * (lhs: Point, rhs: Point) -> Point { + Point(x: lhs.x * rhs.x, y: lhs.y * rhs.y) + } + + static func *= (lhs: inout Point, rhs: Point) { + lhs = lhs * rhs + } + + static func * (lhs: Point, rhs: Float) -> Point { + Point(x: lhs.x * rhs, y: lhs.y * rhs) + } + + static func *= (lhs: inout Point, rhs: Float) { + lhs = lhs * rhs + } + + static func / (lhs: Point, rhs: Point) -> Point { + Point(x: lhs.x / rhs.x, y: lhs.y / rhs.y) + } + + static func /= (lhs: inout Point, rhs: Point) { + lhs = lhs / rhs + } + + static func / (lhs: Point, rhs: Float) -> Point { + Point(x: lhs.x / rhs, y: lhs.y / rhs) + } + + static func /= (lhs: inout Point, rhs: Float) { + lhs = lhs / rhs + } + + static prefix func - (point: Point) -> Point { + Point(x: -point.x, y: -point.y) + } +} + // MARK: AffineTransformable extension Point: AffineTransformable {