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