[orx-shapes] Add Rectangle.intersection()

This commit is contained in:
Edwin Jakobs
2024-09-25 22:26:05 +02:00
parent 585399244f
commit 6b9c3593ae
2 changed files with 62 additions and 0 deletions

View File

@@ -0,0 +1,23 @@
package org.openrndr.extra.shapes.primitives
import org.openrndr.shape.Rectangle
import kotlin.math.max
import kotlin.math.min
/**
* Find intersection of [this] and [other]
* @return a rectangle shaped intersection or [Rectangle.EMPTY] when the intersection is empty.
*/
fun Rectangle.intersection(other: Rectangle) : Rectangle = if (this.intersects(other)) {
val tn = this.normalized
val on = other.normalized
val left = max(tn.corner.x, on.corner.x)
val right = min(tn.corner.x + tn.width, on.corner.x + on.width)
val top = max(tn.corner.y, on.corner.y)
val bottom = min(tn.corner.y + tn.height, on.corner.y + on.height)
Rectangle(left, top, right - left, bottom - top)
} else {
Rectangle.EMPTY
}