OTHER CONTRIBUTIONS: SIERPINSKI CHALLENGE (optimized)

This example is the optimized version for the "Sierpinski Challenge", an exciting challenge regarding fractals. This example reproduces the famous "SIERPINSKI TRIANGLE" on retrocomputers. It is a very simple fractal to obtain, which takes its name from the mathematician who first studied its properties. Such a triangle can have different shapes and sizes and can be obtained in various ways. One of the methods to create it is the so-called "Game of chaos". The fractal is built by creating iteratively a sequence of points, starting from a random initial point, in which each point of the sequence is a given fraction of the distance between the previous point and one of the vertices of the polygon; the vertex is chosen at random in each iteration. Repeating this iterative process a large number of times, selecting the vertex at random at each iteration, often (but not always) produces a fractal shape. Using a regular triangle and the factor 1/2, it will result in a Sierpinski triangle.

source compile sandbox issues? back to examples

SOURCE CODE ()

' ============================================================================
' INITIALIZATION
' ============================================================================

' We start measure time from the graphical initialization.
t=TI

' Enable the monocrome bitmap graphic, 
' with a minimum resolution of 160x100 pixels.
BITMAP ENABLE(320,200,2)

' Pen color is black
INK BLACK

' Paper color is white
CLS WHITE

' Let's calculate the three constants
CONST x1=(SCREEN WIDTH \ #2): CONST y1=0
CONST x2=0: CONST y2=(SCREEN HEIGHT - 1 )
CONST x3=(SCREEN WIDTH - 1): CONST y3=(SCREEN HEIGHT - 1 )

' Number of points to draw
CONST limit = 10000

' ============================================================================
' MAIN CYCLE
' ============================================================================

' Initialize the coordinates.
x=#x1 AS POSITION
y=#y1 AS POSITION

' Initialize the counter.
n=#0 AS INTEGER

' Repeat the cycle up to (limit) points.
REPEAT

	' Select a random triangle
	r=(RANDOM BYTE \ 32) AND 3
	
	' If the triange has been chosen...
	IF r=0 THEN 
		' Update the coordinates accordingly.
		ADD x, x1 : DIV x, #2 
		ADD y, y1 : DIV y, #2 
	ELSE IF r=1 THEN 
		ADD x, x2 : DIV x, #2
		ADD y, y2 : DIV y, #2
	ELSE
		ADD x, x3 : DIV x, #2
		ADD y, y3 : DIV y, #2
	ENDIF
	
	' Plot the point on the screen.
	PLOT x, y

	' Increment the number of point traced.
	INC n

UNTIL n=limit

' Calculate the time passed
te=TI-t

HOME
' Print the stats
PRINT "time = ";(te/TICKS PER SECOND);" sec"
PRINT "points = ";n%

How to compile and run the example

The instructions here refer to compiling the example from the command line. For Microsoft Windows users we suggest using UGBASIC-IDE, which allows you to download and compile each single example with just one click.



Are instructions for your specific home computer / console missing? First of all, check if your computer is supported by clicking here. If so, since ugBASIC is a language which does not provide abstractions, it is possible that this example will not work on your target. If you think this is an issue, please click here.

Amstrad CPC 464, Amstrad CPC 6128, Amstrad CPC 664

In order to compile the example, type this command on the command line:

Linux

ugbc.cpc -O dsk -o contrib_sierpinski2.dsk contrib_sierpinski2.bas

Windows

ugbc.cpc.exe -O dsk -o contrib_sierpinski2.dsk contrib_sierpinski2.bas

For Microsoft Windows users we suggest using UGBASIC-IDE, which allows you to download and compile this example with just one click.

Atari 1200XL, Atari 130XE, Atari 600XL, Atari 65XE, Atari 800XE, Atari 800XL, Atari XEGS

In order to compile the example, type this command on the command line:

Linux

ugbc.atarixl -O xex -o contrib_sierpinski2.xex contrib_sierpinski2.bas

Windows

ugbc.atarixl.exe -O xex -o contrib_sierpinski2.xex contrib_sierpinski2.bas

For Microsoft Windows users we suggest using UGBASIC-IDE, which allows you to download and compile this example with just one click.

Atari 400, Atari 800

In order to compile the example, type this command on the command line:

Linux

ugbc.atari -O xex -o contrib_sierpinski2.xex contrib_sierpinski2.bas

Windows

ugbc.atari.exe -O xex -o contrib_sierpinski2.xex contrib_sierpinski2.bas

For Microsoft Windows users we suggest using UGBASIC-IDE, which allows you to download and compile this example with just one click.

Coleco Vision, Dina (Chuang Zao Zhe 50), SpectraVideo SV-603 VGA

In order to compile the example, type this command on the command line:

Linux

ugbc.coleco -O rom -o contrib_sierpinski2.rom contrib_sierpinski2.bas

Windows

ugbc.coleco.exe -O rom -o contrib_sierpinski2.rom contrib_sierpinski2.bas

For Microsoft Windows users we suggest using UGBASIC-IDE, which allows you to download and compile this example with just one click.

Commodore 128 (CPU 8502)

In order to compile the example, type this command on the command line:

Linux

ugbc.c128 -O prg -o contrib_sierpinski2.prg contrib_sierpinski2.bas

Windows

ugbc.c128.exe -O prg -o contrib_sierpinski2.prg contrib_sierpinski2.bas

For Microsoft Windows users we suggest using UGBASIC-IDE, which allows you to download and compile this example with just one click.

Commodore 64, Commodore Executive 64

In order to compile the example, type this command on the command line:

Linux

ugbc.c64 -O prg -o contrib_sierpinski2.prg contrib_sierpinski2.bas

Windows

ugbc.c64.exe -O prg -o contrib_sierpinski2.prg contrib_sierpinski2.bas

For Microsoft Windows users we suggest using UGBASIC-IDE, which allows you to download and compile this example with just one click.

Dragon 32

In order to compile the example, type this command on the command line:

Linux

ugbc.d32 -O bin -o contrib_sierpinski2.bin contrib_sierpinski2.bas

Windows

ugbc.d32.exe -O bin -o contrib_sierpinski2.bin contrib_sierpinski2.bas

For Microsoft Windows users we suggest using UGBASIC-IDE, which allows you to download and compile this example with just one click.

Dragon 64

In order to compile the example, type this command on the command line:

Linux

ugbc.d64 -O bin -o contrib_sierpinski2.bin contrib_sierpinski2.bas

Windows

ugbc.d64.exe -O bin -o contrib_sierpinski2.bin contrib_sierpinski2.bas

For Microsoft Windows users we suggest using UGBASIC-IDE, which allows you to download and compile this example with just one click.

SEGA SC-3000

In order to compile the example, type this command on the command line:

Linux

ugbc.sc3000 -O rom -o contrib_sierpinski2.rom contrib_sierpinski2.bas

Windows

ugbc.sc3000.exe -O rom -o contrib_sierpinski2.rom contrib_sierpinski2.bas

For Microsoft Windows users we suggest using UGBASIC-IDE, which allows you to download and compile this example with just one click.

SEGA SG-1000

In order to compile the example, type this command on the command line:

Linux

ugbc.sg1000 -O rom -o contrib_sierpinski2.rom contrib_sierpinski2.bas

Windows

ugbc.sg1000.exe -O rom -o contrib_sierpinski2.rom contrib_sierpinski2.bas

For Microsoft Windows users we suggest using UGBASIC-IDE, which allows you to download and compile this example with just one click.

TRS-80 Color Computer, TRS-80 Color Computer 2

In order to compile the example, type this command on the command line:

Linux

ugbc.coco -O bin -o contrib_sierpinski2.bin contrib_sierpinski2.bas

Windows

ugbc.coco.exe -O bin -o contrib_sierpinski2.bin contrib_sierpinski2.bas

For Microsoft Windows users we suggest using UGBASIC-IDE, which allows you to download and compile this example with just one click.

ZX Spectrum +2, ZX Spectrum 128K, ZX Spectrum 48K

In order to compile the example, type this command on the command line:

Linux

ugbc.zx -O tap -o contrib_sierpinski2.tap contrib_sierpinski2.bas

Windows

ugbc.zx.exe -O tap -o contrib_sierpinski2.tap contrib_sierpinski2.bas

For Microsoft Windows users we suggest using UGBASIC-IDE, which allows you to download and compile this example with just one click.

Any problem?

If you have found a problem trying to run this example, if you think there is a bug or, more simply, you would like it to be improved, open an issue for this example on GitHub.
Thank you!

open an issue BACK TO EXAMPLES