summaryrefslogtreecommitdiff
path: root/composeApp/src/commonMain/kotlin/coffee/liz/ecs/Vec.kt
diff options
context:
space:
mode:
Diffstat (limited to 'composeApp/src/commonMain/kotlin/coffee/liz/ecs/Vec.kt')
-rw-r--r--composeApp/src/commonMain/kotlin/coffee/liz/ecs/Vec.kt33
1 files changed, 33 insertions, 0 deletions
diff --git a/composeApp/src/commonMain/kotlin/coffee/liz/ecs/Vec.kt b/composeApp/src/commonMain/kotlin/coffee/liz/ecs/Vec.kt
new file mode 100644
index 0000000..0b771e4
--- /dev/null
+++ b/composeApp/src/commonMain/kotlin/coffee/liz/ecs/Vec.kt
@@ -0,0 +1,33 @@
+package coffee.liz.ecs
+
+/**
+ * Rectangle in cartesian space.
+ */
+data class Rect(
+ val topLeft: Vec2,
+ val dimensions: Vec2
+) {
+ fun overlaps(other: Rect): Boolean {
+ val xOverlap = topLeft.x <= other.topLeft.x + other.dimensions.x &&
+ topLeft.x + dimensions.x >= other.topLeft.x
+ val yOverlap = topLeft.y <= other.topLeft.y + other.dimensions.y &&
+ topLeft.y + dimensions.y >= other.topLeft.y
+ return xOverlap && yOverlap
+ }
+}
+
+/**
+ * Cartesian point.
+ */
+data class Vec2(
+ val x: Float,
+ val y: Float
+) {
+ fun plus(other: Vec2): Vec2 = Vec2(x + other.x, y + other.y)
+ fun minus(other: Vec2): Vec2 = Vec2(x - other.x, y - other.y)
+ fun times(scalar: Float): Vec2 = Vec2(x * scalar, y * scalar)
+ fun div(scalar: Float): Vec2 = Vec2(x / scalar, y / scalar)
+ fun distanceTo(other: Vec2): Float = (this.minus(other)).length()
+ fun normalize(): Vec2 = this.div(length())
+ fun length(): Float = kotlin.math.sqrt(x * x + y * y)
+} \ No newline at end of file