add a couple more procs, mostly for rect
This commit is contained in:
parent
56a0e6ea2d
commit
5bd74ecbf5
1 changed files with 22 additions and 4 deletions
|
@ -11,6 +11,10 @@ proc clamp*(n, min, max: float32): float32 =
|
||||||
return max
|
return max
|
||||||
return n
|
return n
|
||||||
|
|
||||||
|
proc between*(value, min, max: float32): bool =
|
||||||
|
## Returns true if value is between min and max or equal to them.
|
||||||
|
(value >= min) and (value <= max)
|
||||||
|
|
||||||
proc sign*(v: float32): float32 =
|
proc sign*(v: float32): float32 =
|
||||||
## Returns the sign of a number, -1 or 1.
|
## Returns the sign of a number, -1 or 1.
|
||||||
if v >= 0:
|
if v >= 0:
|
||||||
|
@ -1398,10 +1402,12 @@ proc `/`*(r: Rect, v: float): Rect =
|
||||||
## / all elements of a Rect.
|
## / all elements of a Rect.
|
||||||
rect(r.x / v, r.y / v, r.w / v, r.h / v)
|
rect(r.x / v, r.y / v, r.w / v, r.h / v)
|
||||||
|
|
||||||
proc intersects*(rect: Rect, pos: Vec2): bool =
|
proc `+`*(a, b: Rect): Rect =
|
||||||
## Checks if pos is inside rect.
|
## Add two boxes together.
|
||||||
(rect.x <= pos.x and pos.x <= rect.x + rect.w) and (
|
result.x = a.x + b.x
|
||||||
rect.y <= pos.y and pos.y <= rect.y + rect.h)
|
result.y = a.y + b.y
|
||||||
|
result.w = a.w
|
||||||
|
result.h = a.h
|
||||||
|
|
||||||
proc `$`*(a: Rect): string =
|
proc `$`*(a: Rect): string =
|
||||||
return "(" &
|
return "(" &
|
||||||
|
@ -1409,3 +1415,15 @@ proc `$`*(a: Rect): string =
|
||||||
$a.y & ": " &
|
$a.y & ": " &
|
||||||
$a.w & " x " &
|
$a.w & " x " &
|
||||||
$a.h & ")"
|
$a.h & ")"
|
||||||
|
|
||||||
|
proc inside*(pos: Vec2, rect: Rect): bool =
|
||||||
|
## Checks if pos is inside rect.
|
||||||
|
(rect.x <= pos.x and pos.x <= rect.x + rect.w) and (
|
||||||
|
rect.y <= pos.y and pos.y <= rect.y + rect.h)
|
||||||
|
|
||||||
|
proc overlap*(a, b: Rect): bool =
|
||||||
|
## Returns true if box a overlaps box b.
|
||||||
|
let
|
||||||
|
xOverlap = between(a.x, b.x, b.x + b.w) or between(b.x, a.x, a.x + a.w)
|
||||||
|
yOverlap = between(a.y, b.y, b.y + b.h) or between(b.y, a.y, a.y + a.h)
|
||||||
|
return xOverlap and yOverlap
|
||||||
|
|
Loading…
Reference in a new issue