In a similar vein to the above, we have a Koch Snowflake as well:
(Edited the original post title to include (AND OTHERS). If I keep doing more of these, I'll just put them all here together, rather than create a dozen topics with such similar snippets and programs.)
Code: (Select All)
_Title "Koch Snowflake in QB64PE with 32-bit Color"
Screen _NewImage(640, 480, 32)
Randomize Timer
Const xmid = 320, ymid = 240, side = 200, PI = _Pi
depth = 8
' Calculate the initial points of the equilateral triangle
x1 = xmid - side / 2: y1 = ymid + side * Sin(PI / 3) / 2
x2 = xmid + side / 2: y2 = ymid + side * Sin(PI / 3) / 2
x3 = xmid: y3 = ymid - side * Sin(PI / 3)
' Draw the three sides of the Koch Snowflake
For depth = 0 To 7
Cls
Color _RGB32(Rnd * 255, Rnd * 255, Rnd * 255)
DrawKoch x1, y1, x2, y2, depth
DrawKoch x2, y2, x3, y3, depth
DrawKoch x3, y3, x1, y1, depth
_Delay 2
Next
System
Sub DrawKoch (x1 As Single, y1 As Single, x2 As Single, y2 As Single, depth As Integer)
If depth = 0 Then
Line (x1, y1)-(x2, y2)
Else ' Calculate the points for the Koch Snowflake segment
dx = x2 - x1
dy = y2 - y1
xA = x1 + dx / 3
yA = y1 + dy / 3
xB = x1 + dx * 2 / 3
yB = y1 + dy * 2 / 3
xC = (x1 + x2) / 2 - (Sin(PI / 3) * (y2 - y1)) / 3
yC = (y1 + y2) / 2 + (Sin(PI / 3) * (x2 - x1)) / 3
' Recursively draw the four segments of the Koch Snowflake
DrawKoch x1, y1, xA, yA, depth - 1
DrawKoch xA, yA, xC, yC, depth - 1
DrawKoch xC, yC, xB, yB, depth - 1
DrawKoch xB, yB, x2, y2, depth - 1
End If
End Sub
(Edited the original post title to include (AND OTHERS). If I keep doing more of these, I'll just put them all here together, rather than create a dozen topics with such similar snippets and programs.)