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
79
80
81
82
83
84
85
86
87
|
Rectangle = {}
Rectangle.meta = {
__index = Rectangle,
__tostring = function( r )
return "{"..tostring( r.x )..", "..tostring( r.y )..", "..tostring( r.width )..", "..tostring( r.height ).."}"
end,
-- __add = function( v1, v2 )
-- return Vector2:new( v1.x + v2.x, v1.y + v2.y )
-- end,
-- __sub = function( v1, v2 )
-- return Vector2:new( v1.x - v2.x, v1.y - v2.y )
-- end,
-- __mul = function( v1, v2 )
-- return Vector2:new( v1.x * v2.x, v1.y * v2.y )
-- end,
-- __div = function( v1, v2 )
-- return Vector2:new( v1.x / v2.x, v1.y / v2.y )
-- end,
-- __mod = function( v, value )
-- return Vector2:new( math.fmod( v.x, value ), math.fmod( v.y, value ) )
-- end,
-- __pow = function( v, value )
-- return Vector2:new( v.x ^ value, v.y ^ value )
-- end,
-- __unm = function( v )
-- return Vector2:new( -v.x, -v.y )
-- end,
-- __idiv = function( v, value )
-- return Vector2:new( v.x // value, v.y // value )
-- end,
-- __len = function( v )
-- local len = 0
-- for _, _ in pairs( v ) do
-- len = len + 1
-- end
-- return len
-- end,
-- __eq = function( v1, v2 )
-- return v1.x == v2.x and v1.y == v2.y
-- end,
}
function Rectangle:new( x, y, width, height )
if type( x ) == "table" then
x, y, width, height = table.unpack( x )
elseif type( x ) == "nil" then
x, y, width, height = 0, 0, 0, 0
end
local o = {
x = x,
y = y,
width = width,
height = height,
}
setmetatable( o, Rectangle.meta )
return o
end
function Rectangle:set( x, y, width, height )
if type( x ) == "table" then
x, y, width, height = table.unpack( x )
elseif type( x ) == "nil" then
x, y, width, height = 0, 0, 0, 0
end
self.x = x
self.y = y
self.width = width
self.height = height
end
function Rectangle:arr()
return { self.x, self.y, self.width, self.height }
end
function Rectangle:unpack()
return self.x, self.y, self.width, self.height
end
function Rectangle:clone()
return Rectangle:new( self.x, self.y, self.width, self.height )
end
return Rectangle
|