-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRectObj.lua
More file actions
78 lines (64 loc) · 1.44 KB
/
RectObj.lua
File metadata and controls
78 lines (64 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
-- RectObj.lua
-- RectObj is a helper class that is used by things that have dimensions and angles
RectObj = class(PositionObj)
function RectObj:init(x,y,w,h)
PositionObj.init(self,x,y)
self.w = w
self.h = h
self.mode = CORNER
self.angle = 0 -- in rads
end
---------------------- SETTERS -----------------------
function RectObj:setMode(mode)
self.mode = mode
end
function RectObj:setSize(w,h)
self.w = w
self.h = h
self:moveCB()
end
-- ang in degrees
function RectObj:rotate(ang)
self.angle = self.angle + math.rad(ang)
self:moveCB()
end
function RectObj:setAngle(ang)
self.angle = math.rad(ang)
self:moveCB()
end
---------------------- GETTERS -----------------------
function RectObj:getW()
return self.w
end
function RectObj:getH()
return self.h
end
function RectObj:getSize()
return self.w,self.h
end
-- return value is in degrees
function RectObj:getAngle()
return math.deg(self.angle)
end
-- check if t={x=x,y=y} is inside this rectangle
function RectObj:inbounds(t)
local x1,y1,x2,y2 = self:boundingBox()
return (t.x>=x1 and t.y>=y1 and t.x<=x2 and t.y<=y2)
end
function RectObj:boundingBox()
local x,y = self.x,self.y
local w,h = self.w,self.h
if w < 0 then
w = -w
x = x - w
end
if h < 0 then
h = -h
y = y - h
end
if mode == CENTER then
x = x - w/2
y = y - h/2
end
return x,y,x+w,y+h
end