summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--city/geometry-test.lua37
-rw-r--r--city/geometry.lua18
2 files changed, 52 insertions, 3 deletions
diff --git a/city/geometry-test.lua b/city/geometry-test.lua
index 758ce95..d48abbc 100644
--- a/city/geometry-test.lua
+++ b/city/geometry-test.lua
@@ -132,6 +132,43 @@ test(
end
)
+test(
+ 'intersect various squares',
+ function()
+ local center = geom.point(0, 0)
+ local square = geom.square(center, 2)
+
+ -- overlap x/y
+ assert(square:intersects(
+ geom.square(geom.point(1, 1), 2)))
+
+ -- nest inside
+ assert(square:intersects(
+ geom.square(geom.point(0, 0), 1)))
+
+ -- nest outside
+ assert(square:intersects(
+ geom.square(geom.point(0, 0), 5)))
+
+ -------- non-intersections --------
+
+ -- share x on left
+ assert(not square:intersects(
+ geom.square(geom.point(-2, 0), 2)))
+
+ -- share x on right
+ assert(not square:intersects(
+ geom.square(geom.point(2, 0), 2)))
+
+ -- share y on top
+ assert(not square:intersects(
+ geom.square(geom.point(0, -2), 2)))
+
+ -- share y on bottom
+ assert(not square:intersects(
+ geom.square(geom.point(0, 2), 2)))
+ end
+)
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
--
diff --git a/city/geometry.lua b/city/geometry.lua
index 5893731..a24e508 100644
--- a/city/geometry.lua
+++ b/city/geometry.lua
@@ -62,15 +62,27 @@ geom.square = class{
return false
end,
+ -- check if the square intersects the given square
+ intersects = function(self, square)
+ local overlap = function(a, b)
+ return math.max(a.min, b.min) < math.min(a.max, b.max)
+ end
+ local x_overlap = overlap(self.x, square.x)
+ local y_overlap = overlap(self.y, square.y)
+
+ if x_overlap and y_overlap then 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),
@@ -105,7 +117,7 @@ geom.qt_node = class{
-- point isn't even in this region
return false
end
-
+
if self:is_leaf() and not self.point then
-- no preexisting point, just store it
self.point = point