5 s - screen position (unzoomed)
6 z - screen position (zoomed)
7 c - co-ordinate on the complex plane
13 int initial_zoom = 16;
14 int num_iterations = 50;
19 double prev_min_iterations = 200;
21 int zoom = initial_zoom;
24 double cur_min_iterations;
34 boolean anything_changed = true;
36 boolean drawing = false;
40 size( zSizeX, zSizeY );
41 do_update_pixels( false );
47 cWidth = max_re - min_re;
48 cHeight = max_im - min_im;
50 if( anything_changed )
52 cur_min_iterations = num_iterations;
54 mult_x = cWidth / (double)sSizeX;
55 mult_y = cHeight / (double)sSizeY;
57 do_update_pixels( true );
62 anything_changed = update_view();
67 if( mouseButton == CENTER )
69 println( cCentreX + " + " + cCentreY + "i" );
70 println( "width = " + (max_re - min_re) );
82 if( mouseButton == RIGHT )
84 zZoomY += mouseY - pmouseY;
88 zDragX += mouseX - pmouseX;
89 zDragY += mouseY - pmouseY;
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 )
113 for( int x = 0; x < sSizeX; ++x )
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 );
125 rect( zX, zY, zoom - 1, zoom - 1 );
126 //point( xzoom, yzoom );
131 prev_min_iterations = cur_min_iterations;
134 color get_pixel_color( double c_re, double c_im )
140 for( it = 0; it < num_iterations; ++it )
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;
146 if( z_re < -2 || z_re > 2 ||
147 z_im < -2 || z_im > 2 )
153 if( it == num_iterations )
155 return color( 0, 0, 0 );
159 if( it < cur_min_iterations )
161 cur_min_iterations = it;
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 );
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;
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;
202 return ( cOldMaxX != max_re || cOldMinX != min_re || cOldMaxY != max_im || cOldMinY != min_im );