What am I not getting? (See attached)
No, you will find for a square that the shortest distance is not drawing two diagonals.
So if the sides of the square are 10. Around the perimeter is 30, drawing two diagonals is 28.28 (approx). Surprisingly there is an even shorter way.
deflng a-z
const sw = 800
const sh = 600
dim shared pi as double
pi = 4*atn(1)
declare sub circlef(x, y, r, c)
n = 8
dim x(n), y(n)
'for i=0 to n-1
' x(i) = 200*cos(2*pi*i/n)
' y(i) = 200*sin(2*pi*i/n)
'next
x(0) = 0: y(0) = -200
x(1) = -30: y(1) = -30
x(2) = -200: y(2) = 0
x(3) = -30: y(3) = 30
x(4) = 0: y(4) = 200
x(5) = 30: y(5) = 30
x(6) = 200: y(6) = 0
x(7) = 30: y(7) = -30
x(n) = x(0)
y(n) = y(0)
'screenres sw, sh, 32
screen _newimage(sw, sh, 32)
redraw = -1
drag = 0
do
'm = getmouse(mx, my, mw, mb)
do
mx = _mousex
my = _mousey
mb = -_mousebutton(1)
loop while _mouseinput
if drag then
if mb = 1 then
x(ii) = mx - sw/2
y(ii) = sh/2 - my
else
drag = 0
end if
else
dmin = 1e10
for i=0 to n - 1
dx = (mx - sw/2) - x(i)
dy = (sh/2 - my) - y(i)
d = (dx*dx + dy*dy)
if d < dmin then
dmin = d
ii = i
end if
next
if mb = 1 then
omx = mx
omy = my
drag = -1
end if
end if
redraw = -1
a = 0
cx = 0
cy = 0
for i=0 to n-1
d = x(i)*y(i + 1) - x(i + 1)*y(i)
cx = cx + (x(i) + y(i + 1))*d
cy = cy + (y(i) + y(i + 1))*d
a = a + d
next
cx = cx/(3*a)
cy = cy/(3*a)
if redraw then
'screenlock
line (0,0)-(sw,sh),_rgb(0,0,0),bf
locate 1,1: ? m, mx, my, mw, mb
x(n) = x(0)
y(n) = y(0)
x0 = sw/2 + x(0)
y0 = sh/2 - y(0)
for i=1 to n
x1 = sw/2 + x(i)
y1 = sh/2 - y(i)
line (x0, y0)-(x1, y1)
line -(sw/2 + cx, sh/2 - cy)
circlef x1, y1, 8, _rgb(255,255,255)
x0 = x1
y0 = y1
next
circlef sw/2 + x(ii), sh/2 - y(ii), 8, _rgb(255,0,0)
'screenunlock
'screensync
redraw = 0
_display
end if
loop until _keyhit = 27
system
sub circlef(x, y, r, c)
x0 = r
y0 = 0
e = -r
do while y0 < x0
if e <=0 then
y0 = y0 + 1
line (x - x0, y + y0)-(x + x0, y + y0), c, bf
line (x - x0, y - y0)-(x + x0, y - y0), c, bf
e = e + 2*y0
else
line (x - y0, y - x0)-(x + y0, y - x0), c, bf
line (x - y0, y + x0)-(x + y0, y + x0), c, bf
x0 = x0 - 1
e = e - 2*x0
end if
loop
line (x - r, y)-(x + r, y), c, bf
end sub