-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgeometry.lua
76 lines (70 loc) · 2.24 KB
/
geometry.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
local M = {}
------------------------------------------------------------
-- Public: checks if the given point is inside the given container
--
-- Assumptions:
-- The container has the properties: top, left, bottom, right
-- The point has the properties x, y
--
-- Returns:
-- true if the point is inside the container
-- false if not
------------------------------------------------------------
local pointIsInside = function(point, rectangle)
if (
(point.x >= rectangle.left) and
(point.x <= rectangle.right) and
(point.y >= rectangle.top) and
(point.y <= rectangle.bottom)
) then
return true
else
return false
end
end
M.pointIsInside = pointIsInside
------------------------------------------------------------
-- Public: checks if the given rectangle is completely
-- inside the given rectangular container
--
-- Assumptions:
-- Both container and object have the properties: top, left, bottom, right
--
-- Returns:
-- true if the both (top,left) and (bottom,right)
-- of the rectangle are inside the container
-- false otherwise
------------------------------------------------------------
local rectIsInside = function(object, container)
if pointIsInside({x = object.left, y = object.top}, container) and
pointIsInside({x = object.right, y = object.bottom}, container) then
return true
else
return false
end
end
M.rectIsInside = rectIsInside
------------------------------------------------------------
-- Public: checks if the given rectangle is at at least partially
-- inside the given rectangular container
--
-- Assumption:
-- Both container and object have the properties: top, left, bottom, right
--
-- Returns:
-- true if at least one of the corners of the rectangle
-- is inside the container
-- false otherwise
------------------------------------------------------------
local rectIsPartiallyInside = function(object, container)
if pointIsInside({x = object.left, y = object.top}, container) or
pointIsInside({x = object.right, y = object.top}, container) or
pointIsInside({x = object.left, y = object.bottom}, container) or
pointIsInside({x = object.right, y = object.bottom}, container) then
return true
else
return false
end
end
M.rectIsPartiallyInside = rectIsPartiallyInside
return M