How to resolve the algorithm Sierpinski triangle step by step in the BASIC programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Sierpinski triangle step by step in the BASIC programming language
Table of Contents
Problem Statement
Produce an ASCII representation of a Sierpinski triangle of order N.
The Sierpinski triangle of order 4 should look like this:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Sierpinski triangle step by step in the BASIC programming language
Source code in the basic programming language
DECLARE SUB triangle (x AS INTEGER, y AS INTEGER, length AS INTEGER, n AS INTEGER)
CLS
triangle 1, 1, 16, 5
SUB triangle (x AS INTEGER, y AS INTEGER, length AS INTEGER, n AS INTEGER)
IF n = 0 THEN
LOCATE y, x: PRINT "*";
ELSE
triangle x, y + length, length / 2, n - 1
triangle x + length, y, length / 2, n - 1
triangle x + length * 2, y + length, length / 2, n - 1
END IF
END SUB
clg
call triangle (1, 1, 60)
end
subroutine triangle (x, y, l)
if l = 0 then
color blue
text (x, y, "*")
else
call triangle (x, y + l, int(l/2))
call triangle (x + l, y, int(l/2))
call triangle (x + l * 2, y + l, int(l/2))
end if
end subroutine
MODE 8
OFF
order% = 5
PROCsierpinski(0, 0, 2^(order%-1))
REPEAT UNTIL GET
END
DEF PROCsierpinski(x%, y%, l%)
IF l% = 0 THEN
PRINT TAB(x%,y%) "*";
ELSE
PROCsierpinski(x%, y%+l%, l% DIV 2)
PROCsierpinski(x%+l%, y%, l% DIV 2)
PROCsierpinski(x%+l%+l%, y%+l%, l% DIV 2)
ENDIF
ENDPROC
sub sier(x as uinteger, y as uinteger, l as uinteger)
if l=0 then
locate y, x: print "*"
else
sier(x,y+l,l\2)
sier(x+l,y,l\2)
sier(x+2*l,y+l,l\2)
end if
end sub
cls
sier(1,1,2^3)
100 PROGRAM "Triangle.bas"
110 TEXT 40
120 CALL TRIANGLE(1,1,8)
130 DEF TRIANGLE(X,Y,L)
140 IF L=0 THEN
150 PRINT AT Y,X:"*"
160 ELSE
170 CALL TRIANGLE(X,Y+L,INT(L/2))
180 CALL TRIANGLE(X+L,Y,INT(L/2))
190 CALL TRIANGLE(X+2*L,Y+L,INT(L/2))
200 END IF
210 END DEF
You may also check:How to resolve the algorithm Anagrams step by step in the CLU programming language
You may also check:How to resolve the algorithm Playing cards step by step in the Fortran programming language
You may also check:How to resolve the algorithm Binary digits step by step in the Emacs Lisp programming language
You may also check:How to resolve the algorithm Mandelbrot set step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Letter frequency step by step in the Factor programming language