summaryrefslogtreecommitdiff
path: root/ext/mandel/mandel.c
blob: 359c0756da254510aeb26240492ec95a2ab1e07c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/************************************************

  mandel.c -

  $Author$

************************************************/

#include "ruby.h"
#include "math.h"

static VALUE
mandel(self, re, im, max)
    VALUE self;
    VALUE re;
    VALUE im;
    VALUE max;
{
    double real, image;
    double z_real, z_image;
    double tmp_real;
    int maximum;
    int i;
	
    Check_Type(re, T_FLOAT);
    Check_Type(im, T_FLOAT);
    Check_Type(max, T_FIXNUM);

    real = RFLOAT(re)->value;
    image = RFLOAT(im)->value;
    maximum = FIX2INT(max);

    /***
    z = c = Complex(re, im)
    for i in 0 .. $max_deapth
      z = (z * z) + c
      break if z.abs > 2
    end
    return i
    ***/

    z_real = real;
    z_image = image;
    for (i = 0; i < maximum; i++) {
	tmp_real = ((z_real * z_real) - (z_image * z_image)) + real;
	z_image = ((z_real * z_image) + (z_image * z_real)) + image;
	z_real = tmp_real;
	if ( ((z_real * z_real) + (z_image * z_image)) > 4.0 ) {
	    break;
	}
    }
    return INT2FIX(i);
}

Init_mandel()
{
    VALUE mMandel = rb_define_module("Mandel");
    rb_define_module_function(mMandel, "mandel", mandel, 3);
}