Thought I'd share this and get some advice

H

Hiato Xaero

[Note: parts of this message were removed to make it a legal post.]

I decided to take it upon myself to write a text Mandelbrot Set Generator in
Ruby (seeing as I couldn't find any...)

Anyway, here is the code, it's procedural style, not entirely utilizing Ruby
to its fullest, but hey

p ' '
p "NOTE: Do not run this from a console that has less than 128 columns, 130
to be safe! If you fail to do so, then you will see rubbish inplace of the
mandelbrot set"
p ' '

i = -24.00
until (i>=24)
k = -64.00
s = ''
until (k>=63)
p = 0
x = 0
y = 0
while ((x*x)+(y*y)<4)
xt = x*x - y*y + (k-23)/43.5
yt = 2*x*y + i/22
x = xt
y = yt
p = p + 6
break unless p<260
end
case p
when 1..10: s << 'A'
when 10..20: s << 'B'
when 20..30: s << 'C'
when 30..40: s << 'D'
when 40..50: s << 'E'
when 50..60: s << 'F'
when 60..70: s << 'G'
when 70..80: s << 'H'
when 80..90: s << 'I'
when 90..100: s << 'J'
when 100..110: s << 'K'
when 110..120: s << 'L'
when 120..130: s << 'M'
when 130..140: s << 'N'
when 140..150: s << 'O'
when 150..160: s << 'P'
when 160..170: s << 'Q'
when 170..180: s << 'R'
when 180..190: s << 'S'
when 190..200: s << 'T'
when 200..210: s << 'U'
when 210..220: s << 'V'
when 220..230: s << 'W'
when 230..240: s << 'X'
when 240..250: s << 'Y'
when 250..400: s << ' '
end
k = k + 1.00
end
p s
i = i + 1.00
end

p ' '
p "NOTE: Do not run this from a console that has less than 128 columns, 130
to be safe! If you fail to do so, then you will see rubbish inplace of the
mandelbrot set"
p ' '

gets()

Finally, any feedback is welcome, and I'm just wondering if there isn't a
more efficient way to do this.

Anyway, cheers and thanks for reading
 
S

Sebastian Hungerecker

Hiato said:
i =3D -24.00
until (i>=3D24)
[...]
i =3D i + 1.00
end
=2D24.0.step(24,1) do |i|
=2E..
end
=A0 case p
=A0 =A0 when 1..10: s << 'A'
=A0 =A0 when 10..20: s << 'B'
=A0 =A0 [...]
=A0 =A0 when 240..250: s << 'Y'
=A0 =A0 when 250..400: s << ' '
=A0 end

s << (p > 250 ? ' ' : (65 + (p-1)/10).chr)

=2D-=20
NP: Six Feet Under - Nonexistence
Jabber: (e-mail address removed)
ICQ: 205544826
 
H

Hiato Xaero

[Note: parts of this message were removed to make it a legal post.]

Thanks :) Much less code now
 
S

Sander Land

I decided to take it upon myself to write a text Mandelbrot Set Generator in
Ruby (seeing as I couldn't find any...)

Anyway, here is the code, it's procedural style, not entirely utilizing Ruby
to its fullest, but hey

p ' '
p "NOTE: Do not run this from a console that has less than 128 columns, 130
to be safe! If you fail to do so, then you will see rubbish inplace of the
mandelbrot set"
p ' '

i = -24.00
until (i>=24)
k = -64.00
s = ''
until (k>=63)
p = 0
x = 0
y = 0
while ((x*x)+(y*y)<4)
xt = x*x - y*y + (k-23)/43.5
yt = 2*x*y + i/22
x = xt
y = yt
p = p + 6
break unless p<260
end
case p
when 1..10: s << 'A'
when 10..20: s << 'B'
when 20..30: s << 'C'
when 30..40: s << 'D'
when 40..50: s << 'E'
when 50..60: s << 'F'
when 60..70: s << 'G'
when 70..80: s << 'H'
when 80..90: s << 'I'
when 90..100: s << 'J'
when 100..110: s << 'K'
when 110..120: s << 'L'
when 120..130: s << 'M'
when 130..140: s << 'N'
when 140..150: s << 'O'
when 150..160: s << 'P'
when 160..170: s << 'Q'
when 170..180: s << 'R'
when 180..190: s << 'S'
when 190..200: s << 'T'
when 200..210: s << 'U'
when 210..220: s << 'V'
when 220..230: s << 'W'
when 230..240: s << 'X'
when 240..250: s << 'Y'
when 250..400: s << ' '
end
k = k + 1.00
end
p s
i = i + 1.00
end

p ' '
p "NOTE: Do not run this from a console that has less than 128 columns, 130
to be safe! If you fail to do so, then you will see rubbish inplace of the
mandelbrot set"
p ' '

gets()

Finally, any feedback is welcome, and I'm just wondering if there isn't a
more efficient way to do this.

Anyway, cheers and thanks for reading
Hi Hiato,

Cool little script. The Mandelbrot is always nice to see. :)
Some comments:
First of all, the case statement in Ruby is really just equivalent to
if/elsif/elsif/..., so this is not very efficient. Better to replace
this with some arithmetic.

