From fb5ad00ca8cd51de64571f24b8e3d2213612ddc2 Mon Sep 17 00:00:00 2001 From: Svjatoslav Agejenko Date: Tue, 29 Oct 2024 19:02:08 +0200 Subject: [PATCH] Using AI to improve code readability --- Math/3D graph/3dgraph.bas | 100 +++++++++++++++++++++++++------------- 1 file changed, 65 insertions(+), 35 deletions(-) diff --git a/Math/3D graph/3dgraph.bas b/Math/3D graph/3dgraph.bas index 528c62c..751c502 100644 --- a/Math/3D graph/3dgraph.bas +++ b/Math/3D graph/3dgraph.bas @@ -1,8 +1,18 @@ -' 3D heightmap explorer. Allows to visualize heightmap for arbitrary formula. +DECLARE SUB formula (x!, y!, z!) +DECLARE SUB graaf () +DECLARE SUB mkgr3 (x1!, y1!, z1!) +DECLARE SUB mkgr2 (x1!, y1!, z1!) +DECLARE SUB mkgr (x1!, y1!, z1!) +DECLARE SUB start () +DECLARE SUB getcor () +DECLARE SUB nait3d () +' 3D heightmap explorer. Allows to visualize heightmap for arbitrary function. +' Inspect and edit function "formula" to visualize alternative mathematical functions. + ' By Svjatoslav Agejenko. ' Email: svjatoslav@svjatoslav.eu ' Homepage: http://www.svjatoslav.eu -' + ' Changelog: ' 2002, Initial version ' 2024.09, Improved program readability using AI @@ -13,27 +23,11 @@ ' + - fly down ' ESC - exit program -' Type your formula to sub module "valem". +' Type your formula to sub module "formula". ' X & Y are surface coordinates. Z must be formula ' result, indicating height. "tm" variable counts ' frames. Use it in your formula to make graph moving in time. -DECLARE SUB valem (x!, y!, z!) -DECLARE SUB graaf () -DECLARE SUB mkgr3 (x1!, y1!, z1!) -DECLARE SUB mkgr2 (x1!, y1!, z1!) -DECLARE SUB mkgr (x1!, y1!, z1!) -DECLARE SUB ruut2 (x!, y!, z!, s!) -DECLARE SUB ruut (x!, y!, z!, s!) -DECLARE SUB kuus (x, y, z, s) -DECLARE SUB porand () -DECLARE SUB addp (x, y, z) -DECLARE SUB start () -DECLARE SUB addsq (x1%, y1%, z1%) -DECLARE SUB getcor () -DECLARE SUB mulcor () -DECLARE SUB nait3d () -DECLARE SUB calcsin () DIM SHARED vertexX(4000), vertexY(4000), vertexZ(4000) DIM SHARED x(4000), y(4000), z(4000) @@ -58,13 +52,20 @@ nait3d PRINT "Kuskil programmis l�ks mingi arv �le lubatud piiride!!!" RESUME +' This subroutine initializes the coordinate system and sets up the grid. SUB getcor c = 12 + ' Create background 2D grids along every axis in 3D space + ' to help viewer perceive scale. mkgr -500, 0, 0 mkgr2 0, 0, 500 mkgr3 0, -500, 0 + ' Place center-crossing pink lines across every axis (3D cross). + ' This is to help viewer to see where zero point on the graph is. + + ' Add vertices for the 3D cross. vertexX(vertexCount + 1) = 0 vertexY(vertexCount + 1) = -500 vertexZ(vertexCount + 1) = 0 @@ -89,6 +90,7 @@ SUB getcor vertexY(vertexCount + 6) = 0 vertexZ(vertexCount + 6) = 500 + ' Add lines for the 3D cross. linePoint1(lineCount + 1) = vertexCount + 1 linePoint2(lineCount + 1) = vertexCount + 2 lineColor(lineCount + 1) = c @@ -101,12 +103,14 @@ SUB getcor linePoint2(lineCount + 3) = vertexCount + 6 lineColor(lineCount + 3) = c + ' Update the vertex and line counts vertexCount = vertexCount + 6 lineCount = lineCount + 3 tmvertexCount = vertexCount tmlineCount = lineCount END SUB +' This subroutine generates a grid of points based on the formula SUB graaf c = 14 @@ -115,12 +119,14 @@ SUB graaf FOR x = -500 TO 500 STEP 50 FOR z = -500 TO 500 STEP 50 + ' Increment the vertex count and add a new vertex d = d + 1 vertexX(vertexCount + d) = x - valem x / 50, z / 50, y + formula x / 50, z / 50, y ' evaluate formula that we want to visualize vertexY(vertexCount + d) = y * 50 vertexZ(vertexCount + d) = z + ' Connect current point on the grid with neighbors IF z > -500 THEN e = e + 1 linePoint1(lineCount + e) = vertexCount + d @@ -138,10 +144,12 @@ SUB graaf NEXT z NEXT x + ' Update the vertex and line counts vertexCount = vertexCount + d lineCount = lineCount + e END SUB +' This subroutine generates a measuring grid in the ZY plane. SUB mkgr (x1, y1, z1) c = 3 @@ -150,11 +158,13 @@ SUB mkgr (x1, y1, z1) FOR z = -500 TO 500 STEP 100 FOR y = -500 TO 500 STEP 100 + ' Increment the vertex count and add a new vertex d = d + 1 vertexX(vertexCount + d) = x1 vertexY(vertexCount + d) = y1 + y vertexZ(vertexCount + d) = z1 + z + ' Add lines to the line array if necessary IF y > -500 THEN e = e + 1 linePoint1(lineCount + e) = vertexCount + d @@ -172,10 +182,12 @@ SUB mkgr (x1, y1, z1) NEXT y NEXT z + ' Update the vertex and line counts vertexCount = vertexCount + d lineCount = lineCount + e END SUB +' This subroutine generates a measuring grid XY plane. SUB mkgr2 (x1, y1, z1) c = 3 @@ -184,11 +196,13 @@ SUB mkgr2 (x1, y1, z1) FOR x = -500 TO 500 STEP 100 FOR y = -500 TO 500 STEP 100 + ' Increment the vertex count and add a new vertex d = d + 1 vertexX(vertexCount + d) = x1 + x vertexY(vertexCount + d) = y1 + y vertexZ(vertexCount + d) = z1 + ' Add lines to the line array if necessary IF y > -500 THEN e = e + 1 linePoint1(lineCount + e) = vertexCount + d @@ -206,10 +220,12 @@ SUB mkgr2 (x1, y1, z1) NEXT y NEXT x + ' Update the vertex and line counts vertexCount = vertexCount + d lineCount = lineCount + e END SUB +' This subroutine generates a measuring grid in the XZ plane. SUB mkgr3 (x1, y1, z1) c = 3 @@ -218,11 +234,13 @@ SUB mkgr3 (x1, y1, z1) FOR x = -500 TO 500 STEP 100 FOR z = -500 TO 500 STEP 100 + ' Increment the vertex count and add a new vertex d = d + 1 vertexX(vertexCount + d) = x1 + x vertexY(vertexCount + d) = y1 vertexZ(vertexCount + d) = z + ' Add lines to the line array if necessary IF z > -500 THEN e = e + 1 linePoint1(lineCount + e) = vertexCount + d @@ -240,10 +258,12 @@ SUB mkgr3 (x1, y1, z1) NEXT z NEXT x + ' Update the vertex and line counts vertexCount = vertexCount + d lineCount = lineCount + e END SUB +' This subroutine renders the 3D scene. SUB nait3d 1 @@ -266,18 +286,21 @@ Deg2 = Deg2 + d2 C1 = COS(deg1): S1 = SIN(deg1) C2 = COS(Deg2): S2 = SIN(Deg2) +' Transform the vertices to 3D space FOR a = 1 TO vertexCount xo = vertexX(a) - myx yo = -vertexY(a) - myy zo = vertexZ(a) - myz + ' Apply rotation transformations x1 = (xo * C1 - zo * S1) z1 = (xo * S1 + zo * C1) y1 = (yo * C2 - z1 * S2) z2 = (yo * S2 + z1 * C2) + ' Project the vertices onto the 2D screen xo(a) = x(a) yo(a) = y(a) @@ -289,19 +312,24 @@ FOR a = 1 TO vertexCount END IF NEXT +' Draw the lines on the screen FOR a = 1 TO lineCount p1 = linePoint1(a) p2 = linePoint2(a) + ' Skip drawing if either point is off-screen IF xo(p1) = -1 OR xo(p2) = -1 THEN ' Do nothing ELSE + ' erase line at previous position LINE (xo(p1), yo(p1))-(xo(p2), yo(p2)), 0 + ' draw line at new position LINE (x(p1), y(p1))-(x(p2), y(p2)), lineColor(a) END IF NEXT +' Handle keyboard input K$ = INKEY$ IF K$ <> "" THEN @@ -354,10 +382,10 @@ IF K$ <> "" THEN END SELECT END IF - GOTO 1 END SUB +' This subroutine initializes the graphics and sets up the program. SUB start SCREEN 12 CLS @@ -373,24 +401,26 @@ getcor END SUB -SUB valem (x, y, z) +' This subroutine calculates the height of a point based on the formula +SUB formula (x, y, z) z = 0 -v = SQR(x * x + y * y) ' v = distance from center, some formulas needs it. +v = SQR(x * x + y * y) ' v = distance from center, some formulas need it. +' Apply the formula to calculate the height z = z + SIN(x + y) * SIN(tm / 10) ' diagonal lines z = z + (SQR((15 + v) * (15 - v)) - 10) ' top of the ball -' here I mixed 2 formulas. - -'z = z + RND * 1 ' noise -'z = z + SIN((y + tm) / 2) ' forward moving wave -'z = z + SIN(v / 2) * 2 ' circular waves -'z = z - SQR(v * 6) ' sharp - -'z = z + SIN(y / 1.5) / 1.5 + COS(x / 1.5) / 1.5' custom 1 -'z = z + SIN(y / 1.5) * COS(x / 1.5) / 1.5 ' custom 2 -'z = z + INT(SIN(1.5 * x * SIN(tm / 10))) * 3 ' custom 3 -'z = z - INT(v / 5) * 3 + 3 ' custom 4 -'z = z + 3 * ((-INT((x - .3) / 20) * INT((23 + x - ABS(y * 1.2)) / 15)) + -INT(-y / 20) * -INT(-x / 20) * INT(-((x - 2) * (x - 2) + (y * 1.2 - 4) * (y * 1.2 - 4)) / 2000 + 1.01) + -INT(y / 20) * -INT(-x / 20) * INT(-((x - 2) * (x - 2) + (y * 1.2 + 4) * (y * 1.2 + 4)) / 2000 + 1.01)) ' heart + +' As you see, multiple formulas can be enabled simultaneously. +' Few more example formulas that you can uncomment and enable. +' z = z + RND * 1 ' noise +' z = z + SIN((y + tm) / 2) ' forward moving wave +' z = z + SIN(v / 2) * 2 ' circular waves +' z = z - SQR(v * 6) ' sharp peak +' z = z + SIN(y / 1.5) / 1.5 + COS(x / 1.5) / 1.5' custom 1 +' z = z + SIN(y / 1.5) * COS(x / 1.5) / 1.5 ' custom 2 +' z = z + INT(SIN(1.5 * x * SIN(tm / 10))) * 3 ' custom 3 +' z = z - INT(v / 5) * 3 + 3 ' custom 4 +' z = z + 3 * ((-INT((x - .3) / 20) * INT((23 + x - ABS(y * 1.2)) / 15)) + -INT(-y / 20) * -INT(-x / 20) * INT(-((x - 2) * (x - 2) + (y * 1.2 - 4) * (y * 1.2 - 4)) / 2000 + 1.01) + -INT(y / 20) * -INT(-x / 20) * INT(-((x - 2) * (x - 2) + (y * 1.2 + 4) * (y * 1.2 + 4)) / 2000 + 1.01)) ' heart END SUB -- 2.20.1