Improve code readability
authorSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Mon, 24 Feb 2025 21:57:43 +0000 (23:57 +0200)
committerSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Mon, 24 Feb 2025 21:57:43 +0000 (23:57 +0200)
Graphics/Presentations/AI/AI demo.bas

index 7037aa9..4bc6711 100644 (file)
@@ -19,16 +19,15 @@ DECLARE SUB sc10 ()
 DECLARE SUB sc9 ()\r
 DECLARE SUB box (x1!, y1!, x2!, y2!)\r
 DECLARE SUB calc (tx1!, ty1!, tz1!, tx2!, ty2!, tz2!, tx3!, ty3!, tz3!, c!)\r
-DECLARE SUB getan (x1!, y1!, x2!, y2!, N!)\r
+DECLARE SUB GetAngle (x1!, y1!, x2!, y2!, N!)\r
 DECLARE SUB RotatePoint (zx!, zy!, x1!, y1!, N!)\r
-'--- Renamed here ---\r
 DECLARE SUB FillPolygon (xCoord1!, yCoord1!, xCoord2!, yCoord2!, xCoord3!, yCoord3!, colorVal!)\r
-'---------------------\r
 DECLARE SUB InitializeFont ()\r
-DECLARE SUB inpur ()\r
-DECLARE SUB mkback ()\r
+DECLARE SUB WaitForInput ()\r
+DECLARE SUB MakeBackground ()\r
 DECLARE SUB SetPalette (r!, g!, b!, c!)\r
 DECLARE SUB PrintText (x!, y!, s!, c!, a$)\r
+\r
 ' AI presentation\r
 ' made by Svjatoslav Agejenko\r
 ' in 2002\r
@@ -52,8 +51,9 @@ Scene8
 Scene9\r
 \r
 SUB box (x1, y1, x2, y2)\r
-' Draws a soft-edged rectangular box\r
-' by blending pixel colors around the edges.\r
+' Draws a soft-edged rectangular box by blending pixel colors around the edges.\r
+' The edges fade gently to create a smoothed border.\r
+\r
     FOR y = y1 TO y2\r
         s = 10\r
         IF y - y1 <= 10 THEN\r
@@ -76,8 +76,9 @@ SUB box (x1, y1, x2, y2)
 END SUB\r
 \r
 SUB calc (tx1, ty1, tz1, tx2, ty2, tz2, tx3, ty3, tz3, c)\r
-' Computes a value (c) based on\r
-' the positions of three 3D points.\r
+' Computes a value (c) based on the positions of three 3D points.\r
+' It rotates these points around multiple axes, then calculates\r
+' an offset 'a' used to determine a final computed result in 'c'.\r
 \r
     x1 = tx1\r
     y1 = ty1\r
@@ -91,16 +92,19 @@ SUB calc (tx1, ty1, tz1, tx2, ty2, tz2, tx3, ty3, tz3, c)
     y3 = ty3\r
     z3 = tz3\r
 \r
-    getan x1, y1, x2, y2, n1\r
+    ' First rotation\r
+    GetAngle x1, y1, x2, y2, n1\r
     RotatePoint x1, y1, x2, y2, -n1\r
     RotatePoint x1, y1, x3, y3, -n1\r
 \r
-    getan y1, z1, y2, z2, n2\r
+    ' Second rotation\r
+    GetAngle y1, z1, y2, z2, n2\r
     n2 = n2 + pi / 2\r
     RotatePoint y1, z1, y2, z2, -n2\r
     RotatePoint y1, z1, y3, z3, -n2\r
 \r
-    getan x1, z1, x3, z3, n3\r
+    ' Third rotation\r
+    GetAngle x1, z1, x3, z3, n3\r
     n3 = n3 + pi / 2\r
     RotatePoint x1, z1, x2, z2, -n3\r
     RotatePoint x1, z1, x3, z3, -n3\r
@@ -113,16 +117,15 @@ SUB calc (tx1, ty1, tz1, tx2, ty2, tz2, tx3, ty3, tz3, c)
     RotatePoint y1, z1, y4, z4, n2\r
     RotatePoint x1, y1, x4, y4, n1\r
 \r
-    'LINE (tx1, ty1)-(x4, y4), 255\r
+    ' The distance 'a' is used to calculate 'c'\r
     x1 = tx1 + 20\r
     y1 = ty1 + 10\r
     a = SQR((x1 - x4) ^ 2 + (y1 - y4) ^ 2)\r
     c = 49 - a\r
     IF c < 0 THEN c = 0\r
 END SUB\r
