Skip to content

Commit

Permalink
Add math operators to Point (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
finnvoor authored Aug 12, 2024
1 parent 419b8a5 commit 6f41a39
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions Sources/PlaydateKit/Geometry/Point.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 6f41a39

Please sign in to comment.