arrays in srucs

Andy Balba pwplus7 at gmail.com
Fri Jul 31 18:29:47 UTC 2020


On Friday, 31 July 2020 at 17:26:17 UTC, Paul Backus wrote:
> On Friday, 31 July 2020 at 17:02:46 UTC, Andy Balba wrote:
>> The above code, compiles and runs ok  .. but sometimes I get 
>> run runtime errors using the same paradym, which disappear 
>> when I substitute (img.p)[i]
>>
>> Any explanation for this ?
>
> Can you show an example where it doesn't work?

Copy the code block below into your editor then
Look down at second statement marked "// printf "

when I use () it works fine, but without () it gives run-time 
errors

import image_BMP ;
import colorMaps : hsv_colormap, jet_colormap, prism_colormap, 
vga_colormap ;

import std.math ;
import std.random ;
import std.stdio ;

int main()
{  int cmapM= 2, cmapJ= 3; // cmap:  0=hsv_colormap, 
1=jet_colormap, 2=prism_colormap, 3= vga_colormap
                            // cmapM, cmapJ = colormap for 
Mendelbrot, Julia
    const uint W = 1200, H =  800, Bp=3, row_size= W*Bp ; // 
fractal width, height, Bytes per pixel
    IFImage img ;  // fractal image
    img.w = W; img.h = H; img.c = ColFmt.RGB; img.pixels = new 
ubyte[H*W*3];   // c= color format: 1=Gray 2=Gray+Alpha, 3=RGB  
4=RGB +Alpha
       // 1200 * 800 *3 = 2.8 MB per image     4 images= 11.2 MB
       int max_iterations= 1000;
       for (int Y=0; Y< H; ++Y)
       {  for (int X=0; X< W; ++X)
          {
             double cr= 1.5 *(2.0*X /W-1.0) -0.5,  ci= (2.0*Y /H 
-1.0); // Mendelbrot Fractal constant= (cr, ci)
             double nr=0, ni= 0,  pr=0, pi = 0;
             for (int i= 0; i< max_iterations; i++)
             {  pr= nr; pi = ni;  nr= pr*pr -pi*pi +cr;  ni= 
2*pr*pi +ci;  // z(real, imag)= (pr,pi),  z^2+c (real, imag)= 
(nr,ni)
                if (((nr*nr) +(ni*ni)) > 4)
                {  if (max_iterations != i)
                   {  double z= sqrt(nr*nr +ni*ni);
                      int cid = cast(int) (1000.0*log2(1.75 +i 
-log2(log2(z)))/ log2(max_iterations));
                      ubyte[3][4] c= [ hsv_colormap[cid], 
jet_colormap[cid], prism_colormap[cid], vga_colormap[cid] ];
//printf("Y%i X%i :\n", Y, X);  stdout.flush();
                      foreach ( p; 0..3 )
                      {
//printf(">%i %i %i", p, cmapM, Y*row_size +X*Bp +p );   
stdout.flush();
                          img.pixels [ Y*row_size +X*Bp +p ] = 
c[p][cmapM] ;
                      }
                   }
                   break;
       }  }  }  }
       write_image ( "M_fractal.bmp", cast(long) img.w, cast(long) 
img.h, img.pixels, cast(long)img.c ) ;



More information about the Digitalmars-d-learn mailing list