From: Svjatoslav Agejenko Date: Sat, 26 Oct 2024 19:28:37 +0000 (+0300) Subject: Using AI to improve code readability X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=commitdiff_plain;h=6300379aa7495f77477e05e7e309e7548aa14317;p=qbasicapps.git Using AI to improve code readability --- diff --git a/Graphics/4D engine/qeng.bas b/Graphics/4D engine/qeng.bas index aea1069..31fbaa7 100755 --- a/Graphics/4D engine/qeng.bas +++ b/Graphics/4D engine/qeng.bas @@ -1,18 +1,25 @@ -' 4D engine. It renders 5-cell (aka. pentachoron) as a series of 3D tetrahedrons with varying brightness. -' Brightness is used to represent shift in fourth dimension. +' 4D engine. It renders a 5-cell (aka. pentachoron) as a series +' of 3D tetrahedrons with varying brightness. Brightness is used +' to indicate shift in the fourth dimension. ' -' In essence you can look at 3D object as a series of 2D crossections along third dimension. -' Here we look at 4D object as series of 3D crossections with varying brightness (to distinguish between them). +' In essence, you can look at a 3D object as a series of 2D +' cross-sections along the third dimension. Here we look at +' a 4D object as a series of 3D cross-sections with varying +' brightness (to distinguish between them). ' -' 4 dimensions also make it possible to rotate object along 6 different axis. -' Interestingly shape of the object changes in 3D space when it is rotated -' along any of the axis that involves 4th dimension. +' 4 dimensions also make it possible to rotate the object along +' 6 different axes. Interestingly, the shape of the object changes +' in 3D space when it is rotated along any of the axes that +' involve the fourth dimension. ' +' By Svjatoslav Agejenko. +' Email: svjatoslav@svjatoslav.eu +' Homepage: http://www.svjatoslav.eu ' -' made by Svjatoslav Agejenko -' in 2003.08 -' H-Page: svjatoslav.eu -' E-Mail: svjatoslav@svjatoslav.eu +' Changelog: +' 2003.08, Initial version +' 2024, Improved program readability using AI + DECLARE SUB chlin (x1!, y1!, z1!, q1!, x2!, y2!, z2!, q2!) DECLARE SUB rot (x1!, y1!, z1!, q1!, x4!, y4!, z4!, q4!) @@ -32,18 +39,16 @@ DIM SHARED py(1 TO 10) DIM SHARED pm DIM SHARED frm -PRINT " 4D Engine, 2003.08" -PRINT " Svjatoslav Agejenko: n0@hot.ee" PRINT "" -PRINT " use keys:" -PRINT " rotate:" +PRINT " Use keys:" +PRINT " Rotate:" PRINT " qw - XZ" PRINT " as - YZ" PRINT " zx - XY" PRINT " er - QX" PRINT " df - QY" PRINT " cv - QZ" -PRINT " move:" +PRINT " Move:" PRINT " 46 - x" PRINT " 82 - y" PRINT " 71 - z" @@ -51,11 +56,12 @@ PRINT " -+ - q" PRINT PRINT " ESC - to quit program" PRINT -PRINT "press any key to continue..." +PRINT " Press any key to continue..." a$ = INPUT$(1) pi = 3.1415 +' Angles for rotation along different axes an1 = pi * .5 an2 = an1 an3 = an1 @@ -63,16 +69,19 @@ an4 = an1 an5 = an1 an6 = an1 +' Current position in the 4D space myx = 0 myy = 0 myz = 0 myq = .5 + SCREEN 12 setpal - 1 CLS + +' Calculate sine and cosine for each rotation angle s1 = SIN(an1): c1 = COS(an1) s2 = SIN(an2): c2 = COS(an2) s3 = SIN(an3): c3 = COS(an3) @@ -80,12 +89,14 @@ s4 = SIN(an4): c4 = COS(an4) s5 = SIN(an5): c5 = COS(an5) s6 = SIN(an6): c6 = COS(an6) +' Render the 3D tetrahedrons with varying brightness FOR frm = 1 TO 15 STEP 3 qpyra -10, -10, -10, 0, 10, -10, -10, 0, 0, -10, 10, 0, 0, 10, 0, 0, 0, 0, 0, 10 NEXT frm a$ = INPUT$(1) +' Handle user input for rotation and movement SELECT CASE a$ CASE CHR$(27) SYSTEM @@ -114,6 +125,7 @@ CASE "c" CASE "v" an6 = an6 - .1 +' Handle user input for movement in the 4D space CASE "4" myx = myx - 3 CASE "6" @@ -132,143 +144,160 @@ CASE "-" myq = myq - .3 END SELECT + GOTO 1 +' Subroutine to calculate the linear interpolation between two points SUB chlin (ox1, oy1, oz1, oq1, ox2, oy2, oz2, oq2) -x1 = ox1: y1 = oy1: z1 = oz1: q1 = oq1 -x2 = ox2: y2 = oy2: z2 = oz2: q2 = oq2 - -IF (q1 > myq) AND (q2 < myq) THEN - SWAP x1, x2 - SWAP y1, y2 - SWAP z1, z2 - SWAP q1, q2 -END IF - -IF (q1 < myq) AND (q2 > myq) THEN - vq = q2 - q1 - vmq = myq - q1 - jt = vmq / vq - pm = pm + 1 - rx = (x2 - x1) * jt + x1 - ry = (y2 - y1) * jt + y1 - rz = (z2 - z1) * jt + z1 + 50 - px(pm) = rx / rz * 700 + 320 - py(pm) = ry / rz * 700 + 240 -END IF + x1 = ox1: y1 = oy1: z1 = oz1: q1 = oq1 + x2 = ox2: y2 = oy2: z2 = oz2: q2 = oq2 + + ' Swap points if the first point is behind the current position in the fourth dimension + IF (q1 > myq) AND (q2 < myq) THEN + SWAP x1, x2 + SWAP y1, y2 + SWAP z1, z2 + SWAP q1, q2 + END IF + + ' Calculate the interpolated point if the first point is in front of the current position + ' and the second point is behind it + IF (q1 < myq) AND (q2 > myq) THEN + vq = q2 - q1 + vmq = myq - q1 + jt = vmq / vq + pm = pm + 1 + rx = (x2 - x1) * jt + x1 + ry = (y2 - y1) * jt + y1 + rz = (z2 - z1) * jt + z1 + 50 + px(pm) = rx / rz * 700 + 320 + py(pm) = ry / rz * 700 + 240 + END IF END SUB +' Subroutine to get a point at a specific distance along the line segment SUB getp (x1, y1, z1, q1, x2, y2, z2, q2, n, rx, ry, rz, rq) -xv = x2 - x1 -yv = y2 - y1 -zv = z2 - z1 -qv = q2 - q1 - -rx = x1 + (xv * n) -ry = y1 + (yv * n) -rz = z1 + (zv * n) -rq = q1 + (qv * n) + ' Calculate the vector between the two points + xv = x2 - x1 + yv = y2 - y1 + zv = z2 - z1 + qv = q2 - q1 + + ' Calculate the interpolated point at the specified distance along the line segment + rx = x1 + (xv * n) + ry = y1 + (yv * n) + rz = z1 + (zv * n) + rq = q1 + (qv * n) END SUB +' Subroutine to render a 3D tetrahedron with varying brightness SUB qpyra (ox1, oy1, oz1, oq1, ox2, oy2, oz2, oq2, ox3, oy3, oz3, oq3, ox4, oy4, oz4, oq4, ox5, oy5, oz5, oq5) -ox1 = ox1 - myx -oy1 = oy1 - myy -oz1 = oz1 - myz -oq1 = oq1 - myq - frm - -ox2 = ox2 - myx -oy2 = oy2 - myy -oz2 = oz2 - myz -oq2 = oq2 - myq - frm - -ox3 = ox3 - myx -oy3 = oy3 - myy -oz3 = oz3 - myz -oq3 = oq3 - myq - frm - -ox4 = ox4 - myx -oy4 = oy4 - myy -oz4 = oz4 - myz -oq4 = oq4 - myq - frm - -ox5 = ox5 - myx -oy5 = oy5 - myy -oz5 = oz5 - myz -oq5 = oq5 - myq - frm - -rot ox1, oy1, oz1, oq1, x1, y1, z1, q1 -rot ox2, oy2, oz2, oq2, x2, y2, z2, q2 -rot ox3, oy3, oz3, oq3, x3, y3, z3, q3 -rot ox4, oy4, oz4, oq4, x4, y4, z4, q4 -rot ox5, oy5, oz5, oq5, x5, y5, z5, q5 - -pm = 0 - -chlin x1, y1, z1, q1, x2, y2, z2, q2 -chlin x1, y1, z1, q1, x3, y3, z3, q3 -chlin x1, y1, z1, q1, x4, y4, z4, q4 -chlin x1, y1, z1, q1, x5, y5, z5, q5 - -chlin x2, y2, z2, q2, x3, y3, z3, q3 -chlin x2, y2, z2, q2, x4, y4, z4, q4 -chlin x2, y2, z2, q2, x5, y5, z5, q5 - -chlin x3, y3, z3, q3, x4, y4, z4, q4 -chlin x3, y3, z3, q3, x5, y5, z5, q5 - -chlin x4, y4, z4, q4, x5, y5, z5, q5 - -FOR a = 1 TO pm - FOR b = a + 1 TO pm - LINE (px(a), py(a))-(px(b), py(b)), 15 - frm - NEXT b -NEXT a - + ' Adjust the coordinates for the current position in the 4D space + ox1 = ox1 - myx + oy1 = oy1 - myy + oz1 = oz1 - myz + oq1 = oq1 - myq - frm + + ox2 = ox2 - myx + oy2 = oy2 - myy + oz2 = oz2 - myz + oq2 = oq2 - myq - frm + + ox3 = ox3 - myx + oy3 = oy3 - myy + oz3 = oz3 - myz + oq3 = oq3 - myq - frm + + ox4 = ox4 - myx + oy4 = oy4 - myy + oz4 = oz4 - myz + oq4 = oq4 - myq - frm + + ox5 = ox5 - myx + oy5 = oy5 - myy + oz5 = oz5 - myz + oq5 = oq5 - myq - frm + + ' Rotate the points along the specified axes + rot ox1, oy1, oz1, oq1, x1, y1, z1, q1 + rot ox2, oy2, oz2, oq2, x2, y2, z2, q2 + rot ox3, oy3, oz3, oq3, x3, y3, z3, q3 + rot ox4, oy4, oz4, oq4, x4, y4, z4, q4 + rot ox5, oy5, oz5, oq5, x5, y5, z5, q5 + + ' Initialize the list of points to be drawn + pm = 0 + + ' Calculate the linear interpolation between each pair of points + chlin x1, y1, z1, q1, x2, y2, z2, q2 + chlin x1, y1, z1, q1, x3, y3, z3, q3 + chlin x1, y1, z1, q1, x4, y4, z4, q4 + chlin x1, y1, z1, q1, x5, y5, z5, q5 + + chlin x2, y2, z2, q2, x3, y3, z3, q3 + chlin x2, y2, z2, q2, x4, y4, z4, q4 + chlin x2, y2, z2, q2, x5, y5, z5, q5 + + chlin x3, y3, z3, q3, x4, y4, z4, q4 + chlin x3, y3, z3, q3, x5, y5, z5, q5 + + chlin x4, y4, z4, q4, x5, y5, z5, q5 + + ' Draw lines between each pair of points + FOR a = 1 TO pm + FOR b = a + 1 TO pm + LINE (px(a), py(a))-(px(b), py(b)), 15 - frm + NEXT b + NEXT a END SUB +' Subroutine to rotate a point along the specified axes SUB rot (x1, y1, z1, q1, x4, y4, z4, q4) -' qx -q2 = q1 * s4 - x1 * c4 -x2 = q1 * c4 + x1 * s4 - -' qy -q3 = q2 * s5 - y1 * c5 -y2 = q2 * c5 + y1 * s5 + ' Rotate the point along the QX axis + q2 = q1 * s4 - x1 * c4 + x2 = q1 * c4 + x1 * s4 -' qz -q4 = q3 * s6 - z1 * c6 -z2 = q3 * c6 + z1 * s6 + ' Rotate the point along the QY axis + q3 = q2 * s5 - y1 * c5 + y2 = q2 * c5 + y1 * s5 -' zx -x3 = x2 * s1 - z2 * c1 -z3 = x2 * c1 + z2 * s1 + ' Rotate the point along the QZ axis + q4 = q3 * s6 - z1 * c6 + z2 = q3 * c6 + z1 * s6 -' zy -y3 = y2 * s2 - z3 * c2 -z4 = y2 * c2 + z3 * s2 + ' Rotate the point along the XZ axis + x3 = x2 * s1 - z2 * c1 + z3 = x2 * c1 + z2 * s1 -' xy -y4 = y3 * s3 - x3 * c3 -x4 = y3 * c3 + x3 * s3 + ' Rotate the point along the YZ axis + y3 = y2 * s2 - z3 * c2 + z4 = y2 * c2 + z3 * s2 + ' Rotate the point along the XY axis + y4 = y3 * s3 - x3 * c3 + x4 = y3 * c3 + x3 * s3 END SUB +' Subroutine to set up the color palette SUB setpal -FOR a = 0 TO 15 - OUT &H3C8, a - OUT &H3C9, a * 4 - OUT &H3C9, a * 4 - OUT &H3C9, a * 4 - LINE (a, 0)-(a, 400), a -NEXT a -'a$ = INPUT$(1) + ' Set up the color palette for the 4D rendering + FOR a = 0 TO 15 + OUT &H3C8, a + OUT &H3C9, a * 4 + OUT &H3C9, a * 4 + OUT &H3C9, a * 4 + LINE (a, 0)-(a, 400), a + NEXT a + END SUB +' Function to calculate the distance between two points in 4D space FUNCTION vahe (x1, y1, z1, q1, x2, y2, z2, q2) -vahe = SQR((x1 - x2) ^ 2 + (y1 - y2) ^ 2 + (z1 - z2) ^ 2 + (q1 - q2) ^ 2) + vahe = SQR((x1 - x2) ^ 2 + (y1 - y2) ^ 2 + (z1 - z2) ^ 2 + (q1 - q2) ^ 2) END FUNCTION +