summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--rect.lua10
1 files changed, 7 insertions, 3 deletions
diff --git a/rect.lua b/rect.lua
index 0a1b79c..1aedacd 100644
--- a/rect.lua
+++ b/rect.lua
@@ -7,6 +7,7 @@ function Rect.from_coords(cls, x0, y0, x1, y1)
return setmetatable({
tl=Pos:make(x0,y0),
br=Pos:make(x1,y1),
+ wh=Pos:make(x1-x0,y1-y0),
x0=x0, y0=y0, x1=x1, y1=y1,
w=x1-x0, h=y1-y0,
}, cls)
@@ -15,7 +16,7 @@ function Rect.from_pos(cls, tl, br)
assert(tl.x<br.x,"x coords interchanged")
assert(tl.y<br.y,"y coords interchanged")
return setmetatable({
- tl=tl, br=br,
+ tl=tl, br=br, wh=br-tl,
x0=tl.x, y0=tl.y, x1=br.x, y1=br.y,
w=br.x-tl.x, h=br.y-tl.y,
}, cls)
@@ -26,6 +27,7 @@ function Rect.from_xywh(cls, x0,y0, width,hight)
return setmetatable({
tl=Pos:make(x0,y0),
br=Pos:make(x0+width,y0+hight),
+ wh=Pos:make(width,hight),
x0=x0, y0=y0,
x1=x0+width,
y1=y0+hight,
@@ -40,6 +42,7 @@ function Rect.from_centre_dims(cls, centre, width, hight)
return setmetatable({
tl=Pos:make(x0,y0),
br=Pos:make(x1,y1),
+ wh=Pos:make(width,hight),
x0=x0,y0=y0,x1=x1,y1=y1,
w=width, h=hight,
}, cls)
@@ -47,8 +50,9 @@ end
Rect.from_cwh = Rect.from_centre_dims
function Rect.has(self, point)
- return self.x0 <= point.x and point.x <= self.x1
- and self.y0 <= point.y and point.y <= self.y1
+ -- should these comparisons be < or <=? does that depend on usecase?
+ return self.x0 <= point.x and point.x < self.x1
+ and self.y0 <= point.y and point.y < self.y1
end
function Rect.draw(self, mode)