diff options
| author | rebecca <ubq323@ubq323.website> | 2026-07-14 20:53:05 +0100 |
|---|---|---|
| committer | rebecca <ubq323@ubq323.website> | 2026-09-17 20:53:05 +0100 |
| commit | 7f60b7c4e8d99f97a0a24b60bd4c4fd15697a4bb (patch) | |
| tree | ee75cc0d975aa877a66bea097c484c3b0e89e10a /rect.lua | |
| parent | ec3b1b1c6f6ed364ece503588c780f3d6e564f10 (diff) | |
rect: add wh field, adjust has() comparisons
Diffstat (limited to 'rect.lua')
| -rw-r--r-- | rect.lua | 10 |
1 files changed, 7 insertions, 3 deletions
@@ -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) |
