Here’s another useless program. Each circle draws a fat line to its nearest neighbor. Tap the screen to add 5 more circles. I had nothing better to do.
displayMode(FULLSCREEN)
function setup()
tab={}
FPS=60
create()
end
function create()
for z=1,5 do
local sp=2
local t={}
t.x=math.random(WIDTH)
t.y=math.random(HEIGHT)
t.xv=math.random(-sp,sp)
t.yv=math.random(-sp,sp)
t.dist=0 -- nearest neighbor distance
t.x1=0 -- nearest neighbor x,y
t.y1=0
t.col=color(math.random(255),math.random(255),math.random(255))
table.insert(tab,t)
end
end
function draw()
background(80, 80, 80, 54)
fill(255)
text("Tap screen for 5 more circles",WIDTH/2,HEIGHT-20)
text("Circles "..#tab,WIDTH/2,HEIGHT-40)
text("FPS "..FPS//1,WIDTH/2,HEIGHT-60)
-- find nearest neighbor
for a,b in pairs(tab) do
local v1=vec2(b.x,b.y)
b.dist=9999
for c,d in pairs(tab) do
local v2=vec2(d.x,d.y)
local dst=v1:dist(v2)
if dst>0 and dst<b.dist then
b.dist=dst
b.x1=d.x
b.y1=d.y
end
end
end
-- draw lines
for a,b in pairs(tab) do
stroke(b.col)
strokeWidth(40)
line(b.x,b.y,b.x1,b.y1)
end
-- draw circles over lines
for a,b in pairs(tab) do
fill(0)
noStroke()
ellipse(b.x,b.y,10)
stroke(0)
strokeWidth(5)
line(b.x,b.y,b.x-(b.x-b.x1)/4,b.y-(b.y-b.y1)/4)
end
-- change position and reverse direction at edges
for a,b in pairs(tab) do
b.x=b.x+b.xv
b.y=b.y+b.yv
if b.x<0 or b.x>WIDTH then
b.xv=-b.xv
end
if b.y<0 or b.y>HEIGHT then
b.yv=-b.yv
end
end
FPS = FPS * 0.9 + 0.1 / DeltaTime
end
function touched(t)
if t.state==BEGAN then
create()
end
end