summaryrefslogtreecommitdiff
path: root/city/geometry.lua
diff options
context:
space:
mode:
authorsanine <sanine.not@pm.me>2022-02-25 10:03:47 -0600
committersanine <sanine.not@pm.me>2022-02-25 10:03:47 -0600
commitb10141db0f63429111f6c82c85ccb921723b5b82 (patch)
tree1cbbfb54690acb1d4f053b55764ef611cec6b9e3 /city/geometry.lua
parentc167a029faf0d381c546f90abc9ed2a3bc96f94d (diff)
add square:divide() function
Diffstat (limited to 'city/geometry.lua')
-rw-r--r--city/geometry.lua20
1 files changed, 20 insertions, 0 deletions
diff --git a/city/geometry.lua b/city/geometry.lua
index 1632393..13088d1 100644
--- a/city/geometry.lua
+++ b/city/geometry.lua
@@ -47,6 +47,9 @@ geom.square = class{
axis_range = function(c, r) return {min=c-r, max=c+r} end,
+ -- returns true if a point is inside of the square
+ -- and false otherwise
+ -- (x/y mininum exclusive, maximum inclusive)
contains = function(self, point)
local x_overlap =
(point.x > self.x.min) and (point.x <= self.x.max)
@@ -57,6 +60,23 @@ geom.square = class{
return true
end
return false
+ end,
+
+ -- return an array of four squares covering the same
+ -- area as their parent
+ divide = function(self)
+ local this = getmetatable(self).__index
+
+ local x = self.center.x
+ local y = self.center.y
+ local d = self.span / 4
+
+ return {
+ this(geom.point(x-d, y-d), d*2),
+ this(geom.point(x+d, y-d), d*2),
+ this(geom.point(x-d, y+d), d*2),
+ this(geom.point(x+d, y+d), d*2),
+ }
end
}