Thought I'd share this and get some advice

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 ((xx)+(yy)<4)
xt = xx - yy + (k-23)/43.5
yt = 2xy + 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

Hiato X. wrote:

i = -24.00
until (i>=24)
[…]
i = i + 1.00
end
-24.0.step(24,1) do |i|

end

case p
when 1…10: s << ‘A’
when 10…20: s << ‘B’
[…]
when 240…250: s << ‘Y’
when 250…400: s << ’ ’
end

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

Sebastian H. wrote:

Hiato X. wrote:

i = -24.00
until (i>=24)
[…]
i = i + 1.00
end

-24.0.step(24,1) do |i|

end

step(23,1), actually.

Thanks :slight_smile: Much less code now

On Dec 28, 2007 12:57 PM, Hiato X. [email protected] wrote:

p ’ ’
xt = xx - yy + (k-23)/43.5
when 30…40: s << ‘D’
when 140…150: s << ‘O’
when 250…400: s << ’ ’
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. :slight_smile:
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 ((xx)+(yy)<4)
x, y = xx - yy + (k-23)/43.5, 2xy + 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

Hiato X. wrote:

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

Thanks :slight_smile:

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

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

Hiato X. a écrit :

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.

Yeah, strange that

On Dec 28, 2007, at 8:20 AM, Hiato X. wrote:

Thanks :slight_smile:

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

(I’m stuck in an eternal loop of procedural programming because of
Delphi :frowning:
)
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!

Hiato X. wrote:

Thanks :slight_smile:

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

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

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’)

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs