local test = require('minunit').test local geom = require 'geometry' -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -- -- geom.point tests -- -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ test( 'create point object', function() local pt = geom.point(1, 5.5) assert(pt.x == 1) assert(pt.y == 5.5) local mt = getmetatable(pt) assert(mt.__index == geom.point) end ) test( 'point distances', function() local a = geom.point(1, 3) local b = geom.point(4, 7) assert(a:distance_to(b) == 5) assert(a:distance_to(a) == 0) end ) -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -- -- geom.square tests -- -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ test( 'create square object', function() 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 == 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 )