-\r
+'-------------------------------------------------------------------------------\r
 SUB FillPolygon (xCoord1, yCoord1, xCoord2, yCoord2, xCoord3, yCoord3, colorVal)\r
-\r
 ' Fills a triangular area by connecting the edges.\r
 ' It uses a scanline approach, storing intersection points in yb() array,\r
 ' then drawing horizontal lines between those intersection points.\r
@@ -172,9 +175,11 @@ mkl:
 \r
 1:\r
 END SUB\r
-'----------------------------------------------------------------------------------\r
+'-------------------------------------------------------------------------------\r
+SUB GetAngle (x1, y1, x2, y2, N)\r
+' Determines the angle (N) between two points (x1,y1) and (x2,y2).\r
+' Used for rotation operations in 2D or for adjusting orientation.\r
 \r
-SUB getan (x1, y1, x2, y2, N)\r
     IF y1 = y2 THEN\r
         IF x2 > x1 THEN N = pi / 2 ELSE N = pi * 1.5\r
         GOTO 2\r
@@ -204,15 +209,16 @@ SUB getan (x1, y1, x2, y2, N)
 \r
 2:\r
 END SUB\r
-\r
-' Subroutine to initialize font data\r
+'-------------------------------------------------------------------------------\r
 SUB InitializeFont\r
-    ' Capture font data into array\r
+' Captures the current text font into an array for later use\r
+' when drawing text with PrintText.\r
+\r
     SetPalette 0, 0, 0, 70\r
     COLOR 70\r
     FOR a = 32 TO 150\r
         LOCATE 1, 1\r
-        PRINT CHR$(a)\r
+        PRINT CHR$(a);\r
         FOR y = 0 TO 7\r
             FOR x = 0 TO 7\r
                 font(x, y, a) = POINT(x, y)\r
@@ -220,13 +226,18 @@ SUB InitializeFont
         NEXT y\r
     NEXT a\r
 END SUB\r
+'-------------------------------------------------------------------------------\r
+SUB WaitForInput\r
+' Reads exactly one character from the keyboard and stores it in a$.\r
 \r
-SUB inpur\r
     a$ = INPUT$(1)\r
 END SUB\r
-\r
+'-------------------------------------------------------------------------------\r
 DEFINT A-Z\r
-SUB mkback\r
+SUB MakeBackground\r
+' Creates a fractal-like background by iteratively sampling and\r
+' perturbing pixel values, producing a terrain effect.\r
+\r
     CLS\r
     SetPalette 0, 5, 5, 250\r
     SetPalette 0, 5, 5, 251\r
@@ -295,10 +306,12 @@ SUB mkback
     IF s > 2 THEN GOTO 5\r
 \r
 END SUB\r
-\r
+'-------------------------------------------------------------------------------\r
 DEFSNG A-Z\r
-' Subroutine to print text on screen\r
 SUB PrintText (x, y, s, c, a$)\r
+' Prints text at a specified (x,y) position on the screen, by reading\r
+' from the 'font' array captured in InitializeFont. Allows for scaling = 1.\r
+\r
     IF s = 1 THEN\r
         x2 = x\r
         FOR a = 1 TO LEN(a$)\r
@@ -315,9 +328,11 @@ SUB PrintText (x, y, s, c, a$)
         NEXT a\r
     END IF\r
 END SUB\r
-\r
-' Subroutine to rotate a point around an axis\r
+'-------------------------------------------------------------------------------\r
 SUB RotatePoint (zx, zy, x1, y1, N)\r
+' Rotates a point (x1,y1) around a center (zx,zy) by angle N (in radians).\r
+' This is a standard 2D rotation transform.\r
+\r
     x2 = x1 - zx\r
     y2 = y1 - zy\r
     c1 = SIN(N)\r
@@ -325,8 +340,12 @@ SUB RotatePoint (zx, zy, x1, y1, N)
     x1 = x2 * s1 - y2 * c1 + zx\r
     y1 = x2 * c1 + y2 * s1 + zy\r
 END SUB\r
-\r
+'-------------------------------------------------------------------------------\r
 SUB Scene1\r
+' Initializes palette and reads 3D data from "data.dat" for a rotating\r
+' point/line demonstration. The code then renders lines in 2D projection\r
+' while applying transformations over time to create animation-like motion.\r
+\r
     SetPalette 0, 63, 20, 255\r
     DIM px(0 TO 1000)\r
     DIM py(0 TO 1000)\r
