Skip to content

Commit

Permalink
Merge pull request #67 from JD557/center-rect
Browse files Browse the repository at this point in the history
Add centerAt method to Rect
  • Loading branch information
JD557 committed Nov 11, 2023
2 parents ef2e29d + 3687d3a commit 6715993
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
14 changes: 10 additions & 4 deletions core/src/main/scala/eu/joaocosta/interim/Rect.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import scala.annotation.targetName
* Alternatively, (x1, y1) is the top left coordinate and (x2, y2) is the bottom right one.
*/
final case class Rect(x: Int, y: Int, w: Int, h: Int):
def x1 = x
def y1 = y
def x2 = x + w
def y2 = y + h
def x1 = x
def y1 = y
def x2 = x + w
def y2 = y + h
def centerX = x + w / 2
def centerY = y + h / 2

/** Returns true if the rectangle has no area
*/
Expand All @@ -33,6 +35,10 @@ final case class Rect(x: Int, y: Int, w: Int, h: Int):
def resize(dw: Int, dh: Int): Rect =
copy(w = w + dw, h = h + dh)

/** Centers this rectangle at the defined position. */
def centerAt(x: Int, y: Int): Rect =
copy(x = x - w / 2, y = y - h / 2)

/** Shrinks this area by removing `size` pixels from each side.
*/
def shrink(size: Int): Rect =
Expand Down
9 changes: 9 additions & 0 deletions core/src/test/scala/eu/joaocosta/interim/RectSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ package eu.joaocosta.interim

class RectSpec extends munit.FunSuite:

test("compute helper positions"):
val rect = Rect(10, 15, 10, 20)
assertEquals(rect.x1, 10)
assertEquals(rect.x2, 20)
assertEquals(rect.y1, 15)
assertEquals(rect.y2, 35)
assertEquals(rect.centerX, 15)
assertEquals(rect.centerY, 25)

test("isMouseOver detects collisions with the mouse"):
val rect = Rect(10, 10, 10, 10)
assertEquals(rect.isMouseOver(using InputState(0, 0, false, "")), false)
Expand Down

0 comments on commit 6715993

Please sign in to comment.