DECLARE SUB blob (i%, j%) DECLARE SUB hop (x%) DECLARE FUNCTION look! (x%) DECLARE SUB notepoint () DECLARE SUB setgrey () DECLARE SUB turn (x%) DEFINT I-K, M-N DIM SHARED speed AS INTEGER CLS INPUT "Speed setting: 0=fast, 1=slower, 2=slowest"; speed SCREEN 13 setgrey CONST white = 15, black = 0 CONST yellow = 14 DIM SHARED d AS INTEGER, m, n, i, j, il, jl, v, istep, jstep, xold, yold DIM SHARED lightest, darkest, greyline, fade, trail, straight, diff, kk DIM SHARED here, beenthere, thresh v = 63 / 1024 'maps array value to screen shade m = 64 '32 'size of array - try 32, 64 or 100 n = 64 '32 thresh = 1024 / m * 4 v1 = 512 / m 'for setting up pattern DIM SHARED vi(7), vj(7) FOR d = 0 TO 7 READ vi(d), vj(d) NEXT 'i and j increments for compass directions N, NE, E, SE, S, SW, W, NW DATA 1,0, 1,1, 0,1, -1,1, -1,0, -1,-1, 0,-1, 1,-1 WINDOW SCREEN (-m / 6, 0)-(7 * m / 6 - 1, n - 1) DIM SHARED pic(m, n) AS INTEGER ' array for test image i = 0 'build pattern to trace FOR j = 0 TO n - 1 pic(i, j) = v1 * m blob i, j NEXT FOR i = 1 TO m - 1 'diamond on shaded background FOR j = 0 TO n - 1 pic(i, j) = v1 * i - 512 * (ABS(i - m / 2) + ABS(j - n / 2) < m / 2 - 1) blob i, j NEXT NEXT i = m / 2 'start in the middle j = n / 2 kk = 0 beenthere = 0 'set up variables to start d = 0 'start searching North here = white lightest = pic(i, j) darkest = pic(i, j) greyline = (lightest + darkest) / 2 fade = 8 DO 'This is the search algorithm IF here = white THEN 'if first point is white here = look(d) 'look at a second point in direction d IF here = white THEN 'if it's the same, white, then turn 1 'turn clockwise for next move ELSE 'otherwise you've crossed a threshold notepoint 'so mark it END IF ELSE 'if first point was black, here = look(d - 3) 'look at a second point in direction d-3 IF here = black THEN 'if it's the same, black, then turn -1 'turn anticlockwise ELSE 'otherwise you've crossed a threshold notepoint 'so mark it END IF END IF LOOP UNTIL beenthere > 0 'keep going until you hit an old 'marked point SUB blob (i, j) 'a blob on the screen representing the shade of pic(i, j) LINE (i - .5, j - .5)-STEP(1, 1), 64 + v * pic(i, j), BF END SUB SUB hop (x%) 'Move to search pont in direction x% y = x% AND 7 istep = vi(y) 'N/S component of direction y jstep = vj(y) 'E/W component of direction y i = (i + istep) IF i >= m THEN i = 0 IF i < 0 THEN i = m - 1 'new i, correct if off the edge j = (j + jstep) 'new j, correct if off the edge IF j >= n THEN j = 0 IF j < 0 THEN j = n - 1 END SUB FUNCTION look (x%) 'move in direction x% and look at new point hop (x%) l = pic(i, j) 'value of new point IF l > greyline THEN 'compare with mid level look = white PSET (i, j), 4 'red dot if lighter IF speed > 1 THEN PLAY "n0e" 'play high note if 'slow' is set ELSE look = black PSET (i, j), 2 'green dot if darker IF speed > 1 THEN PLAY "n0c" 'play lower note if slow END IF 'Next update lightest and darkest to track the extremes found, 'fading them together slowly otherwise IF l > lightest THEN lightest = l ELSE lightest = lightest - fade IF l < darkest THEN darkest = l ELSE darkest = darkest + fade greyline = (lightest + darkest) / 2 'mid level diff = lightest - darkest 'represents local gradient IF diff < thresh THEN look = here 'if too "flat" return same 'answer as last time END FUNCTION SUB notepoint 'look in the middle of the last step p = POINT(i - istep / 2, j - jstep / 2) IF p = yellow THEN beenthere = 1 'already marked? IF speed > 1 THEN PLAY "gfedc" 'warble if slow=2 END IF PSET (i - istep / 2, j - jstep / 2), yellow 'mark transition point IF kk THEN IF ABS(i - xold) + ABS(j - yold) < 4 THEN 'if a previous transition LINE -(xold, yold), yellow 'noted, not too far away, END IF 'draw a line to it END IF 'to mark good boundary xold = i - istep / 2 'remember old point yold = j - jstep / 2 kk = 1 trail = -4 'allow quick turns IF speed THEN PLAY "n0" END SUB DEFSNG I-K, M-N SUB setgrey grey& = 1 + 256 + 65536 FOR i = 0 TO 63 PALETTE i + 64, i * grey& 'colours 64 to 127 are shades of grey NEXT PLAY "l64ms" 'setup for 'music' delay routine 'try "l16ms" for very slow, 'try "l64t240ms" for much faster END SUB DEFINT I-K, M-N SUB turn (x%) 'the complication is to make the routine search in spirals trail = trail + 1 'how far since last edge? straight = straight + 1 'how far since last turn allowed? IF 8 * straight > trail THEN 'if straight is far enough then d = (d + x%) AND 7 'turn in direction x% straight = 0 'and restart counting from this turn END IF END SUB