March 28, 2024, 06:17:09 AM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Mandelbrot problem

Started by aurelCB, January 27, 2012, 03:49:05 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

aurelCB

I am trying to make mandelbrot set on standard window by drawing in pixels
and i don't know why this refuse to work.
What i do wrong ,anyone...?
def w:window
def przelx,przely,a,b,c,a2,b2,x2,y2,z:float
def x,y:INT

OPENWINDOW w,0,0,400,300,@minbox|@noautodraw,0,"Mandlebrot",&main


przelx = 3 / 400
przely = 2 / 300


FOR x = 0 TO 399
wait 1
FOR y = 0 TO 299
a = 0.0
b = 0.0
c = 0.0
x2 = (przelx * x) - 2.0
y2 = (przely * y) - 1.0

LABEL loop

IF c <= 255
frontpen w,rgb(0,0,0)
a2 = (a * a) - (b * b)
b2 = 2 * a * b
a = a2 + x2
b = b2 + y2
z = a * a + b * b

IF z > 4 THEN GOTO break
c = c + 1
GOTO loop
ENDIF

LABEL break

IF c = 255
frontpen w,rgb(220,220,0)
ENDIF

'IF c < 255
'frontpen w,rgb(0,0,0)
'ENDIF
PSET w,x,y
NEXT y
NEXT x


waituntil w=0
end

sub main
     select @class
           case @idclosewindow
           closewindow w
     endselect
return
endsub

Kian

When you set the values of przelx and przely, the divisions returned an integer value, meaning that they were always zero, so you were always effectively calculating the value for the same pixel.
Also, changing the 'if c<=255 . . . GOTO loop' to a 'WHILE' loop speeds it up quite significantly.
I have stuck in a colouring algorithm which you may or may not like.

Kian

def w:window
def przelx,przely,a,b,c,a2,b2,x2,y2,z:float
def x,y:INT

OPENWINDOW w,0,0,400,300,@minbox|@noautodraw,0,"Mandlebrot",&main


przelx = 3.0 / 400
przely = 2.0 / 300


FOR x = 0 TO 399
wait 1
FOR y = 0 TO 299
a = 0.0
b = 0.0
c = 0.0
x2 = (przelx * x) - 2.0
y2 = (przely * y) - 1.0

LABEL loop

WHILE c<=255
a2 = (a * a) - (b * b)
b2 = 2 * a * b
a = a2 + x2
b = b2 + y2
z = a * a + b * b

IF z > 4 THEN GOTO break
c = c + 1
ENDWHILE

LABEL break
IF c<=255 THEN
clr=c*20*256
frontpen w,rgb(clr,clr,clr)
ELSE
frontpen w,rgb(220,220,220)
ENDIF

PSET w,x,y
NEXT y
NEXT x



waituntil w=0
end

sub main
     select @class
           case @idclosewindow
           closewindow w
     endselect
return
endsub

aurelCB

Cool colors...
Thank you Kian ;)

aurelCB

Hmm i was wondering is there a way to speed up the thing with hDC maybe...?

Kian

January 29, 2012, 10:35:43 AM #4 Last Edit: January 29, 2012, 11:11:47 AM by Kian
You could use Sapero's nifty little trick that he used on Graham's 'Fractal-like patterns'.
def w:window
def przelx,przely,a,b,c,a2,b2,x2,y2,z:float
def x,y:INT

OPENWINDOW w,0,0,400,300,@minbox|@noautodraw,0,"Mandlebrot",&main


przelx = 3.0/ 400
przely = 2.0 / 300

int hdcOld=w.m_hPrintDC
w.m_hPrintDC=GETHDC(w)

FOR x = 0 TO 399
wait 1
FOR y = 0 TO 299
a = 0.0
b = 0.0
c = 0.0
x2 = (przelx * x) - 2.0
y2 = (przely * y) - 1.0

LABEL loop

WHILE c<=255
a2 = (a * a) - (b * b)
b2 = 2 * a * b
a = a2 + x2
b = b2 + y2
z = a * a + b * b

IF z > 4 THEN GOTO break
c = c + 1
ENDWHILE

LABEL break
IF c<=255 THEN
clr=c*20*256
frontpen w,rgb(clr,clr,clr)
ELSE
frontpen w,rgb(220,220,220)
ENDIF

PSET w,x,y
NEXT y
NEXT x
int hdc=w.m_hPrintDC
w.m_hPrintDC=hdcOld
RELEASEHDC(w,hdc)


waituntil w=0
end

sub main
    select @class
          case @idclosewindow
          closewindow w
    endselect
return
endsub




You could also remove the @noautodraw from the 'WINDOW' creation to speed it up, but that depends on whether you want to see it update and if you need to respond at any point to an @IDPAINT message.

Kian

aurelCB

Yes just that trick i need ...cool man and thank you.. ;)