Sierpinski triangle/Graphical: Difference between revisions

Content added Content deleted
(Added SmileBASIC, grouped the BASICs)
Line 497: Line 497:
[https://www.dropbox.com/s/c3g1ae1i771ox7g/Sierpinski_triangle_QBasic.png?dl=0 Sierpinski triangle QBasic image]
[https://www.dropbox.com/s/c3g1ae1i771ox7g/Sierpinski_triangle_QBasic.png?dl=0 Sierpinski triangle QBasic image]


=={{header|BBC BASIC}}==
==={{header|BBC BASIC}}===
{{works with|BBC BASIC for Windows}}
{{works with|BBC BASIC for Windows}}
<lang bbcbasic> order% = 8
<lang bbcbasic> order% = 8
Line 509: Line 509:
</lang>
</lang>
[[File:sierpinski_triangle_bbc.gif]]
[[File:sierpinski_triangle_bbc.gif]]

==={{header|FreeBASIC}}===
<lang FreeBASIC>' version 06-07-2015
' compile with: fbc -s console or with: fbc -s gui

#Define black 0
#Define white RGB(255,255,255)

Dim As Integer x, y
Dim As Integer order = 9
Dim As Integer size = 2 ^ order

ScreenRes size, size, 32
Line (0,0) - (size -1, size -1), black, bf

For y = 0 To size -1
For x = 0 To size -1
If (x And y) = 0 Then PSet(x, y) ' ,white
Next
Next

' empty keyboard buffer
While Inkey <> "" : Wend
WindowTitle "Hit any key to end program"
Sleep
End</lang>

==={{header|IS-BASIC}}===
<lang IS-BASIC>100 PROGRAM "Triangle.bas"
110 SET VIDEO MODE 1:SET VIDEO COLOR 0:SET VIDEO X 40:SET VIDEO Y 27
120 OPEN #101:"video:"
130 DISPLAY #101:AT 1 FROM 1 TO 27
140 CALL SIERP(896,180,50)
150 DEF SIERP(W,X,Y)
160 IF W>28 THEN
170 CALL SIERP(W/2,X,Y)
180 CALL SIERP(W/2,X+W/4,Y+W/2)
190 CALL SIERP(W/2,X+W/2,Y)
200 ELSE
210 PLOT X,Y;X+W/2,Y+W;X+W,Y;X,Y
220 END IF
230 END DEF</lang>

==={{header|Liberty BASIC}}===
The ability of LB to handle very large integers makes the Pascal triangle method very attractive. If you alter the rem'd line you can ask it to print the last, central term...
<lang lb>
nomainwin

open "test" for graphics_nsb_fs as #gr

#gr "trapclose quit"
#gr "down; home"
#gr "posxy cx cy"

order =10

w =cx *2: h =cy *2

dim a( h, h) 'line, col

#gr "trapclose quit"
#gr "down; home"

a( 1, 1) =1

for i = 2 to 2^order -1
scan
a( i, 1) =1
a( i, i) =1
for j = 2 to i -1
'a(i,j)=a(i-1,j-1)+a(i-1,j) 'LB is quite capable for crunching BIG numbers
a( i, j) =(a( i -1, j -1) +a( i -1, j)) mod 2 'but for this task, last bit is enough (and it much faster)
next
for j = 1 to i
if a( i, j) mod 2 then #gr "set "; cx +j -i /2; " "; i
next
next
#gr "flush"

wait

sub quit handle$
close #handle$
end
end sub
</lang>
Up to order 10 displays on a 1080 vertical pixel screen.

==={{header|Run BASIC}}===
[[File : SierpinskiRunBasic.png|thumb|right]]
<lang runbasic>graphic #g, 300,300
order = 8
width = 100
w = width * 11
dim canvas(w,w)
canvas(1,1) = 1

for x = 2 to 2^order -1
canvas(x,1) = 1
canvas(x,x) = 1
for y = 2 to x -1
canvas( x, y) = (canvas(x -1,y -1) + canvas(x -1, y)) mod 2
if canvas(x,y) mod 2 then #g "set "; width + (order*3) + y - x / 2;" "; x
next y
next x
render #g
#g "flush"
wait</lang>

==={{header|SmileBASIC}}===
{{Trans|Action!}}
<lang basic>OPTION STRICT
OPTION DEFINT
DEF DRAW X0, Y0, DEPTH
VAR X, Y, SIZE
SIZE = 1 << DEPTH
FOR Y = 0 TO SIZE - 1
FOR X = 0 TO SIZE - 1
IF (X AND Y) == 0 THEN
GPSET X0 + X, Y0 + Y, RGB(X, 255 - Y, 255)
ENDIF
NEXT
NEXT
END
CALL "DRAW", 96, 32, 7
END</lang>

==={{header|TI-83 BASIC}}===
<lang ti83b>:1→X:1→Y
:Zdecimal
:Horizontal 3.1
:Vertical -4.5
:While 1
:X+1→X
:DS<(Y,1
:While 0
:X→Y
:1→X
:End
:If pxl-Test(Y-1,X) xor (pxl-Test(Y,X-1
:PxlOn(Y,X
:End</lang>
This could be made faster, but I just wanted to use the DS<( command

==={{header|Yabasic}}===
[http://retrogamecoding.org/board/index.php?action=dlattach;topic=753.0;attach=1800;image Sierpinski Triangle 3D.png]

3D version.
<lang Yabasic>// Adpated from non recursive sierpinsky.bas for SmallBASIC 0.12.6 [B+=MGA] 2016-05-19 with demo mod 2016-05-29

//Sierpinski triangle gasket drawn with lines from any 3 given points
// WITHOUT RECURSIVE Calls


//first a sub, given 3 points of a triangle draw the traiangle within
//from the midpoints of each line forming the outer triangle
//this is the basic Sierpinski Unit that is repeated at greater depths
//3 points is 6 arguments to function plus a depth level

xmax=800:ymax=600
open window xmax,ymax
backcolor 0,0,0
color 255,0,0
clear window

sub SierLineTri(x1, y1, x2, y2, x3, y3, maxDepth)
local mx1, mx2, mx3, my1, my2, my3, ptcount, depth, i, X, Y
Y = 1
//load given set of 3 points into oa = outer triangles array, ia = inner triangles array
ptCount = 3
depth = 1
dim oa(ptCount - 1, 1) //the outer points array
oa(0, X) = x1
oa(0, Y) = y1
oa(1, X) = x2
oa(1, Y) = y2
oa(2, X) = x3
oa(2, Y) = y3
dim ia(3 * ptCount - 1, 1) //the inner points array
iaIndex = 0
while(depth <= maxDepth)
for i=0 to ptCount-1 step 3 //draw outer triangles at this level
if depth = 1 then
line oa(i,X), oa(i,Y), oa(i+1,X), oa(i+1,Y)
line oa(i+1,X), oa(i+1,Y), oa(i+2,X), oa(i+2,Y)
line oa(i,X), oa(i,Y), oa(i+2,X), oa(i+2,Y)
end if
if oa(i+1,X) < oa(i,X) then mx1 = (oa(i,X) - oa(i+1,X))/2 + oa(i+1,X) else mx1 = (oa(i+1,X) - oa(i,X))/2 + oa(i,X) endif
if oa(i+1,Y) < oa(i,Y) then my1 = (oa(i,Y) - oa(i+1,Y))/2 + oa(i+1,Y) else my1 = (oa(i+1,Y) - oa(i,Y))/2 + oa(i,Y) endif
if oa(i+2,X) < oa(i+1,X) then mx2 = (oa(i+1,X)-oa(i+2,X))/2 + oa(i+2,X) else mx2 = (oa(i+2,X)-oa(i+1,X))/2 + oa(i+1,X) endif
if oa(i+2,Y) < oa(i+1,Y) then my2 = (oa(i+1,Y)-oa(i+2,Y))/2 + oa(i+2,Y) else my2 = (oa(i+2,Y)-oa(i+1,Y))/2 + oa(i+1,Y) endif
if oa(i+2,X) < oa(i,X) then mx3 = (oa(i,X) - oa(i+2,X))/2 + oa(i+2,X) else mx3 = (oa(i+2,X) - oa(i,X))/2 + oa(i,X) endif
if oa(i+2,Y) < oa(i,Y) then my3 = (oa(i,Y) - oa(i+2,Y))/2 + oa(i+2,Y) else my3 = (oa(i+2,Y) - oa(i,Y))/2 + oa(i,Y) endif
//color 9 //testing
//draw all inner triangles
line mx1, my1, mx2, my2
line mx2, my2, mx3, my3
line mx1, my1, mx3, my3
//x1, y1 with mx1, my1 and mx3, my3
ia(iaIndex,X) = oa(i,X)
ia(iaIndex,Y) = oa(i,Y) : iaIndex = iaIndex + 1
ia(iaIndex,X) = mx1
ia(iaIndex,Y) = my1 : iaIndex = iaIndex + 1
ia(iaIndex,X) = mx3
ia(iaIndex,Y) = my3 : iaIndex = iaIndex + 1
//x2, y2 with mx1, my1 and mx2, my2
ia(iaIndex,X) = oa(i+1,X)
ia(iaIndex,Y) = oa(i+1,Y) : iaIndex = iaIndex + 1
ia(iaIndex,X) = mx1
ia(iaIndex,Y) = my1 : iaIndex = iaIndex + 1
ia(iaIndex,X) = mx2
ia(iaIndex,Y) = my2 : iaIndex = iaIndex + 1
//x3, y3 with mx3, my3 and mx2, my2
ia(iaIndex,X) = oa(i+2,X)
ia(iaIndex,Y) = oa(i+2,Y) : iaIndex = iaIndex + 1
ia(iaIndex,X) = mx2
ia(iaIndex,Y) = my2 : iaIndex = iaIndex + 1
ia(iaIndex,X) = mx3
ia(iaIndex,Y) = my3 : iaIndex = iaIndex + 1
next i
//update and prepare for next level
ptCount = ptCount * 3
depth = depth + 1
redim oa(ptCount - 1, 1 )
for i = 0 to ptCount - 1
oa(i, X) = ia(i, X)
oa(i, Y) = ia(i, Y)
next i
redim ia(3 * ptCount - 1, 1)
iaIndex = 0
wend
end sub

//Test Demo for the sub (NEW as 2016 - 05 - 29 !!!!!)
cx=xmax/2
cy=ymax/2
r=cy - 20
N=3
for i = 0 to 2
color 64+42*i,64+42*i,64+42*i
SierLineTri(cx, cy, cx+r*cos(2*pi/N*i), cy +r*sin(2*pi/N*i), cx + r*cos(2*pi/N*(i+1)), cy + r*sin(2*pi/N*(i+1)), 5)
next i
</lang>

Simple recursive version
<lang Yabasic>w = 800 : h = 600
open window w, h
window origin "lb"

sub SierpinskyTriangle(level, x, y, w, h)
local w2, w4, h2
w2 = w/2 : w4 = w/4 : h2 = h/2
if level=1 then
new curve
line to x, y
line to x+w2, y+h
line to x+w, y
line to x, y
else
SierpinskyTriangle(level-1, x, y, w2, h2)
SierpinskyTriangle(level-1, x+w4, y+h2, w2, h2)
SierpinskyTriangle(level-1, x+w2, y, w2, h2)
end if
end sub

SierpinskyTriangle(7, w*0.05, h*0.05, w*0.9, h*0.9)</lang>


=={{header|C}}==
=={{header|C}}==
Line 886: Line 1,164:
triangle s" triangle.ppm" save_image \ done, save the image</lang>
triangle s" triangle.ppm" save_image \ done, save the image</lang>
{{Out}}''Because Rosetta code doesn't allow file uploads, the output can't be shown.''
{{Out}}''Because Rosetta code doesn't allow file uploads, the output can't be shown.''
=={{header|FreeBASIC}}==
<lang FreeBASIC>' version 06-07-2015
' compile with: fbc -s console or with: fbc -s gui

#Define black 0
#Define white RGB(255,255,255)

Dim As Integer x, y
Dim As Integer order = 9
Dim As Integer size = 2 ^ order

ScreenRes size, size, 32
Line (0,0) - (size -1, size -1), black, bf

For y = 0 To size -1
For x = 0 To size -1
If (x And y) = 0 Then PSet(x, y) ' ,white
Next
Next

' empty keyboard buffer
While Inkey <> "" : Wend
WindowTitle "Hit any key to end program"
Sleep
End</lang>

=={{header|gnuplot}}==
=={{header|gnuplot}}==
Generating X,Y coordinates by the ternary digits of parameter t.
Generating X,Y coordinates by the ternary digits of parameter t.
Line 1,021: Line 1,273:
{{libheader|Icon Programming Library}}
{{libheader|Icon Programming Library}}
[http://www.cs.arizona.edu/icon/library/src/gprogs/sier1.icn Original source IPL Graphics/sier1.]
[http://www.cs.arizona.edu/icon/library/src/gprogs/sier1.icn Original source IPL Graphics/sier1.]

=={{header|IS-BASIC}}==
<lang IS-BASIC>100 PROGRAM "Triangle.bas"
110 SET VIDEO MODE 1:SET VIDEO COLOR 0:SET VIDEO X 40:SET VIDEO Y 27
120 OPEN #101:"video:"
130 DISPLAY #101:AT 1 FROM 1 TO 27
140 CALL SIERP(896,180,50)
150 DEF SIERP(W,X,Y)
160 IF W>28 THEN
170 CALL SIERP(W/2,X,Y)
180 CALL SIERP(W/2,X+W/4,Y+W/2)
190 CALL SIERP(W/2,X+W/2,Y)
200 ELSE
210 PLOT X,Y;X+W/2,Y+W;X+W,Y;X,Y
220 END IF
230 END DEF</lang>


=={{header|J}}==
=={{header|J}}==
Line 1,387: Line 1,623:
}
}
}</lang>
}</lang>

=={{header|Liberty BASIC}}==
The ability of LB to handle very large integers makes the Pascal triangle method very attractive. If you alter the rem'd line you can ask it to print the last, central term...
<lang lb>
nomainwin

open "test" for graphics_nsb_fs as #gr

#gr "trapclose quit"
#gr "down; home"
#gr "posxy cx cy"

order =10

w =cx *2: h =cy *2

dim a( h, h) 'line, col

#gr "trapclose quit"
#gr "down; home"

a( 1, 1) =1

for i = 2 to 2^order -1
scan
a( i, 1) =1
a( i, i) =1
for j = 2 to i -1
'a(i,j)=a(i-1,j-1)+a(i-1,j) 'LB is quite capable for crunching BIG numbers
a( i, j) =(a( i -1, j -1) +a( i -1, j)) mod 2 'but for this task, last bit is enough (and it much faster)
next
for j = 1 to i
if a( i, j) mod 2 then #gr "set "; cx +j -i /2; " "; i
next
next
#gr "flush"

wait

sub quit handle$
close #handle$
end
end sub
</lang>
Up to order 10 displays on a 1080 vertical pixel screen.


=={{header|Logo}}==
=={{header|Logo}}==
Line 2,510: Line 2,701:
end
end
</lang>
</lang>

=={{header|Run BASIC}}==
[[File : SierpinskiRunBasic.png|thumb|right]]
<lang runbasic>graphic #g, 300,300
order = 8
width = 100
w = width * 11
dim canvas(w,w)
canvas(1,1) = 1

for x = 2 to 2^order -1
canvas(x,1) = 1
canvas(x,x) = 1
for y = 2 to x -1
canvas( x, y) = (canvas(x -1,y -1) + canvas(x -1, y)) mod 2
if canvas(x,y) mod 2 then #g "set "; width + (order*3) + y - x / 2;" "; x
next y
next x
render #g
#g "flush"
wait</lang>


=={{header|Rust}}==
=={{header|Rust}}==
Line 2,698: Line 2,868:
update; # So we can see progress
update; # So we can see progress
sierpinski .c {200 10 390 390 10 390} 7</lang>
sierpinski .c {200 10 390 390 10 390} 7</lang>

=={{header|TI-83 BASIC}}==
<lang ti83b>:1→X:1→Y
:Zdecimal
:Horizontal 3.1
:Vertical -4.5
:While 1
:X+1→X
:DS<(Y,1
:While 0
:X→Y
:1→X
:End
:If pxl-Test(Y-1,X) xor (pxl-Test(Y,X-1
:PxlOn(Y,X
:End</lang>
This could be made faster, but I just wanted to use the DS<( command


=={{header|Wren}}==
=={{header|Wren}}==
Line 2,763: Line 2,916:
SetVid(3); \restore normal text display
SetVid(3); \restore normal text display
]</lang>
]</lang>

=={{header|Yabasic}}==
[http://retrogamecoding.org/board/index.php?action=dlattach;topic=753.0;attach=1800;image Sierpinski Triangle 3D.png]

3D version.
<lang Yabasic>// Adpated from non recursive sierpinsky.bas for SmallBASIC 0.12.6 [B+=MGA] 2016-05-19 with demo mod 2016-05-29

//Sierpinski triangle gasket drawn with lines from any 3 given points
// WITHOUT RECURSIVE Calls


//first a sub, given 3 points of a triangle draw the traiangle within
//from the midpoints of each line forming the outer triangle
//this is the basic Sierpinski Unit that is repeated at greater depths
//3 points is 6 arguments to function plus a depth level

xmax=800:ymax=600
open window xmax,ymax
backcolor 0,0,0
color 255,0,0
clear window

sub SierLineTri(x1, y1, x2, y2, x3, y3, maxDepth)
local mx1, mx2, mx3, my1, my2, my3, ptcount, depth, i, X, Y
Y = 1
//load given set of 3 points into oa = outer triangles array, ia = inner triangles array
ptCount = 3
depth = 1
dim oa(ptCount - 1, 1) //the outer points array
oa(0, X) = x1
oa(0, Y) = y1
oa(1, X) = x2
oa(1, Y) = y2
oa(2, X) = x3
oa(2, Y) = y3
dim ia(3 * ptCount - 1, 1) //the inner points array
iaIndex = 0
while(depth <= maxDepth)
for i=0 to ptCount-1 step 3 //draw outer triangles at this level
if depth = 1 then
line oa(i,X), oa(i,Y), oa(i+1,X), oa(i+1,Y)
line oa(i+1,X), oa(i+1,Y), oa(i+2,X), oa(i+2,Y)
line oa(i,X), oa(i,Y), oa(i+2,X), oa(i+2,Y)
end if
if oa(i+1,X) < oa(i,X) then mx1 = (oa(i,X) - oa(i+1,X))/2 + oa(i+1,X) else mx1 = (oa(i+1,X) - oa(i,X))/2 + oa(i,X) endif
if oa(i+1,Y) < oa(i,Y) then my1 = (oa(i,Y) - oa(i+1,Y))/2 + oa(i+1,Y) else my1 = (oa(i+1,Y) - oa(i,Y))/2 + oa(i,Y) endif
if oa(i+2,X) < oa(i+1,X) then mx2 = (oa(i+1,X)-oa(i+2,X))/2 + oa(i+2,X) else mx2 = (oa(i+2,X)-oa(i+1,X))/2 + oa(i+1,X) endif
if oa(i+2,Y) < oa(i+1,Y) then my2 = (oa(i+1,Y)-oa(i+2,Y))/2 + oa(i+2,Y) else my2 = (oa(i+2,Y)-oa(i+1,Y))/2 + oa(i+1,Y) endif
if oa(i+2,X) < oa(i,X) then mx3 = (oa(i,X) - oa(i+2,X))/2 + oa(i+2,X) else mx3 = (oa(i+2,X) - oa(i,X))/2 + oa(i,X) endif
if oa(i+2,Y) < oa(i,Y) then my3 = (oa(i,Y) - oa(i+2,Y))/2 + oa(i+2,Y) else my3 = (oa(i+2,Y) - oa(i,Y))/2 + oa(i,Y) endif
//color 9 //testing
//draw all inner triangles
line mx1, my1, mx2, my2
line mx2, my2, mx3, my3
line mx1, my1, mx3, my3
//x1, y1 with mx1, my1 and mx3, my3
ia(iaIndex,X) = oa(i,X)
ia(iaIndex,Y) = oa(i,Y) : iaIndex = iaIndex + 1
ia(iaIndex,X) = mx1
ia(iaIndex,Y) = my1 : iaIndex = iaIndex + 1
ia(iaIndex,X) = mx3
ia(iaIndex,Y) = my3 : iaIndex = iaIndex + 1
//x2, y2 with mx1, my1 and mx2, my2
ia(iaIndex,X) = oa(i+1,X)
ia(iaIndex,Y) = oa(i+1,Y) : iaIndex = iaIndex + 1
ia(iaIndex,X) = mx1
ia(iaIndex,Y) = my1 : iaIndex = iaIndex + 1
ia(iaIndex,X) = mx2
ia(iaIndex,Y) = my2 : iaIndex = iaIndex + 1
//x3, y3 with mx3, my3 and mx2, my2
ia(iaIndex,X) = oa(i+2,X)
ia(iaIndex,Y) = oa(i+2,Y) : iaIndex = iaIndex + 1
ia(iaIndex,X) = mx2
ia(iaIndex,Y) = my2 : iaIndex = iaIndex + 1
ia(iaIndex,X) = mx3
ia(iaIndex,Y) = my3 : iaIndex = iaIndex + 1
next i
//update and prepare for next level
ptCount = ptCount * 3
depth = depth + 1
redim oa(ptCount - 1, 1 )
for i = 0 to ptCount - 1
oa(i, X) = ia(i, X)
oa(i, Y) = ia(i, Y)
next i
redim ia(3 * ptCount - 1, 1)
iaIndex = 0
wend
end sub

//Test Demo for the sub (NEW as 2016 - 05 - 29 !!!!!)
cx=xmax/2
cy=ymax/2
r=cy - 20
N=3
for i = 0 to 2
color 64+42*i,64+42*i,64+42*i
SierLineTri(cx, cy, cx+r*cos(2*pi/N*i), cy +r*sin(2*pi/N*i), cx + r*cos(2*pi/N*(i+1)), cy + r*sin(2*pi/N*(i+1)), 5)
next i
</lang>

Simple recursive version
<lang Yabasic>w = 800 : h = 600
open window w, h
window origin "lb"

sub SierpinskyTriangle(level, x, y, w, h)
local w2, w4, h2
w2 = w/2 : w4 = w/4 : h2 = h/2
if level=1 then
new curve
line to x, y
line to x+w2, y+h
line to x+w, y
line to x, y
else
SierpinskyTriangle(level-1, x, y, w2, h2)
SierpinskyTriangle(level-1, x+w4, y+h2, w2, h2)
SierpinskyTriangle(level-1, x+w2, y, w2, h2)
end if
end sub

SierpinskyTriangle(7, w*0.05, h*0.05, w*0.9, h*0.9)</lang>


=={{header|zkl}}==
=={{header|zkl}}==