Author Topic: Trying simple fractals  (Read 40 times)

Marcus

  • Administrator
  • Hero Member
  • *****
  • Posts: 552
    • View Profile
Trying simple fractals
« on: November 17, 2017 »
I've never tried creating any fractals before, so don't laugh :)  Here are two simple line based ones, will try creating polygon based fractals next:

Code: [Select]
' Some simple fractals.

import "ifx.lib"

visible:
lineCount = 0
hidden:

h = 640
w = h*16/9

set window (screenw() - w)/2, (screenh() - h)/2, w, h
'set window screenw() - w - 32, screenh() - h - 64, w, h
x# = float(width(primary)/2)
y# = float(height(primary))
l# = float(height(primary))*0.26
a# = 270.0

if not IFX_Init() then end

set redraw off
create image 0, width(primary), height(primary)

' A tree fractal.
set image 0
_FractalOne x, y, l, a, 1, 16.0
set color 0, 0, 0
draw rect 0, 0, 256, 32, true
set color 255, 255, 255
set caret 8, 8
wln "Tree fractal: ", lineCount, " lines"
set image primary
_DrawWithGlow 0, 128, 200, 255
redraw
wait keydown

' A line fractal.
set image 0
set color 0, 0, 0; cls
set color 255, 255, 255
lineCount = 0
'_FractalTwo 0.0, float(h/2), float(w), float(h), 16.0
_FractalTwo 0.0, float(h), float(w), float(h), 16.0
set color 0, 0, 0
draw rect 0, 0, 256, 32, true
set color 255, 255, 255
set caret 8, 8
wln "Line fractal: ", lineCount, " lines"
set image primary
set color 0, 0, 0; cls
_DrawWithGlow 0, 255, 64, 8
redraw
wait keydown

procedure DrawWithGlow(img, r, g, b)
set color r, g, b
set additive true
for i = 5 downto 0
draw image img, 0, 0
_IFX_Blur max(i*8, 1)
next
set color 255, 255, 255
draw image img, 0, 0
set additive false
endproc

procedure FractalOne(x#, y#, l#, a#, step, limSqr#)
x2# = x + l*cos(a)
y2# = y + l*sin(a)
dx# = x2 - x
dy# = y2 - y
if dx*dx + dy*dy < limSqr then return
_FractalOne x2, y2, l*0.75, a + 22.5, step + 1, limSqr
_FractalOne x2, y2, l*0.75, a - 45.0, step + 1, limSqr
set color step*12, step*12, step*12
draw line int(x), int(y), int(x2), int(y2)
lineCount = lineCount + 1
endproc

procedure FractalTwo(x1#, y1#, x2#, y2#, limSqr#)
dx# = x2 - x1
dy# = y2 - y1
if dx*dx + dy*dy < limSqr
draw line int(x1), int(y1), int(x2), int(y2)
lineCount = lineCount + 1
return
else
cx1# = x1 + dx*1.0/4.0
cy1# = y1 + dy*1.0/4.0
cx2# = x1 + dx*1.0/2.0
cy2# = y1 + dy*1.0/2.0
draw line int(x1), int(y1), int(cx1), int(cy1)
draw line int(cx2), int(cy2), int(x2), int(y2)
lineCount = lineCount + 2
cx# = dy*0.686
cy# = -dx*0.686

_FractalTwo cx1, cy1, (x1 + x2)*0.5 + cx, (y1 + y2)*0.5 + cy, limSqr
_FractalTwo (x1 + x2)*0.5 + cx, (y1 + y2)*0.5 + cy, cx2, cy2, limSqr
endif
endproc

Sorry for trying to make them look cooler with glow effects, but ... it does the trick.
« Last Edit: November 17, 2017 by Marcus »
.\\\opz

Rick3137

  • Sr. Member
  • ****
  • Posts: 347
  • May the Force be with You
    • View Profile
    • Rick's Programs
Re: Trying simple fractals
« Reply #1 on: November 17, 2017 »
 Nice work.

   I learnt a new trick. Your use of " return" to limit a recursion.
   I somehow missed that one when I read the examples the first time.