Graphics demos
Table of Contents
- 1. Bump mapping
- 2. Tree
- 3. Rotation in 2D space using trigonometry functions
- 4. Various text mode animation effects
- 5. Snowfall
- 6. Screensaver
- 7. Screensaver - flying hand fans
- 8. Polygon rendering
- 9. Textured polygon rendering
- 10. Yin and yang animation
- 11. Orbiting particles
- 12. DNA animation
- 13. Matrix
- 14. Hacker
1. Bump mapping
Light source moves around. Based on light source location, different parts of the surface become illuminated.
2. Tree
Tree grows and branches out.
DECLARE SUB setPalette () ' Render tree that starts with single root and then branches out. ' By Svjatoslav Agejenko. ' Email: svjatoslav@svjatoslav.eu ' Homepage: http://www.svjatoslav.eu ' ' Changelog: ' 2001, Initial version ' 2024.08, Improved program readability using AI DECLARE SUB start () DECLARE SUB show (d%) DECLARE SUB setpal () DECLARE SUB showpal () DEFINT A-Y DIM SHARED x(1 TO 500) DIM SHARED y(1 TO 500) DIM SHARED s(1 TO 500) DIM SHARED x4(1 TO 500) DIM SHARED y4(1 TO 500) DIM SHARED z(1 TO 500) DIM SHARED mitu start 1 mitu = 1 x4(1) = -1 y4(1) = -1 x(1) = 420 y(1) = 340 s(1) = 70 * 100 z(1) = 1 ' Main loop to render the tree branches FOR tr = 1 TO 6 FOR b = 1 TO 50 FOR a = 1 TO mitu show a NEXT a NEXT b ' Duplicate existing branches and add randomness FOR a = 1 TO mitu x(mitu + a) = x(a) y(mitu + a) = y(a) s(mitu + a) = s(a) z(mitu + a) = z(a) x4(mitu + a) = RND * 4 - 2 y4(mitu + a) = RND * 4 - 2 NEXT a mitu = mitu * 2 NEXT tr ' Exit application if any key was pressed by user IF INKEY$ <> "" THEN SYSTEM SLEEP 2 CLS GOTO 1 SUB setPalette ' Set the palette colors to grayscale FOR a = 0 TO 16 OUT &H3C8, a OUT &H3C9, a * 4 OUT &H3C9, a * 4 OUT &H3C9, a * 4 NEXT END SUB SUB show (d) ' Retrieve current branch properties x1 = x(d) y1 = y(d) s1 = s(d) z1 = z(d) ' Calculate color based on angle c = SIN(z1) * 7 + 9 ' Draw and fill the circle representing the branch CIRCLE (x1, y1), s1 / 100, c PAINT (x1, y1), c ' Update position based on current angle and size x(d) = x(d) + (SIN(z1) * 1000) / (s1 + 15) y(d) = y(d) + (COS(z1) * 1000) / (s1 + 15) ' Decay the size slightly s(d) = s(d) / 1.01 ' Update angle based on direction IF x4(d) >= 0 THEN z(d) = z(d) + .1 ELSE z(d) = z(d) - .1 ' Move branch in its random direction x(d) = x(d) + x4(d) y(d) = y(d) + y4(d) END SUB SUB start SCREEN 12 setPalette RANDOMIZE TIMER END SUB
3. Rotation in 2D space using trigonometry functions
Grid of dots is rotated on the screen.
' This program showcases the rotation of points on an X-Y coordinate ' system using trigonometric functions, specifically sine and cosine. By ' simulating the effect of rotating a collection of grid points around ' the origin, it demonstrates the mathematical principles behind 2D ' rotation. ' ' By Svjatoslav Agejenko. ' Email: svjatoslav@svjatoslav.eu ' Homepage: http://www.svjatoslav.eu ' Changelog: ' 2003.12, Initial version ' 2024, Improved program readability using AI DIM SHARED pointXCoordinates(1000) ' Array to store x coordinates of points DIM SHARED pointYCoordinates(1000) ' Array to store y coordinates of points DIM SHARED oldPointXCoordinates(1000) ' Array to store previous x coordinates of points DIM SHARED oldPointYCoordinates(1000) ' Array to store previous y coordinates of points SCREEN 13 numPoints = 0 ' Initialize the number of points FOR pointXVal = -10 TO 10 FOR pointYVal = -10 TO 10 numPoints = numPoints + 1 pointXCoordinates(numPoints) = pointXVal pointYCoordinates(numPoints) = pointYVal NEXT pointYVal NEXT pointXVal ' Main rotation loop rotationAngle = 0 ' Initialize the rotation angle to 0 1 rotationAngle = rotationAngle + .01 ' Increment the rotation angle by 0.01 radians ' Calculate the sine and cosine of the current rotation angle sineOfRotationAngle = SIN(rotationAngle) cosineOfRotationAngle = COS(rotationAngle) FOR pointIndex = 1 TO numPoints PSET (oldPointXCoordinates(pointIndex), oldPointYCoordinates(pointIndex)), 0 ' Clear the previous position xCoordinate = pointXCoordinates(pointIndex) yCoordinate = pointYCoordinates(pointIndex) ' Calculate the new x and y coordinate after rotation newXCoordinate = xCoordinate * sineOfRotationAngle + yCoordinate * cosineOfRotationAngle newYCoordinate = xCoordinate * cosineOfRotationAngle - yCoordinate * sineOfRotationAngle ' Scale and translate the new x and y coordinates to the center of the screen newXCoordinate = newXCoordinate * 7 + 160 newYCoordinate = newYCoordinate * 7 + 100 ' Store x and y on-screen coordinates for clearing on next iteration oldPointXCoordinates(pointIndex) = newXCoordinate oldPointYCoordinates(pointIndex) = newYCoordinate PSET (newXCoordinate, newYCoordinate), 15 ' Draw the point at the new position NEXT pointIndex IF INKEY$ = "" THEN GOTO 1 ' Continue rotating if no key is pressed
4. Various text mode animation effects
Program demonstrates various animation effects that can be accomplished using text-mode rendering.
5. Snowfall
Program simulates falling of snow particles. Particles fall towards the ground because of the gravity. Once particle falls on the surface, it tries to skid around a bit, bit ultimately freezes in-place.
' Svjatoslav Agejenko 2003.04 DEFINT A-Z DECLARE SUB fall (particleIndex) DECLARE SUB start () amo = 500 DIM SHARED fx(1 TO amo) DIM SHARED fy(1 TO amo) ' Initialize particle positions FOR a = 1 TO amo fx(a) = RND * 300 + 10 fy(a) = RND * 100 + 10 NEXT a start 1 ' Main loop to simulate snowfall FOR b = 1 TO 100 a = INT(RND * amo) + 1 fall a NEXT b SOUND 0, .1 IF INKEY$ <> "" THEN SYSTEM GOTO 1 SUB fall (particleIndex) t = 0 2 ' Draw the particle at its current position PSET (fx(particleIndex), fy(particleIndex)), 0 ny = fy(particleIndex) + 1 nx = fx(particleIndex) + INT(RND * 3) - 1 ' Check for collision with another particle IF POINT(nx, ny) > 0 THEN ' If collision detected and t is less than 10, increment t and retry IF t < 10 THEN t = t + 1 GOTO 2 END IF ' If collision persists, change particle color to indicate collision PSET (fx(particleIndex), fy(particleIndex)), 15 nx = RND * 300 + 10 ny = 1 END IF ' Check if the particle has reached the bottom of the screen IF fy(particleIndex) > 198 THEN PSET (fx(particleIndex), fy(particleIndex)), 15 nx = RND * 300 + 10 ny = 1 END IF ' Update particle position fx(particleIndex) = nx fy(particleIndex) = ny ' Draw the particle at its new position PSET (fx(particleIndex), fy(particleIndex)), 15 END SUB DEFSNG A-Z SUB start SCREEN 13 ' Create nice and curvy surface for snow particles to fall onto. ' Here we draw "SNOW" with big and wobbly letters to the screen ' to serve as an obstacle for snow particles. LOCATE 1, 1 PRINT "SNOW" FOR y = 0 TO 15 STEP .2 xp = SIN(y / 1) * 3 + 65 FOR x = 0 TO 30 STEP .1 ys = 4 + COS(x / 5) yp = COS(x / 4 + 3) * 5 + 130 c = POINT(x, y) ' Draw a line if the point is not black IF c > 0 THEN LINE (x * 6 + xp, y * ys + yp)-(x * 6 + xp + 1, y * ys + yp + 1), 11, BF END IF NEXT x NEXT y LOCATE 1, 1 PRINT " " END SUB
6. Screensaver
Application of trigonometry functions is explored here to calculate line coordinates.
' Mystery screensaver ' By Svjatoslav Agejenko. ' Email: svjatoslav@svjatoslav.eu ' Homepage: http://www.svjatoslav.eu ' ' Changelog: ' 2004.01, Initial version ' 2024.08, Improved program readability using AI SCREEN 7, , , 1 ' Main loop for animation DO ' Increment frame counter frameCounter = frameCounter + 1 ' Calculate scaling factor based on frame counter scaleFactor = (SIN(frameCounter / 100) + 1.1) * 2 ' Draw lines to create animation effect FOR s = 1 TO 20 STEP .1 ' Calculate x and y coordinates for the first point x = SIN(s / 1 + frameCounter / 7) * 100 y = COS(s / 1 + frameCounter / 10) * 100 ' Calculate x and y coordinates for the second point x1 = SIN(s / 1 - frameCounter / 8) * 100 y1 = COS(s / 1 + frameCounter / 15) * 100 ' Draw a line between the two points with varying thickness LINE (x + 160, y + 100)-(x1 + 160, y1 + 100), s MOD 15 NEXT s ' Copy screen buffer to display the animation PCOPY 0, 1 ' Generate a sound effect SOUND 0, 1 ' Clear the screen for the next frame CLS ' Check if a key is pressed and exit if so IF INKEY$ <> "" THEN SYSTEM LOOP
7. Screensaver - flying hand fans
Quick implementation for colorful flying hand fans.
' Screensaver ' By Svjatoslav Agejenko. ' Email: svjatoslav@svjatoslav.eu ' Homepage: http://www.svjatoslav.eu ' ' Changelog: ' 2003.04, Initial version ' 2024.08, Improved program readability using AI SCREEN 7, , , 1 ' Main animation loop 1 : ' Adjust frame counter if it exceeds a certain threshold IF frameCounter > 10000 THEN frameCounter = -10000 ' Update the positions of six points based on the frame counter FOR pointIndex = 1 TO 6 OUT &H3C8, pointIndex OUT &H3C9, SIN(pointIndex + frameCounter * 3) * 30 + 31 OUT &H3C9, COS(pointIndex * 1 + frameCounter * 5) * 30 + 31 OUT &H3C9, SIN(pointIndex * .7 + frameCounter * 2.23) * 30 + 31 NEXT pointIndex ' Increment the frame counter for the next iteration frameCounter = frameCounter + .01 ' Render lines connecting points FOR objectIndex = 1 TO 10 ' Determine the color based on the remainder of objectIndex divided by 6 pointColor = (objectIndex MOD 6) + 1 ' Calculate the X coordinate for the starting point of the line xCoordinate = SIN(objectIndex + frameCounter) * 100 + 150 ' Calculate the Y coordinate for the starting point of the line yCoordinate = COS(objectIndex * 1.2 + frameCounter * 1.81) * 80 + 100 ' Calculate the sine component for the line's angle xSineComponent = SIN(objectIndex * frameCounter * 2.3) ' Draw lines from the starting point with varying lengths and angles FOR xPositionOffset = -50 TO 50 STEP 10 ' Calculate the cosine component for the line's angle based on xPositionOffset yCosineComponent = COS(xPositionOffset / 60 + frameCounter * 1 + objectIndex) * 50 ' Draw a line segment with varying thickness and color LINE (xCoordinate, yCoordinate)-(xCoordinate + xPositionOffset * xSineComponent, yCoordinate - yCosineComponent), pointColor NEXT xPositionOffset NEXT objectIndex ' Copy the graphics from the hidden page to the visible page PCOPY 0, 1 ' Clear the screen for the next frame CLS ' Play a sound at a specific frequency and duration SOUND 0, .4 ' Check if any key is pressed; if so, exit the program IF INKEY$ <> "" THEN SYSTEM ' Loop back to the start of the animation GOTO 1
8. Polygon rendering
Algorithm to demonstrate rendering or polygons across arbitrary coordinates.
' Program to render polygons at random locations and random colors. ' By Svjatoslav Agejenko. ' Email: svjatoslav@svjatoslav.eu ' Homepage: http://www.svjatoslav.eu ' Changelog: ' 2001, Initial version ' 2024, Improved program readability using AI DEFINT A-Z DECLARE SUB fillPolygon (x1, y1, x2, y2, x3, y3, c) SCREEN 13 MainLoop: ' Generate random coordinates for the first vertex x1 = RND * 318 + 1 y1 = RND * 198 + 1 ' Generate random coordinates for the second vertex x2 = RND * 318 + 1 y2 = RND * 198 + 1 ' Generate random coordinates for the third vertex x3 = RND * 318 + 1 y3 = RND * 198 + 1 ' Fill the polygon with a random color fillPolygon x1, y1, x2, y2, x3, y3, RND * 255 ' Add delay SOUND 0, 1 ' Check if any key is pressed to exit the loop IF INKEY$ <> "" THEN SYSTEM GOTO MainLoop SUB fillPolygon (x1, y1, x2, y2, x3, y3, c) ' Buffer array to store x-coordinates for each y-index DIM yBuffer(-10 TO 210) ' Draw the line between the first and second vertices tempX1 = x1 tempY1 = y1 tempX2 = x2 tempY2 = y2 GOSUB makeLine ' Draw the line between the first and third vertices tempX1 = x1 tempY1 = y1 tempX2 = x3 tempY2 = y3 GOSUB makeLine ' Draw the line between the second and third vertices tempX1 = x3 tempY1 = y3 tempX2 = x2 tempY2 = y2 GOSUB makeLine GOTO FillEnd makeLine: ' Ensure that the start point is always below the end point IF tempY2 < tempY1 THEN SWAP tempY1, tempY2: SWAP tempX1, tempX2 ' Loop through each y-index from the start to the end FOR yIndex = tempY1 TO tempY2 - 1 ' Calculate the x-position for the current y-index xPos = tempX1 + (tempX2 - tempX1) * ((yIndex - tempY1) / (tempY2 - tempY1)) ' If the buffer is empty, store the x-position IF yBuffer(yIndex) = 0 THEN yBuffer(yIndex) = xPos ELSE ' Otherwise, draw a line between the stored and calculated positions LINE (xPos, yIndex)-(yBuffer(yIndex), yIndex), c END IF NEXT yIndex RETURN FillEnd: END SUB
9. Textured polygon rendering
Algorithm to demonstrate rendering or textured polygons across arbitrary coordinates.
10. Yin and yang animation
Yin and yang is a concept that originated in Chinese philosophy, describing an opposite but interconnected, self-perpetuating cycle. Yin and yang can be thought of as complementary and at the same time opposing forces that interact to form a dynamic system in which the whole is greater than the assembled parts and the parts are important for cohesion of the whole.
' Yin and yang animation ' By Svjatoslav Agejenko. ' Email: svjatoslav@svjatoslav.eu ' Homepage: http://www.svjatoslav.eu ' ' Changelog: ' 2000, Initial version ' 2024.08, Improved program readability using AI DECLARE SUB Cir (x!, y!, r!, c!) SCREEN 13 pi = 3.141592599999999# PAINT (1, 1), 1 ' Main animation loop DO ' Calculate the x and y positions for both circles using sine and cosine functions x = SIN(a) * 40 + 160 x1 = SIN(a + pi) * 40 + 160 y = COS(a) * 34 + 100 y1 = COS(a + pi) * 34 + 100 ' Draw the first circle with color 0 (black) Cir x, y, 40, 0 ' Draw the second circle with color 1 (blue) Cir x1, y1, 40, 1 ' Increment the angle to animate the circles a = a + .05 ' Check for user input to exit the program IF INKEY$ <> "" THEN SYSTEM ' delay to slow down animation SOUND 0, 1 LOOP ' Subroutine to draw a circle with specified center (x, y), radius r, and color c SUB Cir (x, y, r, c) ' Define colors for the circle outline cc1 = 0 ' Black cc2 = 15 ' White ' Swap colors if the second color is desired for the inner part of the circle IF c = 1 THEN SWAP cc1, cc2 ' Draw the circle from radius 1 to r FOR a = 1 TO r ' Determine the color for the current circle segment IF a < r / 2 THEN c1 = cc1 ELSE c1 = cc2 ' Draw the circle segment with the determined color CIRCLE (x, y), a, c1 NEXT a END SUB
11. Orbiting particles
Trivial to implement but interesting looking effect. Various particles are orbiting central point. Each particle is connected to the center.
' Projects animated particles in orbit around a central point. ' By Svjatoslav Agejenko. ' Email: svjatoslav@svjatoslav.eu ' Homepage: http://www.svjatoslav.eu ' Changelog: ' ?, Initial version ' 2024, Improved program readability using AI SCREEN 7, , , 1 RANDOMIZE TIMER ' Declare shared arrays to store particles DIM SHARED particleColor(1 TO 100) DIM SHARED particleX(1 TO 100) DIM SHARED particleAngle(1 TO 100) ' Initialize the arrays with random values FOR a = 1 TO 100 particleColor(a) = RND * 15 particleX(a) = RND * 100 particleAngle(a) = RND * 100 NEXT a ' Main loop to draw the animated particles 1 CLS FOR a = 1 TO 50 ' Get current segment data currentColor = particleColor(a) currentParticleX = particleX(a) currentparticleAngle = particleAngle(a) ' Calculate the x and y coordinates for the current segment xCoordinate = SIN(currentparticleAngle) * 25 yCoordinate = SIN(currentParticleX) * 20 ' Scale the coordinates scaleFactor = (COS(currentparticleAngle) + 2) * 2 xCoordinate = xCoordinate * scaleFactor yCoordinate = yCoordinate * scaleFactor ' Draw the current particle as a circle CIRCLE (xCoordinate + 160, yCoordinate + 100), scaleFactor, currentColor ' Fill the inside of the circle with color PAINT (xCoordinate + 160, yCoordinate + 100), currentColor ' Draw a line from the center to the current particle LINE (160, 100)-(xCoordinate + 160, yCoordinate + 100), currentColor ' Rotate particle by small amount for next frame particleAngle(a) = particleAngle(a) + .1 NEXT a ' Copy screen buffer 0 to screen buffer 1 PCOPY 0, 1 ' Check for user input and exit if any key is pressed IF INKEY$ <> "" THEN SYSTEM ' Use sound function with inaudible 0 Hz but fixed delay to slow down animation SOUND 0, 1 ' Go back to the main loop GOTO 1
12. DNA animation
Animated DNA. Nowhere close to being anatomically correct, but resembles animation as seen in the movies :)
' Program to render animated DNA as seen in the movies. ' ' By Svjatoslav Agejenko. ' Email: svjatoslav@svjatoslav.eu ' Homepage: http://www.svjatoslav.eu ' ' Changelog: ' ?, Initial version ' 2024, Improved program readability using AI DIM SHARED xCoordinates(1 TO 100) DIM SHARED yCoordinates(1 TO 100) DIM SHARED zCoordinates(1 TO 100) DIM SHARED colorCodes(1 TO 100) SCREEN 7, , , 1 1: b = 0 rotationAngle = rotationAngle + 0.1 FOR a = 1 TO 20 b = b + 1 ' Calculate x-coordinate using sine function and add to array xCoordinates(b) = SIN(a / 2 + rotationAngle) * 30 + 150 ' Calculate z-coordinate using sine function and add to array zCoordinates(b) = SIN(a / 2 + rotationAngle + 1.6) * 2 + 2 ' Calculate y-coordinate by multiplying a with 8 and adding z-coordinate yCoordinates(b) = a * 8 + zCoordinates(b) ' Assign color code to the current point colorCodes(b) = 3 b = b + 1 ' Calculate x-coordinate using sine function and add to array xCoordinates(b) = SIN(a / 2 + rotationAngle + 2.5) * 30 + 150 ' Calculate z-coordinate using sine function and add to array zCoordinates(b) = SIN(a / 2 + rotationAngle + 1.6 + 2.5) * 2 + 2 ' Calculate y-coordinate by multiplying a with 8 and adding z-coordinate yCoordinates(b) = a * 8 + zCoordinates(b) ' Assign color code to the current point colorCodes(b) = 4 NEXT a ' Clear the screen CLS ' Draw lines and circles based on z-coordinate FOR b = 0 TO 4 IF b = 1 THEN FOR a = 1 TO 40 STEP 2 ' Draw line between consecutive points LINE (xCoordinates(a), yCoordinates(a))-(xCoordinates(a + 1), yCoordinates(a + 1)), 15 NEXT a END IF FOR a = 1 TO 40 ' Check if the current z-coordinate matches the loop variable b IF int(zCoordinates(a)) = b THEN ' Draw circle with specified color code CIRCLE (xCoordinates(a), yCoordinates(a)), b + 5, colorCodes(a) PAINT (xCoordinates(a), yCoordinates(a)), colorCodes(a) ' Draw an black outline of the circle CIRCLE (xCoordinates(a), yCoordinates(a)), b + 5, 0 END IF NEXT a NEXT b ' Copy the screen to buffer and clear the screen PCOPY 0, 1 CLS ' Check if any key is pressed IF INKEY$ = "" THEN GOTO 1 ' End the program SYSTEM
13. Matrix
Effect inspired by "The Matrix" movie.
14. Hacker
Ultra-realistic hacker screen simulator! Behold da glory of a true hacker's terminal, brimming wif mystical green text that cascades like a waterfall of knowledge across thy monitor.