Code: Select all
function getAllUniquePoints ()
local map, uniquePoints = {}, {}
for i, segment in ipairs (segments) do
for j = 1, #segment-1, 2 do
local x, y = segment[j], segment[j+1]
if map[x] then
map[x][y] = true
else
map[x] = {[y] = true}
end
end
end
for x, ys in pairs (map) do
for y, bool in pairs (ys) do
table.insert (uniquePoints, {x=x,y=y})
end
end
return uniquePoints
end
function love.load()
love.window.setMode(640*2, 360*2, {resizable=true, borderless=false})
segments = {
-- border
{0,0, 640,0, 640,360, 0,360},
-- #1
{100,150, 120,50, 200,80, 140,210},
-- #2
{100,200, 120,250, 60, 300},
-- #3
{200,260, 220,150, 300,200, 350,320},
-- #4
{340,60, 360,40, 370,70},
-- #5
{450,190, 560,170, 540,270, 430,290},
-- #6
{400,95, 580,50, 450,150},
}
uniquePoints = getAllUniquePoints ()
end
function love.draw()
-- draw segments (polygons)
love.graphics.scale (2)
love.graphics.setColor (1,1,1)
love.graphics.setLineWidth (2)
for i, polygon in ipairs (segments) do
love.graphics.polygon('line', polygon)
end
local mx, my = love.mouse.getPosition()
mx, my = mx/2, my/2
-- lines to uniquePoints
love.graphics.setLineWidth (0.5)
for i, point in ipairs (uniquePoints) do
love.graphics.line (mx, my, point.x, point.y)
end
end