Better directory names
authorSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Sun, 18 Aug 2024 09:54:27 +0000 (12:54 +0300)
committerSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Sun, 18 Aug 2024 09:54:27 +0000 (12:54 +0300)
Graphics/3D/3dland.bas

index eac4cfb..5d6e4d2 100755 (executable)
-' Svjatoslav Agejenko\r
-' year 1999\r
-\r
-DECLARE SUB setpal ()\r
-DEFINT A-Y\r
-DECLARE SUB box (x1, y1, x2, y2, x3, y3, x4, y4, c) 'draw filled\r
-                                                    'box using 4 cordinates\r
-                                                 '(sometimes don't work\r
-                                                 'correctly, but fast.\r
-                                                 '(PAINT command used))\r
-DIM SHARED x1(1 TO 40, 1 TO 40)  ' X & Y  cordinates\r
-DIM SHARED y1(1 TO 40, 1 TO 40)  '\r
+DECLARE SUB DrawBox (x1%, y1%, x2%, y2%, x3%, y3%, x4%, y4%, c1%)\r
 \r
-SCREEN 12\r
-\r
-setpal\r
-zfa = 1.5\r
-1\r
-FOR b = 1 TO 40\r
-FOR a = 1 TO 40\r
-\r
-x = 120 + (a * 10)\r
-y = 200 + (b * 3)\r
-\r
-y = y - COS(SQR((a - 20) ^ 2 + (b - 20) ^ 2) / zfa) * 20\r
-\r
-x = (x - 320) * (b + 50) / 50 + 320\r
-y = (y - 240) * (b + 50) / 50 + 240\r
-\r
-x1(a, b) = x\r
-y1(a, b) = y\r
+' Program to render 3D shaded landscape with perspective and distortion effects.\r
+' By Svjatoslav Agejenko.\r
+' Email: svjatoslav@svjatoslav.eu\r
+' Homepage: http://www.svjatoslav.eu\r
+'\r
+' Changelog:\r
+' 1999, Initial version\r
+' 2024.08, Improved program readability using AI\r
 \r
-NEXT a\r
-NEXT b\r
+DECLARE SUB SetPalette ()\r
+DEFINT A-Z\r
 \r
+' Declare shared arrays for X and Y coordinates\r
+DIM SHARED xCoordinates(1 TO 40, 1 TO 40)\r
+DIM SHARED yCoordinates(1 TO 40, 1 TO 40)\r
 \r
+' Set screen mode to 12\r
+SCREEN 12\r
 \r
-FOR b = 1 TO 39\r
-FOR a = 1 TO 39\r
-IF (a + b) \ 2 = (a + b + 1) \ 2 THEN c = 0 ELSE c = 5\r
-kz = b + (a / 3)\r
-\r
-box x1(a, b), y1(a, b), x1(a + 1, b), y1(a + 1, b), x1(a, b + 1), y1(a, b + 1), x1(a + 1, b + 1), y1(a + 1, b + 1), c\r
-NEXT a\r
-NEXT b\r
-\r
-a$ = INPUT$(1)\r
-zfa = zfa * 1.9\r
+' Initialize color palette\r
+SetPalette\r
+\r
+' Set initial scaling factor\r
+scalingFactor = 1.5\r
+\r
+' Main loop start\r
+1 :\r
+' Loop through each point in the grid\r
+FOR rowIndex = 1 TO 40\r
+    FOR colIndex = 1 TO 40\r
+        ' Calculate the position with distortion\r
+        xPosition = 120 + (colIndex * 10)\r
+        yPosition = 200 + (rowIndex * 3)\r
+\r
+        ' Apply a cosine distortion based on distance from center\r
+        yPosition = yPosition - COS(SQR((colIndex - 20) ^ 2 + (rowIndex - 20) ^ 2) / scalingFactor) * 20\r
+\r
+        ' Apply perspective transformation\r
+        xPosition = (xPosition - 320) * (rowIndex + 50) / 50 + 320\r
+        yPosition = (yPosition - 240) * (rowIndex + 50) / 50 + 240\r
+\r
+        ' Store the transformed coordinates\r
+        xCoordinates(colIndex, rowIndex) = xPosition\r
+        yCoordinates(colIndex, rowIndex) = yPosition\r
+    NEXT colIndex\r
+NEXT rowIndex\r
+\r
+' Draw boxes based on the stored coordinates\r
+FOR rowIndex = 1 TO 39\r
+    FOR colIndex = 1 TO 39\r
+        ' Alternate colors for each box\r
+        IF (colIndex + rowIndex) \ 2 <> (colIndex + rowIndex + 1) \ 2 THEN colorIndex = 0 ELSE colorIndex = 5\r
+        ' Calculate a brightness factor\r
+        brightnessFactor = rowIndex + (colIndex / 3)\r
+\r
+        ' Draw the box with the calculated color and brightness\r
+        DrawBox xCoordinates(colIndex, rowIndex), yCoordinates(colIndex, rowIndex), xCoordinates(colIndex + 1, rowIndex), yCoordinates(colIndex + 1, rowIndex), xCoordinates(colIndex, rowIndex + 1), yCoordinates(colIndex, rowIndex + 1), xCoordinates( _\r
+colIndex + 1, rowIndex + 1), yCoordinates(colIndex + 1, rowIndex + 1), colorIndex\r
+    NEXT colIndex\r
+NEXT rowIndex\r
+\r
+' Wait for user input and adjust the scaling factor\r
+userInput$ = INPUT$(1)\r
+scalingFactor = scalingFactor * 1.9\r
+\r
+' Clear the screen for the next iteration\r
 CLS\r
