--Load and save plaintext line mesh format for DEFCON coastlines etc. local t = {} local lfs = love.filesystem local lg = love.graphics local polygon = { x = 180, X = -180, y = 100, Y = -100 } --default empty bounding box local polymt = { __index = polygon } function polygon:formatDisplayInfo() return ([[ x: %f y: %f X: %f Y: %f N: %d]]):format( self.x, self.y, self.X, self.Y, #self ) end function polygon:drawDirection() local a,b,c,d = self[1], self[2], self[3], self[4] local bx, by = (c + a) / 2, (b + d) / 2 local dx, dy = c - a, d - b local r = math.max( math.sqrt( dy * dy + dx * dx ), 0.0001 ) dx, dy = dx / r, dy / r lg.polygon( "fill", a + dx, b + dy, a - 0.4 * dy, b + 0.4 * dx, a + 0.4 * dy, b - 0.4 * dx ) end function t.load( filename ) local polys = { visible = true, filename = filename } local poly = {} local n = 1 local k = 1 for line in assert( lfs.lines( filename ) ) do if line:find "b" then k = 1 if #poly > 2 then n = n + 1 end poly = setmetatable({}, polymt) --axis-aligned bounding box polys[n] = poly else local _, _, x, y = line:find( "(%g+)%s+(%g+)" ) x, y = tonumber( x ), tonumber( y ) if x and y then poly[k], poly[ k + 1 ] = x, y k = k + 2 if x < poly.x then poly.x = x end if x > poly.X then poly.X = x end if y < poly.y then poly.y = y end if y > poly.Y then poly.Y = y end else print( "LINES: malformed line:", filename, line ) end end end if not polys[n] or (#(polys[n]) < 3) then polys[n] = nil n = n - 1 end print( "LOADED", filename, n ) return setmetatable( polys, {__index = t } ) end function t.selectNearest( lines, wx, wy ) local d = math.huge local nearest for i, poly in ipairs( lines ) do if poly.x - 5 < wx and poly.X + 5 > wx and poly.y - 5 < wy and poly.Y + 5 > wy then for k = 1, #poly, 2 do local x, y = poly[k], poly[k + 1] local r = ( x - wx ) * ( x - wx ) + ( y - wy ) * ( y - wy ) if r < d then d = r nearest = poly end end end end return nearest end function t.save( lines ) local str = { "b" } for i, poly in ipairs( lines ) do str[i + 1] = table.concat( poly, " " ) end str = table.concat( str, "\nb\n" ):gsub("(%S+) (%S+) ", "%1 %2\n") return str end function t.newPolygon( x, y ) end function t.addToCurrentPolygon( x, y ) end function t.deletePolygon( poly ) end function t.drawPolygonEditHandles( poly ) end function t.draw( lines ) if not lines.visible then return end for _, poly in ipairs( lines ) do lg.line( poly ) end end return t