20 line version of
http://forum.thegamecreators.com/?m=forum_view&t=37056&b=1
sync on : disable escapekey : randomize timer()
type pt_t
x as integer : y as integer
endtype
dim points(-1) as pt_t : last as pt_t : last.x = -1 : ink rgb(255,255,255),0 : box 0,0,screen width(),screen height() : ink rgb(0,0,0),0 : dim connect(15)as pt_t
repeat : mx = mousex() : my = mousey() : if mouseclick() = 1 : if last.x = -1 : last.x = mx : last.y = my : else : array insert at bottom points() : points(array count(points())).x = mx : points(array count(points())).y = my : last.x = mx : last.y = my : endif : else : last.x = -1 : endif
total = array count(points()) : if total > 2 : for i = 0 to 15 : rand = rnd(total) : connect(i).x = points(rand).x : connect(i).y = points(rand).y : next i : short = 1121 : for i = 0 to 15 : for ii = 0 to 15 : if i <> ii : x = abs(connect(i).x - connect(ii).x) : y = abs(connect(i).y - connect(ii).y) : dist = x + y : if dist < short : first = i : second = ii : short = dist : endif : endif : next ii : next i : line connect(first).x,connect(first).y,connect(second).x,connect(second).y : endif : sync : until escapekey()
`hello
Exploaded code
sync on : disable escapekey : randomize timer()
type pt_t
x as integer
y as integer
endtype
`array of points, every time a line is drawn its start and end point is added to this array
dim points(-1) as pt_t
last as pt_t
last.x = -1
mx as integer : my as integer
ink rgb(255,255,255),0
box 0,0,screen width(),screen height()
ink rgb(0,0,0),0
dim connect(15)as pt_t
repeat
mx = mousex()
my = mousey()
if mouseclick() = 1
`if a line is not in progress
if last.x = -1
last.x = mx
last.y = my
else
`line in progress so draw a line and add the point to the points() array
`line last.x,last.y,mx,my
array insert at bottom points()
points(array count(points())).x = mx
points(array count(points())).y = my
last.x = mx
last.y = my
endif
else
`make it so a line is not in progress as the mouse is released
last.x = -1
endif
total = array count(points())
if total > 2
`get some random points
for i = 0 to 15
rand = rnd(total)
connect(i).x = points(rand).x
connect(i).y = points(rand).y
next i
short = 1121 : `640 + 480 + 1
`find the two closest points
for i = 0 to 15
for ii = 0 to 15
if i <> ii
x = abs(connect(i).x - connect(ii).x)
y = abs(connect(i).y - connect(ii).y)
dist = x + y
if dist < short
first = i
second = ii
short = dist
endif
endif
next ii
next i
`draw line
line connect(first).x,connect(first).y,connect(second).x,connect(second).y
endif
sync
until escapekey()