Fractals
None
koch.gle
koch.gle
size 10 10
sub koch x1 y1 x2 y2 d
if d = 0 then
amove x1 y1
aline x2 y2
else
local x13 = x1+(x2-x1)/3
local y13 = y1+(y2-y1)/3
local x23 = x1+(x2-x1)*2/3
local y23 = y1+(y2-y1)*2/3
local x_c = x1+0.5*(x2-x1)+(y2-y1)/sqrt(12)
local y_c = y1+0.5*(y2-y1)-(x2-x1)/sqrt(12)
koch x1 y1 x13 y13 d-1
koch x13 y13 x_c y_c d-1
koch x_c y_c x23 y23 d-1
koch x23 y23 x2 y2 d-1
end if
end sub
sub kochflake wd depth
local x0 = pagewidth()/2-wd
local y0 = pageheight()/2-wd*tan(torad(30))
local x1 = x0+wd
local y1 = y0+2*wd*sin(torad(60))
local x2 = x0+wd*2
koch x0 y0 x2 y0 depth
koch x1 y1 x0 y0 depth
koch x2 y0 x1 y1 depth
end sub
for i = 1 to 4
set color rgb(0,0,i/5)
kochflake 0.4*(1.8^i) i
next i
pythagorean.gle
pythagorean.gle
size 32 22
a = 3; b = 4; c = sqrt(a^2+b^2)
sub pythagorean n
local red = (34+n*92)/(n+1)
local green = (139+n*64)/(n+1)
local blue = (34+n*51)/(n+1)
box c c fill rgb255(red,green,blue)
if n = 0 then return
begin translate c c
begin rotate todeg(-acos(a/c))
begin scale a/c a/c
begin translate -c 0
pythagorean n-1
end translate
end scale
end rotate
end translate
begin translate 0 c
begin rotate todeg(acos(b/c))
begin scale b/c b/c
pythagorean n-1
end scale
end rotate
end translate
end sub
begin translate 16 0
pythagorean 10
end translate
recursiontree.gle
recursiontree.gle
size 8 8
! Based on "C" code by Lode Vandevenne
! http://www.student.kuleuven.ac.be/~m0216922/CG/
set cap round
maxRecursions = 8 ! never make this too big or it'll take forever
angle = 0.2 * pi ! angle in radians
shrink = 1.8 ! relative size of new branches
! Now follows the recursion function, a function that only draws a single line and calls
! itself a few times, but results in a huge tree!
sub recursion posX posY dirX dirY bsize n
! Here's the part that draws the line. The line is drawn from (posX, posY) to (posX+dirX,
! posY+dirY). So the position and direction of the line is given as a vector, instead of
! a begin point, an angle and a size, because direction vectors are much easier to work
! with. The size parameter itself isn't used to draw the line directly, it's only needed
! later to calculate the direction vector of the next branches. If the maximum number of
! recursions is reached, the function returns immediately after drawing the line, without
! calling itself again.
local posX2 = posX + bsize * dirX
local posY2 = posY + bsize * dirY
if n > 5 then
set color forestgreen
else
set color rgb255(128, 96, 64)
end if
set lwidth 0.2/(n+1)+0.005
amove posX pageheight()-posY
aline posX2 pageheight()-posY2
if n < maxRecursions then
! And in the second part of the function, the new position of the new branches is
! calculated as the endpoint of the previous branch, and the new direction vector for
! the new branches is calculated out of the size and current direction of the current
! branch. The new branches are rotated with the angle, the sine and cosine formulas are
! actually the calculation of the rotation matrix. Then the recursion function is called
! again with the new branch as its parameters. This is done twice: once for a branch
! rotated to the right, and then for a branch rotated to the left.
dirX2 = cos(angle) * dirX + sin(angle) * dirY
dirY2 = -sin(angle) * dirX + cos(angle) * dirY
recursion posX2 posY2 dirX2 dirY2 bsize/shrink n+1
dirX2 = cos(-angle) * dirX + sin(-angle) * dirY
dirY2 = -sin(-angle) * dirX + cos(-angle) * dirY
recursion posX2 posY2 dirX2 dirY2 bsize/shrink n+1
end if
end sub
! The main program doesn't do much more than calling the recursive function.
! The value "h/2.3" in the parameters of the recursion function is the initial
! length of the first branch (the stem). The coordinates are the begin
! point and direction vector of the first branch.
w = pagewidth()
h = pageheight()
recursion w/2 h-0.1 0 -1 h/2.3 0
sierpinski.gle
sierpinski.gle
size 10 8.75
wd = 4.75; ad = 0.2
set lwidth 0.01 cap round
sub a_line x0 y0 x1 y1
amove x0 y0
aline x1 y1
end sub
sub sierpinski x_left x_right y_bot y_top d
if d > 0 then
set color rgb(1/(d+ad),0,0)
local y_c = (y_bot+y_top)/2 ! middle y coordinate
local x_c = (x_left+x_right)/2 ! middle x coordinate
local x_1 = (x_left+x_c)/2 ! 1/3 x coordinate
local x_2 = (x_c+x_right)/2 ! 2/3 x coordinate
a_line x_c y_bot x_1 y_c
a_line x_1 y_c x_2 y_c
a_line x_2 y_c x_c y_bot
sierpinski x_left x_c y_bot y_c d-1 ! lower left triangle
sierpinski x_c x_right y_bot y_c d-1 ! lower right triangle
sierpinski x_1 x_2 y_c y_top d-1 ! top triangle
end if
end sub
x_left = pagewidth()/2 - wd
y_bot = pageheight()/2 - wd*sin(torad(60))
y_top = y_bot + 2*wd*sin(torad(60))
x_right = x_left + wd*2
x_c = x_left + wd
a_line x_left y_bot x_c y_top
a_line x_c y_top x_right y_bot
a_line x_right y_bot x_left y_bot
sierpinski x_left x_right y_bot y_top 7