@@ -350,10 +369,8 @@ SUB Scene1
 \r
     tim = 0\r
     ehi = 1\r
-\r
     an1 = 0\r
     an2 = 0\r
-\r
     np = -1\r
     nl = 0\r
     inco = 0\r
@@ -429,7 +446,6 @@ SUB Scene1
         x1 = x * s2 - z2 * c2\r
 \r
         z1 = z1 + 100\r
-\r
         x1 = x1 / z1 * 74 * 2\r
         y1 = y1 / z1 * 65 * 2\r
 \r
@@ -473,8 +489,12 @@ addlin:
     angl1 = an1\r
     angl2 = an2\r
 END SUB\r
-\r
+'-------------------------------------------------------------------------------\r
 SUB Scene2\r
+' Loads 3D data from "data.dat" and projects triangular faces to 2D.\r
+' The polygons are sorted by depth, then drawn from farthest to nearest\r
+' using FillPolygon to produce simple hidden-surface-like rendering.\r
+\r
     SetPalette 0, 63, 20, 255\r
     CLS\r
     angl1 = 0\r
@@ -539,10 +559,6 @@ SUB Scene2
         rpz(a) = z1\r
     NEXT a\r
 \r
-    'FOR a = 1 TO 63\r
-    'SetPalette COS(a / 9) * 30 + 30, SIN(a / 5) * 30 + 30, SIN(a / 13) * 30 + 30, a\r
-    'NEXT a\r
-\r
     FOR a = 1 TO 49\r
         SetPalette a * 1.1 + 20, a * 1.1 + 10, a * 1.1, a\r
     NEXT a\r
@@ -572,13 +588,14 @@ SUB Scene2
         e = e - 1\r
 \r
         calc rpx(p1), rpy(p1), rpz(p1), rpx(p2), rpy(p2), rpz(p2), rpx(p3), rpy(p3), rpz(p3), d\r
-        ' Call our renamed FillPolygon instead of old "fp"\r
         FillPolygon INT(rpx(p1)), INT(rpy(p1)), INT(rpx(p2)), INT(rpy(p2)), INT(rpx(p3)), INT(rpy(p3)), INT(d)\r
-        'SOUND 0, .07\r
     NEXT a\r
 END SUB\r
-\r
+'-------------------------------------------------------------------------------\r
 SUB Scene3\r
+' Demonstrates some simple raster effects and wave patterns,\r
+' finishing with basic text drawing and random shifts of the screen buffer.\r
+\r
     DIM buf(1 TO 10000)\r
     DIM buf1(0 TO 35)\r
 \r
@@ -642,12 +659,16 @@ SUB Scene3
         SetPalette 0, a, a * 2, 253\r
         SOUND 0, 1\r
     NEXT a\r
-    inpur\r
-END SUB\r
 \r
+    WaitForInput\r
+END SUB\r
+'-------------------------------------------------------------------------------\r
 SUB Scene4\r
+' Uses MakeBackground to generate a fractal backdrop, then draws a box\r
+' and prints some brief text explaining a "Spatial vision" concept.\r
+\r
     RANDOMIZE 1\r
-    mkback\r
+    MakeBackground\r
 \r
     box 30, 50, 290, 150\r
 \r
@@ -661,12 +682,15 @@ SUB Scene4
     y = y + 16\r
     PrintText 30, 70 + y, 1, 250, "            + automaatjuhtimine"\r
 \r
-    inpur\r
+    WaitForInput\r
 END SUB\r
-\r
+'-------------------------------------------------------------------------------\r
 SUB Scene5\r
+' Again uses MakeBackground, draws a box, and prints text describing\r
+' a feature-extraction process in image processing or computer vision.\r
+\r
     RANDOMIZE 4\r
-    mkback\r
+    MakeBackground\r
 \r
     box 30, 50, 290, 150\r
 \r
@@ -684,10 +708,13 @@ SUB Scene5
     y = y + 12\r
     PrintText 30, 70 + y, 1, 250, "   rakkude p�him�tted."\r
 \r
-    inpur\r
+    WaitForInput\r
 END SUB\r
-\r
+'-------------------------------------------------------------------------------\r
 SUB Scene7\r
