IonicWind Software

Creative Basic => 2D/3D => Topic started by: GWS on August 15, 2011, 02:46:07 AM

Title: Fractal-like Patterns
Post by: GWS on August 15, 2011, 02:46:07 AM
Hi,

I love this type of program, which generates amazing patterns from just a few mathematical equations. ;D

I came across the algorithm on another site, and translated it to CB, which is much easier to understand.


' Mathematical Patterns - GWS Aug 2011

def w:window
def i,n,red,grn,blu:int
def wW,wH,dots,xpos,ypos:int
def col[20,4],repeat,run:int
def a,b,c,x,y,z,j,xfocus,yfocus:float

autodefine = "off"
getscreensize wW,wH

window w, -wW,-wH, wW, wH,0,0 ,"CB Fractal Patterns", handler
centerwindow w
setwindowcolor w,rgb(0,0,0)

xfocus = wW * 0.47
yfocus = wH * 0.45

run = 1

control w,"B,Exit,2*(wW - 60)/3,wH * 0.85,60,30,@CTLBTNFLAT, 1"
control w,"B,Next,(wW - 60)/3,wH * 0.85,60,30,@CTLBTNFLAT, 2"
for i = 1 to 2
  setcontrolcolor w,i,rgb(124,171,255),rgb(0,80,180)
next i

' set up random colors ..
for i = 0 to 15
red = rnd(255) : grn = rnd(255) : blu = rnd(255)
col[i,1] = red : col[i,2] = grn : col[i,3] = blu
next i

draw

waituntil run = 0
closewindow w
end

SUB handler
select @CLASS
case @IDCLOSEWINDOW
run = 0
case @IDCONTROL
select @CONTROLID
' clicking the Exit button ...
case 1
run = 0
case 2
' clicking the new image button ...
setwindowcolor w,rgb(0,0,0)
draw
endselect
endselect
return

sub draw

for repeat = 1 to rnd(5)
' set up some random starting positions ..
a = rnd(1.0)
b = 0.9998
c = 2 - 2 * a

dots = 15000

x = 0 : j = 0
y = rnd(1.0)*12 + 0.1

' calculate and draw the points ..
for i = 0 to dots
z = x
x = b * y + j
j = a * x + c * (x^2)/(1 + x^2)
y = j - z
xpos = x*20 + xfocus
ypos = y*20 + yfocus
pset w,xpos,ypos,rgb(col[i/1000,1],col[i/1000,2],col[i/1000,3])
next i

next repeat

return
       


Hope you like it, I've included the .exe for those unfortunate folk who haven't discovered CB yet  :)

Graham

[Edit: I've randomised the repeat loop - I think it looks nicer ..  :)]
Title: Re: Fractal-like Patterns
Post by: aurelCB on August 15, 2011, 02:59:29 PM
Well that is really cool Graham :)

PS...Look into my translation :-X
Title: Re: Fractal-like Patterns
Post by: GWS on August 16, 2011, 02:12:50 AM
He! he .. glad you liked it.  ;D

It's one of the things I like about Basic, that it can be so easily understood and ported to pretty well any Basic dialect.

Your version looks fine  :)

all the best, :)

Graham
Title: Re: Fractal-like Patterns
Post by: sapero on August 16, 2011, 05:35:09 AM
Graham, I love this kind of programs!
Have converted it to IWB and it ran just fine. Modified a bit the draw function to use a cached device context, and that boosted it to turbo ;D

Note: the following is for IWBasic
sub draw
int hdcOld = w.m_hPrintDC ' added
w.m_hPrintDC = GETHDC(w)

' the original code goes here

int hdc = w.m_hPrintDC ' added
w.m_hPrintDC = hdcOld
RELEASEHDC(w, hdc)
endsub


The trick above changes the GETHDC and RELEASEHDC functions called from within PSET command, to use the preinitialized printer device context.
Title: Re: Fractal-like Patterns
Post by: GWS on August 16, 2011, 07:38:07 AM
Hi Sapero,

Pardon me chuckling ..  ;D ;D .. I wouldn't know a 'cached device context' if it walked up and kicked me ..  :o

But I'm pleased you found it interesting   :)

Could you post the IWB version in the IWB area, so folks can see what speed really looks like.

IWB's graphics are very impressive - one of the reasons it's well worth buying.
I shall use it a bit more once the copy protection scheme is sorted.

Of course, CB - the little cheapo Basic - will always be my favourite.  It should have had much improved graphics facilities, and a compile feature if my funded commissions had been carried out.  Unfortunately, that never materialised.  Ah well! .. have to be thankful for what we've got.

best wishes, :)

Graham
Title: Re: Fractal-like Patterns
Post by: Rock Ridge Farm (Larry) on August 16, 2011, 08:43:33 AM
We may yet get to updating Cbasic - it is on my to-do list.

Larry
Title: Re: Fractal-like Patterns
Post by: pistol350 on August 16, 2011, 08:53:43 AM
The difference in terms of speed is "STRIKING!"  8)
Title: Re: Fractal-like Patterns
Post by: GWS on August 16, 2011, 10:55:12 AM
Larry ..  ;D

Graham