Author Topic: Mandelbrot  (Read 4638 times)

Cosmo

  • Guest
Mandelbrot
« on: February 02, 2014, 01:26:15 AM »
Hello,

« Last Edit: December 12, 2014, 05:05:25 PM by PeterMaria »


Cybermonkey

  • Administrator
  • *****
  • Posts: 0
Re: Mandelbrot
« Reply #2 on: February 02, 2014, 11:49:27 AM »
Port to ChipmonkeyLua:
Code: [Select]
local x=0;y=0;zx=0;zy=0;cx=0;cy=0;tmp=0;i=0;a=0
local m=255;z=150;w=0;h=0

lowres()
activepage (0)
ink (0)
paper (65535)
cls()
print("WAIT A LITTLE BIT.....")
visualpage (0)

activepage (1)
paper (65535)
cls()
time1 = gettickcount()
h = screenheight()
w = screenwidth()

for y=0,h do
  for x=0,w do
    zx= 0
    zy= 0
    cx= (x-400)/z
    cy= (y-300)/z
    i = m
  while zx*zx+zy*zy <4 and i >0 do
    tmp = zx*zx-zy*zy+cx
    zy  = 2*zx*zy+cy
    zx  = tmp
    i = i -1
  end
    ink (hrgb(i*9,i*4,i*7))
    pset(x,y)
  end
end
time2=gettickcount()
ink (0)
print ("Time needed: "..((time2-time1)/1000).." sec.")
visualpage (1)

Cybermonkey

  • Administrator
  • *****
  • Posts: 0
Re: Mandelbrot
« Reply #3 on: February 02, 2014, 02:49:42 PM »
Nice to watch with CMLua:
Code: [Select]
--'by cosmo alias peter wirbelauer

paper (hrgb(240,255,255))
cls()
page = 0
local a,v = 0,0
w = screenwidth() /2
h = screenheight()/2
r = math.rad

repeat
activepage (page)
cls()
for a=0,360,10 do
    ink(0)
    circle(w+math.sin(r(a))*100, h+math.cos(r(a))*100, 10)
    circle(w+math.cos(r(a))*110, h+math.sin(r(a))*110, 10)
end
ink (hrgb(255,0,0))
fillcircle(w+math.sin(r(v))* 80, h+math.cos(r(v))* 80, 10)
ink(hrgb(0,0,255))
fillcircle(w+math.cos(r(v))*110, h+math.sin(r(v))*110, 10)

v = v+10
if v >360 then
   v=0
end
sleep (20)
visualpage (page)
page = page + 1
if page > 1 then
page = 0
end
until keypressed()

Tomaaz

  • Guest
Re: Mandelbrot
« Reply #4 on: February 02, 2014, 05:13:48 PM »
Is by me, I am PeterMaria  or Peter Wirbelauer or peterW or peter or xpeter or peterpan.

Well, how am I suppose to know it? Anyway, I guess you're not Bereb and I'm 100% sure you're not Tomaaz, so two of my links are still useful (at least for real newcomers).
« Last Edit: February 02, 2014, 05:16:41 PM by Tomaaz »

Aurel

  • Guest
Re: Mandelbrot
« Reply #5 on: February 05, 2014, 07:18:32 AM »
Cosmo...  ;D

Bereb

  • Guest
Re: Mandelbrot
« Reply #6 on: February 05, 2014, 08:43:14 AM »
I took the liberty of modifying a few little things, just to show the advantage here of local variables and functions (even Lua's buit-in functions) to improve performance... (see v  which is incremented by 2 now, instead of 10)


Code: [Select]
--'by cosmo alias peter wirbelauer

screen(640,480,0,"SINE 2")
backcolor(240,255,255)

local a, v = 0, 0
local w = screenwidth() /2
local h = screenheight()/2
local rad = math.rad

repeat
k = getkey()
clearscreen()
for a=0,360,10 do
color(0,0,0)
circle(w+sin(rad(a))*100, h+cos(rad(a))*100, 10)
circle(w+cos(rad(a))*110, h+sin(rad(a))*110, 10)
end
color(255,0,0)
fillcircle(w+sin(rad(v))* 80, h+cos(rad(v))* 80, 10)
color(0,0,255)
fillcircle(w+cos(rad(v))*110, h+sin(rad(v))*110, 10)
v = v + 2
redraw()
until k == 27

closewindow()

Note : I don't use setframetimer() because I don't know what's the use of it  :-[
Nevertheless I would like to get some clearing up about it, and some demonstrations of its real effectiveness...

Tomaaz

  • Guest
Re: Mandelbrot
« Reply #7 on: February 05, 2014, 10:47:37 AM »
Note : I don't use setframetimer() because I don't know what's the use of it  :-[

setframetimer() lets you control speed of your animations. sync() is basically wait() + redraw() and setframetimer() sets the value of wait(). So setframetimer(1) means that sync will wait 1 sec. before it refreshes the screen and setframetimer(60) means that sync will wait 1/60 sec. before refreshing the screen (in other words your code will run on 60 frames per second). Here is a simple example:

Code: [Select]
openwindow(640, 480, 32, "Test")
setframetimer(10)
x, y, a, b = 20, 20, 4, 4
repeat
clearscreen()
fillcircle(x, y, 20)
sync()
x = x + a
y = y + b
if x > 620 or x < 20 then
a = -a
end
if y > 460 or y < 20 then
b = -b
end
until getkey() == 27

Just play with setframetime() value.

Bereb

  • Guest
Re: Mandelbrot
« Reply #8 on: February 05, 2014, 01:34:25 PM »
Thank you Tomaaz !
Because EGSL is sometimes too fast, I was looking precisely for different ways to slow down the stuff. In order to do, I used to play with possible iterations or with the wait() function, but it's not always satisfactory.
Your explanations and your example are clear and simple, very usefull for me
 :)

Bereb

  • Guest
Re: Mandelbrot
« Reply #9 on: February 05, 2014, 06:27:13 PM »
Fine, I like this kind of short script  8)

It reminds me a script which I adapted from Processing: Distance 2D
Quote
Move the mouse across the image to obscure and reveal the matrix. Measures the distance from the mouse to each square and sets the size proportionally.

Code: [Select]
-------------------------------------------------------
-- * DISTANCE 2D * --
--    Faire bouger la souris sur l'image pour masquer
--    ou révéler la matrice de points.
--    La taille de chaque cercle dépend de la distance
--    entre les cercle et le curseur de la souris.
--  Adapté de Processing :
--  http://processing.org/examples/distance2d.html
-------------------------------------------------------
-- EGSL 1.5.0   -   avril 2012  (Bereb)
-------------------------------------------------------
openwindow(300,300,32,"Distance 2D")
backcolour(50,50,50)
colour(250,250,250)
clearscreen()

-- fonction calculant la distance entre 2 points
function dist(xA, yA, xB, yB)
return math.sqrt( (xB - xA)*(xB - xA) + (yB - yA)*(yB - yA) )
end

Maxdistance = dist( 0, 0, screenwidth(), screenheight() )

-- boucle principale
while getkey() ~= 27 do
clearscreen()
for i = 0, screenwidth(), 20 do
for j = 0, screenheight(), 20 do
taille = dist(mousex(), mousey(), i, j)
taille = taille / Maxdistance * 40
fillcircle(i, j, taille)
end
end
redraw()
end

closewindow()

Processing can be a great source of inspiration for that type of program.
Sorry for the comments in french. I had no time to translate them, so please see the link.
« Last Edit: February 05, 2014, 06:30:34 PM by Bereb »