-
Notifications
You must be signed in to change notification settings - Fork 2
/
rect.lua
160 lines (132 loc) · 5.18 KB
/
rect.lua
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
--[[
#########################################################################
# #
# rect.lua #
# #
# 2D rectangle operations #
# #
# Copyright 2011 Josh Bothun #
# joshbothun@gmail.com #
# http://minornine.com #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License <http://www.gnu.org/licenses/> for #
# more details. #
# #
#########################################################################
--]]
require 'math'
require 'leaf.object'
require 'leaf.vector'
local vector = leaf.vector
local rect = {}
function rect.new(a, b, c, d)
-- If only two values passed, assume right and bottom
if c == nil then
return rect.new(0, 0, a, b)
end
return {
left = a or 0,
top = b or 0,
right = c or 0,
bottom = d or 0,
}
end
function rect.width(left, top, right, bottom)
return right - left
end
function rect.height(left, top, right, bottom)
return bottom - top
end
function rect.size(left, top, right, bottom)
return rect.width(left, top, right, bottom),
rect.height(left, top, right, bottom)
end
function rect.area(left, top, right, bottom)
return rect.width(left, top, right, bottom) *
rect.height(left, top, right, bottom)
end
-- Return the center coordinate of the rectangle
function rect.center(left, top, right, bottom)
local w, h = rect.size(left, top, right, bottom)
return left + w / 2, top + h / 2
end
function rect.translate(left, top, right, bottom, x, y)
local left, top = vector.translate(left, top, x, y)
local right, bottom = vector.translate(right, bottom, x, y)
return left, top, right, bottom
end
function rect.scale(left, top, right, bottom, sx, sy)
local sy = sy or sx
local w, h = rect.size(left, top, right, bottom)
return left, top, left + w * sx, top + h * sy
end
function rect.scaleCenter(left, top, right, bottom, sx, sy)
local nleft, ntop, nright, nbottom = rect.scale(left, top, right, bottom, sx, sy)
local dx, dy = nright - right, nbottom - bottom
return rect.translate(nleft, ntop, nright, nbottom, -dx / 2, -dy / 2)
end
-- Convert to an x, y, w, h rectangle
function rect.toRel(left, top, right, bottom)
return left, top, rect.size(left, top, right, bottom)
end
-- Convert to a left, top, right, bottom rectangle
function rect.toAbs(left, top, width, height)
return left, top, left + width, top + height
end
-- Check if a rect contains a point
function rect.contains(left, top, right, bottom, a, b)
if type(a) == 'table' then
-- Assume a is vector table
return rect.contains(left, top, right, bottom, a.x, a.y)
end
return a >= left and a <= right and b >= top and b <= bottom
end
-- Check if rect intersects another rect
local function axis_overlaps(head, tail, head2, tail2)
return head >= head2 and head <= tail2 or
head2 >= head and head2 <= tail
end
function rect.intersects(left, top, right, bottom, a, b, c, d)
if type(a) == 'table' then
-- Assume a is a rect table
return rect.intersects(left, top, right, bottom,
a.left, a.top, a.right, a.bottom)
end
return axis_overlaps(left, right, a, c) and axis_overlaps(top, bottom, b, d)
end
-- Wrap all rect methods to accept either a table or flat args
for k, v in pairs(rect) do
rect[k] = function(a, b, c, d, ...)
if type(a) == 'table' then
return v(a.left, a.top, a.right, a.bottom, b, c, d, ...)
end
return v(a, b, c, d, ...)
end
end
-- Must pass a table to unpack
function rect.unpack(r)
return r.left, r.top, r.right, r.bottom
end
-- Define in-place functions
for _, key in ipairs({"translate", "scale", "scaleCenter"}) do
rect[key .. "_i"] = function(r, ...)
r.left, r.top, r.right, r.bottom = rect[key](r, ...)
return r
end
end
-- Call shortcut
setmetatable(rect, {
__call = function(self, ...)
return rect.new(...)
end,
})
-- Namespace exports
leaf.rect = rect