puts s over p s to avoid printing the quotes
i = i + 1.00 can be written as i += 1, but its probably better to
replace the 'until' loop with a for loop: for i in -24...24 (or just
(-24...24).each{|i| )

You can replace the loop on k and the s with a map call, and use
parallel assignment instead of xt/yt.


for i in -24...24
puts (-64...63).map{|k|
p = x = y = 0
while ((x*x)+(y*y)<4)
x, y = x*x - y*y + (k-23)/43.5, 2*x*y + i/22.0 # parallel
assignment, and /22.0 for automatic to_f on i
p = p + 6
break unless p<260
end
if p >= 250
' '
else
(?A + (p-1)/10).chr # ?A.ord in 1.9
end
}.join
end
 
S

Sebastian Hungerecker

Hiato said:
I decided to take it upon myself to write a text Mandelbrot Set Generator
in Ruby (seeing as I couldn't find any...)

(-24..23).each do |i|
puts((-64..62).inject("") do |s,k|
p = x = y = 0

while ((x*x)+(y*y)<4)
x, y = x*x - y*y + (k-23)/43.5, 2*x*y + i/22.0
p += 6
break unless p<260
end

s << (p > 250 ? ' ' : (65 + (p-1)/10).chr)
end)
end
 
H

Hiato Xaero

[Note: parts of this message were removed to make it a legal post.]

Thanks :)

Wow, Ruby is really awesome and these different methods are prime examples.
Thanks Sander and Sebastien for making my script officially Rubified :)

(I'm stuck in an eternal loop of procedural programming because of Delphi :(
)
 
K

Koni Marti

Hiato said:
Thanks :)

Wow, Ruby is really awesome and these different methods are prime examples.
Thanks Sander and Sebastien for making my script officially Rubified :)

(I'm stuck in an eternal loop of procedural programming because of Delphi :(
)

Nice example. Ruby is definitely awesome!
It also allows you to get rid of possible computational bottelnecks by
simple c extensions (which makes Ruby ideally for scientific purposes as
well).

-- Koni

Check this out, it makes the script 2-3 times faster and I'm sure one
can still improve that:

* mandelbrot.rb

require 'Mandelbrot'
f=Mandelbrot.new
(-24..23).each do |i|
puts((-64..62).inject("") do |s,k|
p = x = y = 0

while ((x*x)+(y*y)<4)
x, y = f.calculate(x,y,k,i)
#x, y = x*x - y*y + (k-23)/43.5, 2*x*y + i/22.0
p += 6
break unless p<260
end

s << (p > 250 ? ' ' : (65 + (p-1)/10).chr)
end)
end


* mandelbrot.c

#include <ruby.h>

static VALUE rb_mandel;

static VALUE rb_calculate(VALUE self, VALUE xval, VALUE yval, VALUE
kval, VALUE ival)
{
VALUE result;
double x, y, k, i;

x = NUM2DBL(xval);
y = NUM2DBL(yval);
k = NUM2DBL(kval);
i = NUM2DBL(ival);

result = rb_ary_new2(2);

rb_ary_push(result, rb_float_new((x*x - y*y + (k-23.0)/43.5)));
rb_ary_push(result, rb_float_new((2.0*x*y + i/22.0)));

return result;
}

void Init_mandelbrot(void ) {
rb_mandel = rb_define_class("Mandelbrot",rb_cObject);
rb_define_method(rb_mandel, "calculate", rb_calculate, 4);
}


* extconf.rb

require 'mkmf'

create_makefile('mandelbrot')
 
M

Marc Robert

Hiato Xaero a écrit :
Thanks :)

Wow, Ruby is really awesome and these different methods are prime examples.
Thanks Sander and Sebastien for making my script officially Rubified :)

(I'm stuck in an eternal loop of procedural programming because of Delphi :(
)


------------------------------------------------------------------------

No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.5.516 / Virus Database: 269.17.11/1200 - Release Date: 27/12/2007 13:34
funny, the short-code version take twice times than the long-code
version to get done.
 
H

Hiato Xaero

[Note: parts of this message were removed to make it a legal post.]

Yeah, strange that
 
J

John Joyce

Thanks :)

Wow, Ruby is really awesome and these different methods are prime
examples.
Thanks Sander and Sebastien for making my script officially
Rubified :)

(I'm stuck in an eternal loop of procedural programming because of
Delphi :(
)
That's not a bad thing all the time.
Since almost everything is already an object in Ruby, you can do the
procedural thing to just hand program flow.
It doesn't have to be event driven...
Even oop contains procedural programming!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top