Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue/809/rectangle min max size #810

Merged
merged 2 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,46 @@ final case class Rectangle(position: Point, size: Size) derives CanEqual:
def moveTo(x: Int, y: Int): Rectangle =
moveTo(Point(x, y))

/** Resize the rectangle to the given size or the current size, whichever is smaller.
*/
def min(value: Size): Rectangle =
this.copy(size = size.min(value))

/** Resize the rectangle to the given size or the current size, whichever is smaller.
*/
def min(width: Int, height: Int): Rectangle =
min(Size(width, height))

/** Ensure the rectangle is at least, the given size.
*/
def minSize(min: Size): Rectangle =
max(min)

/** Ensure the rectangle is at least, the given size.
*/
def minSize(width: Int, height: Int): Rectangle =
minSize(Size(width, height))

/** Resize the rectangle to the given size or the current size, whichever is larger.
*/
def max(value: Size): Rectangle =
this.copy(size = size.max(value))

/** Resize the rectangle to the given size or the current size, whichever is larger.
*/
def max(width: Int, height: Int): Rectangle =
max(Size(width, height))

/** Ensure the rectangle is at most, the given size.
*/
def maxSize(max: Size): Rectangle =
min(max)

/** Ensure the rectangle is at most, the given size.
*/
def maxSize(width: Int, height: Int): Rectangle =
maxSize(Size(width, height))

def resize(newSize: Size): Rectangle =
this.copy(size = newSize)
def resize(x: Int, y: Int): Rectangle =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,4 +265,28 @@ class RectangleTests extends munit.FunSuite {
assertEquals(Rectangle(10, 10, 10, 10).resizeBy(20, 20), Rectangle(10, 10, 30, 30))
assertEquals(Rectangle(10, 10, 10, 10).resizeBy(20), Rectangle(10, 10, 30, 30))
}

test("min") {
assertEquals(Rectangle(10, 10, 10, 10).min(20, 20), Rectangle(10, 10, 10, 10))
assertEquals(Rectangle(10, 10, 10, 10).min(5, 6), Rectangle(10, 10, 5, 6))
assertEquals(Rectangle(10, 10, 10, 10).min(5, 20), Rectangle(10, 10, 5, 10))
}

test("minSize") {
assertEquals(Rectangle(10, 10, 10, 10).minSize(Size(20, 20)), Rectangle(10, 10, 20, 20))
assertEquals(Rectangle(10, 10, 10, 10).minSize(20, 20), Rectangle(10, 10, 20, 20))
assertEquals(Rectangle(10, 10, 10, 10).minSize(5, 20), Rectangle(10, 10, 10, 20))
}

test("max") {
assertEquals(Rectangle(10, 10, 10, 10).max(Size(20, 20)), Rectangle(10, 10, 20, 20))
assertEquals(Rectangle(10, 10, 10, 10).max(20, 20), Rectangle(10, 10, 20, 20))
assertEquals(Rectangle(10, 10, 10, 10).max(5, 20), Rectangle(10, 10, 10, 20))
}

test("maxSize") {
assertEquals(Rectangle(10, 10, 10, 10).maxSize(Size(20, 20)), Rectangle(10, 10, 10, 10))
assertEquals(Rectangle(10, 10, 10, 10).maxSize(5, 5), Rectangle(10, 10, 5, 5))
assertEquals(Rectangle(10, 10, 10, 10).maxSize(5, 20), Rectangle(10, 10, 5, 10))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,18 @@ class SizeTests extends munit.FunSuite {
assertEquals(clue(Size.mod(Size(-11), Size(-10))), clue(Size(-1)))
}

test("min") {
assertEquals(Size(11, 12).min(Size(10, 10)), Size(10, 10))
assertEquals(Size(11, 12).min(10), Size(10, 10))
assertEquals(Size(11, 12).min(Size(12, 10)), Size(11, 10))
assertEquals(Size(11, 12).min(5), Size(5, 5))
}

test("max") {
assertEquals(Size(11, 12).max(Size(10, 10)), Size(11, 12))
assertEquals(Size(11, 12).max(10), Size(11, 12))
assertEquals(Size(11, 12).max(Size(12, 10)), Size(12, 12))
assertEquals(Size(11, 12).max(12), Size(12, 12))
}

}
Loading