Allow specifying a zoom number to draw blocky versions.
[fractalbiscuit.git] / fracture.pde
blob541c2a0d011ccb82aa5343998fcfb5117a36673b
1 /*
3   Hungarian notation:
5   s - screen position (unzoomed)
6   z - screen position (zoomed)
7   c - co-ordinate on the complex plane
9 */
11 int zSizeX = 800;
12 int zSizeY = 800;
13 int initial_zoom = 16;
14 int num_iterations = 50;
15 double min_re = -1.5;
16 double max_re =  0.5;
17 double min_im = -1;
18 double max_im =  1;
19 double prev_min_iterations = 200;
21 int zoom = initial_zoom;
22 int sSizeX;
23 int sSizeY;
24 double cur_min_iterations;
25 double cWidth;
26 double cHeight;
27 double cCentreX;
28 double cCentreY;
29 double mult_x;
30 double mult_y;
31 int zDragX = 0;
32 int zDragY = 0;
33 int zZoomY = 0;
34 boolean anything_changed = true;
36 boolean drawing = false;
38 void setup()
40   size( zSizeX, zSizeY );
41   do_update_pixels( false );
42   frameRate( 10 );
45 void draw()
47   cWidth  = max_re - min_re;
48   cHeight = max_im - min_im;
50   if( anything_changed )
51   {
52     cur_min_iterations = num_iterations;
54     mult_x = cWidth  / (double)sSizeX;
55     mult_y = cHeight / (double)sSizeY;
57     do_update_pixels( true );
59     draw_crosshair();
60   }
62   anything_changed = update_view();
65 void mousePressed()
67   if( mouseButton == CENTER )
68   {
69     println( cCentreX + " + " + cCentreY + "i" );
70     println( "width = " + (max_re - min_re) );
71   }
72   frameRate( 50 );
75 void mouseReleased()
77   frameRate( 10 );
80 void mouseDragged()
82   if( mouseButton == RIGHT )
83   {
84     zZoomY += mouseY - pmouseY;
85   }
86   else
87   {
88     zDragX += mouseX - pmouseX;
89     zDragY += mouseY - pmouseY;
90   }
93 void draw_crosshair()
95   int zSizeXOver2 = (zSizeX) / 2;
96   int zSizeYOver2 = (zSizeY) / 2;
98   stroke( 255, 0, 0, 75 );
99   line( zSizeXOver2, zSizeYOver2 - 20, zSizeXOver2, zSizeYOver2 - 5 );
100   line( zSizeXOver2, zSizeYOver2 + 20, zSizeXOver2, zSizeYOver2 + 5 );
101   line( zSizeXOver2 - 20, zSizeYOver2, zSizeXOver2 - 5, zSizeYOver2 );
102   line( zSizeXOver2 + 20, zSizeYOver2, zSizeXOver2 + 5, zSizeYOver2 );
105 void do_update_pixels( boolean really_draw )
107   sSizeX = zSizeX / zoom;
108   sSizeY = zSizeY / zoom;
110   for( int y = 0; y < sSizeY; ++y )
111   {
112     int zY = y*zoom;
113     for( int x = 0; x < sSizeX; ++x )
114     {
115       int zX = x*zoom;
117       double coord_x = min_re + ( mult_x * (x+(zoom/2)) );
118       double coord_y = min_im + ( mult_y * (y+(zoom/2)) );
119       color col = get_pixel_color( coord_x, coord_y );
121       if( really_draw )
122       {
123         stroke( col, 255 );
124         fill( col, 255 );
125         rect( zX, zY, zoom - 1, zoom - 1 );
126         //point( xzoom, yzoom );
127       }
128     }
129   }
131   prev_min_iterations = cur_min_iterations;
134 color get_pixel_color( double c_re, double c_im )
136   double z_re = 0;
137   double z_im = 0;
139   int it;
140   for( it = 0; it < num_iterations; ++it )
141   {
142     double z_re_new = (z_re*z_re) - (z_im*z_im) + c_re;
143     z_im = (2*z_re*z_im)             + c_im;
144     z_re = z_re_new;
146     if( z_re < -2 || z_re > 2 ||
147         z_im < -2 || z_im > 2 )
148     {
149       break;
150     }
151   }
153   if( it == num_iterations )
154   {
155     return color( 0, 0, 0 );
156   }
157   else
158   {
159     if( it < cur_min_iterations )
160     {
161       cur_min_iterations = it;
162     }
164     int color_fade = (int)( 255.0 * ( (double)(num_iterations-it) / (double)(num_iterations-prev_min_iterations) ) );
165     return color( color_fade, color_fade, 255 );
166   }
169 boolean update_view()
171   double cOldMaxX = max_re;
172   double cOldMinX = min_re;
173   double cOldMaxY = max_im;
174   double cOldMinY = min_im;
176   double cWidthOver2  = cWidth / 2;
177   double cHeightOver2 = cHeight / 2;
179   cCentreX = min_re + ( cWidthOver2 );
180   cCentreY = min_im + ( cHeightOver2 );
182   double cDragX = ( (double)zDragX / (double)(zSizeX) ) * cWidth;
183   double cDragY = ( (double)zDragY / (double)(zSizeY) ) * cHeight;
185   cCentreX -= cDragX;
186   cCentreY -= cDragY;
188   double cZoom = 1.0 + ( 2.0 * ( (double)zZoomY/(double)zSizeY ) );
190   double cNewWidthOver2  = cWidthOver2 * cZoom;
191   double cNewHeightOver2 = cHeightOver2 * cZoom;
193   max_re = cCentreX + cNewWidthOver2;
194   min_re = cCentreX - cNewWidthOver2;
195   max_im = cCentreY + cNewHeightOver2;
196   min_im = cCentreY - cNewHeightOver2;
198   zDragX = 0;
199   zDragY = 0;
200   zZoomY = 0;
202   return ( cOldMaxX != max_re || cOldMinX != min_re || cOldMaxY != max_im || cOldMinY != min_im );