-IF zfa > 10 THEN SYSTEM\r
-GOTO 1\r
-\r
-SUB box (x1, y1, x2, y2, x3, y3, x4, y4, c1)\r
 \r
-c1 = c1 + (y2 - y1) / 3.5 + (kz / 8) + 4\r
+' If the scaling factor is too large, exit the program\r
+IF scalingFactor > 10 THEN SYSTEM\r
 \r
-IF c1 < 0 THEN c1 = 0\r
-IF c1 > 15 THEN c1 = 15\r
+' Jump back to the main loop start\r
+GOTO 1\r
 \r
-a = SQR((x1 - x2) ^ 2 + (y1 - y2) ^ 2)\r
-b = SQR((x3 - x4) ^ 2 + (y3 - y4) ^ 2)\r
-IF b < a THEN b = a\r
-FOR a = 1 TO b\r
-x5 = (x2 - x1) * a / b + x1\r
-y5 = (y2 - y1) * a / b + y1\r
-x6 = (x4 - x3) * a / b + x3\r
-y6 = (y4 - y3) * a / b + y3\r
-LINE (x5, y5)-(x6, y6), c1\r
-LINE (x5 + 1, y5)-(x6 + 1, y6), c1\r
-NEXT a\r
+' Subroutine to draw a filled box using line drawing\r
+SUB DrawBox (x1, y1, x2, y2, x3, y3, x4, y4, c1)\r
+    ' Adjust color index based on position and brightness factor\r
+    c1 = c1 + (y2 - y1) / 3.5 + (brightnessFactor / 8) + 4\r
+\r
+    ' Ensure the color index is within valid range\r
+    IF c1 < 0 THEN c1 = 0\r
+    IF c1 > 15 THEN c1 = 15\r
+\r
+    ' Calculate the length of the longer side\r
+    a = SQR((x1 - x2) ^ 2 + (y1 - y2) ^ 2)\r
+    b = SQR((x3 - x4) ^ 2 + (y3 - y4) ^ 2)\r
+    IF b < a THEN b = a\r
+\r
+    ' Draw the box using lines\r
+    FOR lineIndex = 1 TO b\r
+        x5 = (x2 - x1) * lineIndex / b + x1\r
+        y5 = (y2 - y1) * lineIndex / b + y1\r
+        x6 = (x4 - x3) * lineIndex / b + x3\r
+        y6 = (y4 - y3) * lineIndex / b + y3\r
+\r
+        ' Draw two adjacent lines to create a filled effect\r
+        LINE (x5, y5)-(x6, y6), c1\r
+        LINE (x5 + 1, y5)-(x6 + 1, y6), c1\r
+    NEXT lineIndex\r
 END SUB\r
 \r
-SUB setpal\r
-FOR a = 0 TO 16\r
-OUT &H3C8, a\r
-OUT &H3C9, a * 4\r
-OUT &H3C9, a * 4\r
-OUT &H3C9, a * 3\r
-NEXT\r
+' Subroutine to initialize color palette\r
+SUB SetPalette\r
+    FOR paletteIndex = 1 TO 16\r
+        ' Set the color values for each palette entry\r
+        OUT &H3C8, paletteIndex\r
+        OUT &H3C9, paletteIndex * 4\r
+        OUT &H3C9, paletteIndex * 4\r
+        OUT &H3C9, paletteIndex * 3\r
+    NEXT paletteIndex\r
 END SUB\r
 \r