From 6adef84355d5c232d7a4165b63336c19d88acb75 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Fri, 19 Apr 2024 14:16:33 +0700 Subject: [PATCH] AABB#withSide --- gradle.properties | 2 +- .../ru/dbotthepony/kommons/util/AABB.kt | 16 +++++++++- .../ru/dbotthepony/kommons/util/AABBi.kt | 29 +++++++++++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/gradle.properties b/gradle.properties index 750524d..1398e06 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,7 +4,7 @@ kotlin.code.style=official specifyKotlinAsDependency=false projectGroup=ru.dbotthepony.kommons -projectVersion=2.14.0 +projectVersion=2.14.1 guavaDepVersion=33.0.0 gsonDepVersion=2.8.9 diff --git a/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/util/AABB.kt b/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/util/AABB.kt index 3e1b8a9..fdc10d6 100644 --- a/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/util/AABB.kt +++ b/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/util/AABB.kt @@ -201,6 +201,9 @@ data class AABB(val mins: Vector2d, val maxs: Vector2d) { } companion object { + /** + * Rectangle with given [width] and [height] + */ fun rectangle(pos: IStruct2d, width: Double, height: Double = width): AABB { val (x, y) = pos @@ -210,7 +213,18 @@ data class AABB(val mins: Vector2d, val maxs: Vector2d) { ) } + /** + * Rectangle with given [width] * 2 and [height] * 2 + */ + fun withSide(pos: IStruct2d, width: Double, height: Double = width): AABB { + val (x, y) = pos + + return AABB( + Vector2d(x - width, y - height), + Vector2d(x + width, y + height), + ) + } + @JvmField val ZERO = AABB(Vector2d.ZERO, Vector2d.ZERO) } } - diff --git a/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/util/AABBi.kt b/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/util/AABBi.kt index 9c38486..b3366f9 100644 --- a/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/util/AABBi.kt +++ b/linear-algebra/src/main/kotlin/ru/dbotthepony/kommons/util/AABBi.kt @@ -2,6 +2,7 @@ package ru.dbotthepony.kommons.util import ru.dbotthepony.kommons.math.intersectRectangles import ru.dbotthepony.kommons.math.rectangleContainsRectangle +import ru.dbotthepony.kommons.vector.Vector2d import ru.dbotthepony.kommons.vector.Vector2i data class AABBi(val mins: Vector2i, val maxs: Vector2i) { @@ -115,4 +116,32 @@ data class AABBi(val mins: Vector2i, val maxs: Vector2i) { return AABBi(Vector2i(minX, minY), Vector2i(maxX, maxY)) } + + companion object { + /** + * Rectangle with given [width] and [height] + */ + fun rectangle(pos: IStruct2i, width: Int, height: Int = width): AABBi { + val (x, y) = pos + + return AABBi( + Vector2i(x - width / 2, y - height / 2), + Vector2i(x + width / 2, y + height / 2), + ) + } + + /** + * Rectangle with given [width] * 2 and [height] * 2 + */ + fun withSide(pos: IStruct2i, width: Int, height: Int = width): AABBi { + val (x, y) = pos + + return AABBi( + Vector2i(x - width, y - height), + Vector2i(x + width, y + height), + ) + } + + @JvmField val ZERO = AABBi(Vector2i.ZERO, Vector2i.ZERO) + } }