diff options
| author | sanine <sanine.not@pm.me> | 2022-02-25 09:42:55 -0600 | 
|---|---|---|
| committer | sanine <sanine.not@pm.me> | 2022-02-25 09:42:55 -0600 | 
| commit | c167a029faf0d381c546f90abc9ed2a3bc96f94d (patch) | |
| tree | 71b0bf872c4d7698759ada74a55b1730bf704220 /city | |
| parent | 5335b0669f5c7c96bbb532af53b499c3f73a68c7 (diff) | |
add square:contains() function
Diffstat (limited to 'city')
| -rw-r--r-- | city/geometry-test.lua | 56 | ||||
| -rw-r--r-- | city/geometry.lua | 12 | 
2 files changed, 61 insertions, 7 deletions
| diff --git a/city/geometry-test.lua b/city/geometry-test.lua index 3fedae6..f85bec0 100644 --- a/city/geometry-test.lua +++ b/city/geometry-test.lua @@ -40,14 +40,56 @@ test(  test(     'create square object',     function() -      local s = geom.square(geom.point(0, 0), 1) -      assert(s.center.x == 0) -      assert(s.center.y == 0) +      local s = geom.square(geom.point(2, 5), 1) +      assert(s.center.x == 2) +      assert(s.center.y == 5)        assert(s.span == 1) -      assert(s.x.min == -0.5) -      assert(s.x.max ==  0.5) -      assert(s.y.min == -0.5) -      assert(s.y.max ==  0.5) +      assert(s.x.min ==  1.5) +      assert(s.x.max ==  2.5) +      assert(s.y.min ==  4.5) +      assert(s.y.max ==  5.5) +   end +) + +test( +   'check if point is in square', +   function() +      local center = geom.point(1, 1) +      local square = geom.square(center, 2) + +      local interior_points = { +	 geom.point(1, 1), +	 geom.point(2, 2), +	 geom.point(2, 0.1), +	 geom.point(0.1, 0.1), +      } +      for _, point in ipairs(interior_points) do +	 assert( +	    square:contains(point), +	    string.format( +	       'point (%0.2f, %0.2f) was incorrectly outside!', +	       point.x, point.y +	    ) +	 ) +      end + +      local exterior_points = { +	 geom.point(0, 0), +	 geom.point(10, 10), +	 geom.point(0, 1), +	 geom.point(1, 0), +	 geom.point(-2, 1), +	 geom.point(1, -2), +      } +      for _, point in ipairs(exterior_points) do +	 assert( +	    not square:contains(point), +	    string.format( +	       'point (%0.2f, %0.2f) was incorrectly inside!', +	       point.x, point.y +	    ) +	 ) +      end     end  ) diff --git a/city/geometry.lua b/city/geometry.lua index a775213..1632393 100644 --- a/city/geometry.lua +++ b/city/geometry.lua @@ -46,6 +46,18 @@ geom.square = class{     end,     axis_range = function(c, r) return {min=c-r, max=c+r} end, + +   contains = function(self, point) +      local x_overlap = +	 (point.x > self.x.min) and (point.x <= self.x.max) +      local y_overlap = +	 (point.y > self.y.min) and (point.y <= self.y.max) + +      if x_overlap and y_overlap then +	 return true +      end +      return false +   end  }  return geom | 
