summaryrefslogtreecommitdiff
path: root/composeApp/src/commonMain/kotlin/coffee/liz/ecs/Vec.kt
blob: 0b771e47f92466331c1722757e2afa3b10878a7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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)
}