(*****
/************************************************

  mandel.c -

  $Author: moriq $

************************************************/
****)

// #include "ruby.h"

library mandel;
uses
  Rubies,
  Pythia;
{$E so}
var
  mMandel: Tvalue;

//static VALUE
//mandel(self, re, im, max)
//    VALUE self;
//    VALUE re;
//    VALUE im;
//    VALUE max;
function _mandel(This, re, im, max: Tvalue): Tvalue; cdecl;

//{
//    double real, image;
//    double z_real, z_image;
//    double tmp_real;
//    int maximum;
//    int i;
var
  real, image, z_real, z_image, tmp_real: extended;
  maxnum, i : Integer;
begin

//
//    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);
  real := dl_Double(re);
  image := dl_Double(im);
  maxnum := dl_Integer(max);

//    z_real = real;
//    z_image = image;
  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;
//        }
//    }
  for i := 0 to maxnum-1 do begin
    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 ) then begin
        result := ap_Integer(i);
        exit;
    end;
  end;
  result := max;
end;

//    return INT2FIX(i);
//}
  //result := rb_ary_new;
  //rb_ary_push(result, ap_Float( z_real ));
  //rb_ary_push(result, ap_Float( z_Image ));

//Init_mandel()
//{
//    VALUE mMandel = rb_define_module("Mandel");
//    rb_define_module_function(mMandel, "mandel", mandel, 3);
//}
procedure Init_mandel;
begin
  mMandel := rb_define_module('Mandel');
  rb_define_module_function(mMandel, 'mandel', @_mandel, 3);
end;

exports
  Init_mandel;

end.