+' Simple transition effect: horizontal lines are drawn across the screen\r
+' from top to bottom, clearing or darkening each row.\r
+\r
     SetPalette 0, 0, 0, 0\r
     FOR a = 0 TO 19\r
         FOR y = a TO 199 STEP 20\r
@@ -696,8 +723,13 @@ SUB Scene7
         SOUND 0, .5\r
     NEXT a\r
 END SUB\r
-\r
+'-------------------------------------------------------------------------------\r
 SUB Scene8\r
+' A more complex 3D-like demo. It generates fractal terrain in the background,\r
+' then plots points in a grid and draws lines between them. A "player" or\r
+' "robot" object (with lines making a cube-like shape) moves around,\r
+' picking up items, until time or user input ends the loop.\r
+\r
     FOR a = 1 TO 50\r
         SetPalette 0, 0, 0, a\r
     NEXT a\r
@@ -915,7 +947,7 @@ SUB Scene8
     CASE 1\r
         desx = px(np)\r
         desz = pz(np)\r
-        getan desx, desz, hlax, hlaz, desa\r
+        GetAngle desx, desz, hlax, hlaz, desa\r
         IF desa - hlka > pi THEN desa = desa - (pi * 2)\r
         IF hlka - desa > pi THEN desa = desa + (pi * 2)\r
         eta = 2\r
@@ -959,8 +991,8 @@ SUB Scene8
     y = 60 - py(INT((hlaz + 10) / 20) * 20 + INT((hlax + 10) / 20))\r
     IF hlay > y + 5 THEN hlay = hlay - 1\r
     IF hlay < y THEN hlay = hlay + 1\r
-    IF hlay > y + 25 THEN hlay = hlay - 1: ' SOUND 1000, 1\r
-    IF hlay < y - 20 THEN hlay = hlay + 1: ' SOUND 1000, 1\r
+    IF hlay > y + 25 THEN hlay = hlay - 1\r
+    IF hlay < y - 20 THEN hlay = hlay + 1\r
 \r
     s1 = SIN(hlka)\r
     c1 = COS(hlka)\r
@@ -1015,8 +1047,8 @@ SUB Scene8
     NEXT a\r
 \r
     FOR a = 1 TO nl\r
-        l1 = lin1!(a)\r
-        l2 = lin2!(a)\r
+        l1 = lin1(a)\r
+        l2 = lin2(a)\r
         x1 = px1(l1)\r
         x2 = px1(l2)\r
         LINE (lbx1(a), lby1(a))-(lbx2(a), lby2(a)), 0\r
@@ -1034,22 +1066,25 @@ SUB Scene8
     IF tim < 28000 THEN GOTO 10\r
 13:\r
 END SUB\r
-\r
+'-------------------------------------------------------------------------------\r
 SUB Scene9\r
+' Wrap-up scene that shows a background, draws a box, and prints\r
+' a final "Thank you for attention!" message.\r
+\r
     RANDOMIZE 45\r
-    mkback\r
+    MakeBackground\r
 \r
     box 30, 50, 290, 80\r
 \r
     SetPalette 32, 64, 32, 250\r
     y = -8\r
     PrintText 30, 70 + y, 1, 250, "     Thank you for attention!"\r
-    inpur\r
+    WaitForInput\r
 END SUB\r
-\r
-' Subroutine to set color palette\r
+'-------------------------------------------------------------------------------\r
 SUB SetPalette (r, g, b, c)\r
-    ' Ensure color values are within valid range\r
+' Sets a palette entry (c) to the specified (r,g,b) values, each 0..63 range.\r
+\r
     IF r < 0 THEN r = 0\r
     IF g < 0 THEN g = 0\r
     IF b < 0 THEN b = 0\r
@@ -1057,17 +1092,20 @@ SUB SetPalette (r, g, b, c)
     IF g > 63 THEN g = 63\r
     IF b > 63 THEN b = 63\r
 \r
-    ' Set palette color\r
     OUT &H3C8, c\r
     OUT &H3C9, r\r
     OUT &H3C9, g\r
     OUT &H3C9, b\r
 END SUB\r
-\r
+'-------------------------------------------------------------------------------\r
 SUB start\r
+' Initializes screen mode, sets up global constants pi and pii,\r
+' and captures system font data for later use in PrintText.\r
+\r
     SCREEN 13\r
     pi = 3.141592\r
     pii = pi\r
     fac = 360 / (pi * 2)\r
     InitializeFont\r
 END SUB\r
+'-------------------------------------------------------------------------------\r