Skip to content

Commit

Permalink
update rect.intersects() & reorder params to a more natural way (x,y,…
Browse files Browse the repository at this point in the history
…x2,y2) (#293)
  • Loading branch information
LeHaine authored Dec 13, 2024
1 parent 9baa514 commit fa0f067
Showing 1 changed file with 7 additions and 16 deletions.
23 changes: 7 additions & 16 deletions core/src/commonMain/kotlin/com/littlekt/math/Rect.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ open class Rect(
/** The width of the rect. */
var width: Float = 0f,
/** The height of the rect. */
var height: Float = 0f
var height: Float = 0f,
) {

/** The right-most x-coordinate. This is the same as doing `x + width`. */
Expand All @@ -44,27 +44,18 @@ open class Rect(
}

/** @return true if this [Rect] intersects with the target [rect]. */
fun intersects(rect: Rect) = intersects(left = rect.x, rect.y2, rect.x2, rect.y)
fun intersects(rect: Rect) = intersects(left = rect.x, rect.y, rect.x2, rect.y2)

/**
* @param left the left-most coordinates
* @param top the top-most coordinates
* @param right the right=most coordinates
* @param bottom the bottom-most coordinates
* @param right the right=most coordinates
* @param top the top-most coordinates
* @return true if this [Rect] intersects the given rectangle at the specified corner
* coordinates.
*/
fun intersects(left: Float, top: Float, right: Float, bottom: Float): Boolean {
if (x >= right || left >= x2) {
return false
}

if (y >= bottom || top >= y2) {
return false
}

return true
}
fun intersects(left: Float, bottom: Float, right: Float, top: Float): Boolean =
!(x >= right || x2 <= left || y >= top || y2 <= bottom)

override fun toString(): String {
return "Rect(x=$x, y=$y, width=$width, height=$height, x2=$x2, y2=$y2)"
Expand Down Expand Up @@ -158,7 +149,7 @@ class RectBuilder {
x2: Float,
y2: Float,
x: Float,
y: Float
y: Float,
) {
val p0 = x0 to y0
val p1 = x1 to y1
Expand Down

0 comments on commit fa0f067

Please sign in to comment.