-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
!refactor: Points are now immutable with operator functions, add a lo…
…t of useful functions on Point & ObservablePoint.
- Loading branch information
Showing
3 changed files
with
82 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
pixi/src/main/kotlin/pixi/externals/extensions/_ObservablePoint.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package pixi.externals.extensions | ||
|
||
import pixi.typings.math.IPointData | ||
import pixi.typings.math.ObservablePoint | ||
import kotlin.math.ceil | ||
import kotlin.math.floor | ||
import kotlin.math.roundToInt | ||
|
||
operator fun <T> ObservablePoint<T>.plus(other: IPointData) = clone().set(x + other.x, y + other.y) | ||
operator fun <T> ObservablePoint<T>.plus(factor: Number) = clone().set(x + factor.unsafeCast<Double>(), y + factor.unsafeCast<Double>()) | ||
|
||
operator fun <T> ObservablePoint<T>.minus(other: IPointData) = clone().set(x - other.x, y - other.y) | ||
operator fun <T> ObservablePoint<T>.minus(factor: Number) = clone().set(x - factor.unsafeCast<Double>(), y - factor.unsafeCast<Double>()) | ||
|
||
operator fun <T> ObservablePoint<T>.times(other: IPointData) = clone().set(x * other.x, y * other.y) | ||
operator fun <T> ObservablePoint<T>.times(factor: Number) = clone().set(x * factor.unsafeCast<Double>(), y * factor.unsafeCast<Double>()) | ||
|
||
operator fun <T> ObservablePoint<T>.div(other: IPointData) = clone().set(x / other.x, y / other.y) | ||
operator fun <T> ObservablePoint<T>.div(factor: Number) = clone().set(x / factor.unsafeCast<Double>(), y / factor.unsafeCast<Double>()) | ||
|
||
operator fun <T> ObservablePoint<T>.rem(other: IPointData) = clone().set(x % other.x, y % other.y) | ||
operator fun <T> ObservablePoint<T>.rem(factor: Number) = clone().set(x % factor.unsafeCast<Double>(), y % factor.unsafeCast<Double>()) | ||
|
||
fun <T> ObservablePoint<T>.floored() = clone().set(floor(x), floor(y)) | ||
fun <T> ObservablePoint<T>.ceiled() = clone().set(ceil(x), ceil(y)) | ||
fun <T> ObservablePoint<T>.rounded() = clone().set(x.roundToInt().unsafeCast<Double>(), y.roundToInt().unsafeCast<Double>()) | ||
|
||
|
||
fun <T> ObservablePoint<T>.max(vararg points: IPointData) = clone().set(maxOf(x, *points.map { it.x }.toDoubleArray()), maxOf(y, *points.map { it.y }.toDoubleArray())) | ||
fun <T> ObservablePoint<T>.min(vararg points: IPointData) = clone().set(minOf(x, *points.map { it.x }.toDoubleArray()), minOf(y, *points.map { it.y }.toDoubleArray())) | ||
fun <T> ObservablePoint<T>.clamp(min: Number, max: Number) = clone().set(x.coerceIn(min.unsafeCast<Double>(), max.unsafeCast<Double>()), y.coerceIn(min.unsafeCast<Double>(), max.unsafeCast<Double>())) | ||
fun <T> ObservablePoint<T>.clamp(min: IPointData, max: IPointData) = clone().set(x.coerceIn(min.x, max.x), y.coerceIn(min.y, max.y)) | ||
|
||
fun <T> ObservablePoint<T>.middle(other: IPointData) = clone().set(x + (other.x - x) / 2, y + (other.y - y) / 2) | ||
|
||
fun <T> ObservablePoint<T>.normalize(): ObservablePoint<T> { | ||
val len = length | ||
|
||
return if (len != 0.0) clone().set(x / len, y / len) | ||
else clone().set(0.0, 0.0) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package pixi.externals.extensions | ||
|
||
import pixi.typings.math.IPointData | ||
import pixi.typings.math.Point | ||
import kotlin.math.ceil | ||
import kotlin.math.floor | ||
import kotlin.math.roundToInt | ||
|
||
operator fun Point.plus(other: IPointData) = Point(x + other.x, y + other.y) | ||
operator fun Point.plus(factor: Number) = Point(x + factor.unsafeCast<Double>(), y + factor.unsafeCast<Double>()) | ||
|
||
operator fun Point.minus(other: IPointData) = Point(x - other.x, y - other.y) | ||
operator fun Point.minus(factor: Number) = Point(x - factor.unsafeCast<Double>(), y - factor.unsafeCast<Double>()) | ||
|
||
operator fun Point.times(other: IPointData) = Point(x * other.x, y * other.y) | ||
operator fun Point.times(factor: Number) = Point(x * factor.unsafeCast<Double>(), y * factor.unsafeCast<Double>()) | ||
|
||
operator fun Point.div(other: IPointData) = Point(x / other.x, y / other.y) | ||
operator fun Point.div(factor: Number) = Point(x / factor.unsafeCast<Double>(), y / factor.unsafeCast<Double>()) | ||
|
||
operator fun Point.rem(other: IPointData) = Point(x % other.x, y % other.y) | ||
operator fun Point.rem(factor: Number) = Point(x % factor.unsafeCast<Double>(), y % factor.unsafeCast<Double>()) | ||
|
||
fun Point.floored() = Point(floor(x), floor(y)) | ||
fun Point.ceiled() = Point(ceil(x), ceil(y)) | ||
fun Point.rounded() = Point(x.roundToInt().unsafeCast<Double>(), y.roundToInt().unsafeCast<Double>()) | ||
|
||
fun Point.max(vararg points: IPointData) = Point(maxOf(x, *points.map { it.x }.toDoubleArray()), maxOf(y, *points.map { it.y }.toDoubleArray())) | ||
fun Point.min(vararg points: IPointData) = Point(minOf(x, *points.map { it.x }.toDoubleArray()), minOf(y, *points.map { it.y }.toDoubleArray())) | ||
fun Point.clamp(min: Number, max: Number) = Point(x.coerceIn(min.unsafeCast<Double>(), max.unsafeCast<Double>()), y.coerceIn(min.unsafeCast<Double>(), max.unsafeCast<Double>())) | ||
fun Point.clamp(min: IPointData, max: IPointData) = Point(x.coerceIn(min.x, max.x), y.coerceIn(min.y, max.y)) | ||
|
||
fun Point.middle(other: IPointData) = Point((x + other.x) / 2, (y + other.y) / 2) | ||
|
||
fun Point.normalize(): Point { | ||
val len = length | ||
|
||
return if (len != 0.0) Point(x / len, y / len) | ||
else Point(0.0, 0.0) | ||
} |