Skip to content

Commit e09a5ba

Browse files
committed
Add clamped
1 parent 77889f0 commit e09a5ba

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

Sources/GameMath/2D Types/Rect.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,20 @@ public extension Rect {
149149
copy.height -= insets.top + insets.bottom
150150
return copy
151151
}
152+
153+
@_transparent
154+
func clamped(within rect: Rect) -> Rect {
155+
var copy = self
156+
copy.x = max(rect.x, self.x)
157+
copy.y = max(rect.y, self.y)
158+
if copy.maxX > rect.maxX {
159+
copy.width = rect.maxX - copy.x
160+
}
161+
if copy.maxY > rect.maxY {
162+
copy.height = rect.maxY - copy.y
163+
}
164+
return copy
165+
}
152166
}
153167

154168
extension Rect {

0 commit comments

Comments
 (0)