REM > Blend

ONERRORONERROROFF:MODE27:REPORT:PRINTERL:END

DIM g%(50, 50)

REPEAT

MODE 28 :REM 640 x 480
VDU 28, 0, 0, 79, 0

 REPEAT
  INPUT "Grid width: "w$
  w% = VAL(w$)
  IF w% > 50 THEN VDU7: PRINT "Maximum is 50";: g=GET: PRINT
  IF w% < 2 THEN VDU7: PRINT "Minimum is 2";: g=GET: PRINT
 UNTIL (w% <= 50) AND (w% > 1)
 REPEAT
  INPUT "Grid height: "h$
  h% = VAL(h$)
  IF h% > 50 THEN VDU7: PRINT "Maximum is 50";: g=GET: PRINT
  IF h% < 2 THEN VDU7: PRINT "Minimum is 2";: g=GET: PRINT
 UNTIL (h% <= 50) AND (h% > 1)

OFF

dx% = 1279 / w%
dy% = 920 / h%
IF dx% < dy% THEN d% = dx% ELSE d% = dy%

GCOL %111111 TINT %11
 FOR x% = 0 TO w%
  LINE x% * d%, 0, x% * d%, h% * d%
 NEXT
 FOR y% = 0 TO h%
  LINE 0, y% * d%, w% * d%, y% * d%
 NEXT

g%() = 0

PRINT "Use 0-6 to choose colours and mouse to place atoms; press space to start";

MOUSE ON
MOUSE RECTANGLE 0, 0, w% * d% - 1, h% * d% - 1

MOUSE X%, Y%, Z%
ox% = X% / d% + 1
oy% = Y% / d% + 1
c% = 1
GCOL %000011 TINT %11

 REPEAT
  MOUSE X%, Y%, Z%
  x% = X% / d% + 1
  y% = Y% / d% + 1
  i$ = INKEY$(0)
   IF (x% <> ox%) OR (y% <> oy%) THEN
    PRINT '"Coordinates: "+STR$x%+", "+STR$y%;
    ox% = x%
    oy% = y%
   ENDIF
   CASE i$ OF
    WHEN "1": c% = 1: GCOL %000011 TINT %11: PRINT '"Red selected";
    WHEN "2": c% = 2: GCOL %001100 TINT %11: PRINT '"Green selected";
    WHEN "3": c% = 3: GCOL %110000 TINT %11: PRINT '"Blue selected";
    WHEN "4": c% = 4: GCOL %001111 TINT %11: PRINT '"Yellow selected";
    WHEN "5": c% = 5: GCOL %110011 TINT %11: PRINT '"Magenta selected";
    WHEN "6": c% = 6: GCOL %111100 TINT %11: PRINT '"Cyan selected";
    WHEN "0": c% = 0: GCOL %000000 TINT %00: PRINT '"Removing atoms";
   ENDCASE
   IF Z% > 0 THEN
    g%(x%, y%) = c%
    CIRCLE FILL x% * d% - (d% / 2), y% * d% - (d% / 2), (d% / 2) - 4
   ENDIF
 UNTIL i$ = " "

MOUSE OFF

GCOL %000000 TINT %00
 FOR x% = 1 TO w% - 1
  LINE x% * d%, 2, x% * d%, h% * d% - 1
 NEXT
 FOR y% = 1 TO h% - 1
  LINE 2, y% * d%, w% * d% - 1, y% * d%
 NEXT

PRINT '"Diffusing... press a key to end";

 REPEAT
  x% = RND(w%)
  y% = RND(h%)
  tx% = x%
  ty% = y%
   IF g%(x%, y%) > 0 THEN
     CASE RND(8) OF
      WHEN 1: ty% += 1
      WHEN 2: tx% += 1: ty% += 1
      WHEN 3: tx% += 1
      WHEN 4: tx% += 1: ty% -= 1
      WHEN 5: ty% -= 1
      WHEN 6: tx% -= 1: ty% -= 1
      WHEN 7: tx% -= 1
      WHEN 8: tx% -= 1: ty% += 1
     ENDCASE
     IF (tx% > 0) AND (ty% > 0) AND (tx% <= w%) AND (ty% <= h%) THEN
       IF g%(tx%, ty%) = 0 THEN
        g%(tx%, ty%) = g%(x%, y%)
        g%(x%, y%) = 0
        GCOL %000000 TINT %00
        CIRCLE FILL x% * d% - (d% / 2), y% * d% - (d% / 2), (d% / 2) - 4
         CASE g%(tx%, ty%) OF
          WHEN 1: GCOL %000011 TINT %11
          WHEN 2: GCOL %001100 TINT %11
          WHEN 3: GCOL %110000 TINT %11
          WHEN 4: GCOL %001111 TINT %11
          WHEN 5: GCOL %110011 TINT %11
          WHEN 6: GCOL %111100 TINT %11
         ENDCASE
        CIRCLE FILL tx% * d% - (d% / 2), ty% * d% - (d% / 2), (d% / 2) - 4
       ENDIF
     ENDIF
   ENDIF
 UNTIL INKEY(0) > -1

PRINT '"Press a key...";
IF GET

UNTIL 0
