view src/Julia.cpp @ 3:0f4ad525f49a

Lot's of cleanup. Applied AbstractFactory to class Fractal and derivatives. Decoupled most objects. Removed remaining standard C function calls. Improved old_views handling.
author Eris Caffee <discordia@eldalin.com>
date Sat, 23 Oct 2010 04:24:48 -0500
parents 0684158d38a8
children df02a7de7fe2
line source
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) 2010 Sarah Eris Horsley Caffee
4 //
5 // mandelbrot - A simple Mandelbrot Set viewer.
6 //
7 // Julia.cpp
8 // Julia set calculator class implementation
9 //
10 ////////////////////////////////////////////////////////////////////////////////
12 #include <string>
13 #include <sstream>
15 #include "Julia.h"
18 ////////////////////////////////////////////////////////////////////////////////
20 Julia::Julia() :
21 k_re (-0.565072),
22 k_im ( 0.491657)
23 {
24 type_name = JULIA_TYPE_NAME;
25 re_min = -2.0;
26 im_max = 2.0;
27 size = 4.0;
28 max_iter = 50;
29 calc_needed = true;
30 set_display_name();
31 }
34 ////////////////////////////////////////////////////////////////////////////////
36 bool Julia::calc_set()
37 {
38 if (!surface) return false;
40 // Code from http://warp.povusers.org/Julia/
42 if (calc_needed)
43 {
44 double re_scale = (size)/(surface->w-1);
45 double im_scale = (size)/(surface->h-1);
47 int r, g, b;
49 Uint32 black = SDL_MapRGB(surface->format, 0, 0, 0);
50 Uint32 color;
51 int halfway = max_iter/2 - 1;
52 float step = (float) 255 / halfway;
54 unsigned x, y, n;
56 for(y = 0; y < surface->h; ++y)
57 {
58 double c_im = im_max - y * im_scale;
59 for(x = 0; x < surface->w; ++x)
60 {
61 double c_re = re_min + x * re_scale;
63 double z_re = c_re, z_im = c_im;
64 bool in_set = true;
65 for(n = 0; n < max_iter; ++n)
66 {
67 double z_re2 = z_re * z_re, z_im2 = z_im * z_im;
68 if(z_re2 + z_im2 > 4)
69 {
70 in_set = false;
71 break;
72 }
73 z_im = 2*z_re * z_im + k_im;
74 z_re = z_re2 - z_im2 + k_re;
75 }
76 if(in_set)
77 {
78 draw_pixel(x, y, &black);
79 }
80 else
81 {
82 if (n <= halfway)
83 {
84 r = n * step;
85 b = g = 0;
86 }
87 else
88 {
89 r = 255;
90 b = g = (n - halfway) * step;
91 }
92 color = SDL_MapRGB(surface->format, r, g, b);
93 draw_pixel(x, y, &color);
94 }
95 }
96 }
97 }
99 calc_needed = false;
100 return true;
101 }
104 ////////////////////////////////////////////////////////////////////////////////
105 bool Julia::init(SDL_Surface * surf)
106 {
107 if (!surf)
108 {
109 return false;
110 }
111 else
112 {
113 surface = surf;
114 }
115 calc_needed = true;
116 return true;
117 }
120 ////////////////////////////////////////////////////////////////////////////////
121 bool Julia::get_option(std::string option, void * value)
122 {
123 if (option.compare("k") == 0)
124 {
125 ((double *)value)[0] = k_re;
126 ((double *)value)[0] = k_im;
127 }
128 else
129 {
130 return false;
131 }
133 return true;
134 }
137 ////////////////////////////////////////////////////////////////////////////////
138 bool Julia::set_option(std::string option, void * value)
139 {
140 if (option.compare("k") == 0)
141 {
142 k_re = ((double *)value)[0];
143 k_im = ((double *)value)[1];
144 set_display_name();
145 }
146 else
147 {
148 return false;
149 }
152 return true;
153 }
156 ////////////////////////////////////////////////////////////////////////////////
157 const char * Julia::get_type_name()
158 {
159 return JULIA_TYPE_NAME;
160 }
163 ////////////////////////////////////////////////////////////////////////////////
165 void Julia::draw_pixel(int x, int y, Uint32 * color)
166 {
167 Fractal::draw_pixel(x, y, color);
168 }
171 ////////////////////////////////////////////////////////////////////////////////
172 void Julia::set_display_name()
173 {
174 std::stringstream ss;
175 ss << "Julia set K = (" << k_re << "," << k_im << ")";
176 display_name = ss.str();
177 }