view src/Julia.cpp @ 5:d691ce98f406

OK. This is the first release. Honest.
author Eris Caffee <discordia@eldalin.com>
date Fri, 12 Nov 2010 23:58:48 -0600
parents df02a7de7fe2
children
line source
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Fracter - A simple Mandelbrot Set viewer.
4 //
5 // Copyright (C) 2010 Sarah Eris Horsley Caffee
6 //
7 // This file is part of Fracter.
8 //
9 // Fracter is free software: you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation, either version 3 of the License, or
12 // (at your option) any later version.
13 //
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with this program. If not, see <http://www.gnu.org/licenses/>.
21 //
22 //
23 // Julia.cpp
24 // Julia set calculator class implementation
25 //
26 ////////////////////////////////////////////////////////////////////////////////
28 #include <cmath>
29 #include <sstream>
30 #include <typeinfo>
32 #include "Julia.h"
35 ////////////////////////////////////////////////////////////////////////////////
36 ViewJulia::ViewJulia(std::string t,
37 double re, double im, double s,
38 unsigned int max,
39 double kr, double ki) :
40 View(t, re, im, s, max),
41 k_re (kr),
42 k_im (ki)
43 {
44 }
46 ////////////////////////////////////////////////////////////////////////////////
48 Julia::Julia() :
49 k_re (-0.565072),
50 k_im ( 0.491657)
51 {
52 re_min = -2.0;
53 im_max = 2.0;
54 size = 4.0;
55 max_iter = 50;
56 calc_needed = true;
57 fractal_name = "Julia set";
58 set_display_name();
59 }
62 ////////////////////////////////////////////////////////////////////////////////
64 bool Julia::calc_set()
65 {
66 if (!surface) return false;
68 // See http://warp.povusers.org/Julia/ for explanation of algorithm.
70 if (calc_needed)
71 {
72 double re_scale = (size)/(surface->w-1);
73 double im_scale = (size)/(surface->h-1);
75 int r, g, b;
77 Uint32 black = SDL_MapRGB(surface->format, 0, 0, 0);
78 Uint32 color;
79 int halfway = max_iter/2 - 1;
80 float step = (float) 255 / halfway;
82 int x, y, n;
84 for(y = 0; y < surface->h; ++y)
85 {
86 double c_im = im_max - y * im_scale;
87 for(x = 0; x < surface->w; ++x)
88 {
89 double c_re = re_min + x * re_scale;
91 double z_re = c_re, z_im = c_im;
92 bool in_set = true;
93 for(n = 0; n < (int) max_iter; ++n)
94 {
95 double z_re2 = z_re * z_re, z_im2 = z_im * z_im;
96 if(z_re2 + z_im2 > 4)
97 {
98 in_set = false;
99 break;
100 }
101 z_im = 2*z_re * z_im + k_im;
102 z_re = z_re2 - z_im2 + k_re;
103 }
104 if(in_set)
105 {
106 draw_pixel(x, y, &black);
107 }
108 else
109 {
110 if (n <= halfway)
111 {
112 r = n * step;
113 b = g = 0;
114 }
115 else
116 {
117 r = 255;
118 b = g = (n - halfway) * step;
119 }
120 color = SDL_MapRGB(surface->format, r, g, b);
121 draw_pixel(x, y, &color);
122 }
123 }
124 }
125 }
127 calc_needed = false;
128 return true;
129 }
132 ////////////////////////////////////////////////////////////////////////////////
133 bool Julia::get_option(std::string option, void * value) const
134 {
135 if (option.compare("k") == 0)
136 {
137 ((double *)value)[0] = k_re;
138 ((double *)value)[0] = k_im;
139 }
140 else
141 {
142 return false;
143 }
145 return true;
146 }
149 ////////////////////////////////////////////////////////////////////////////////
150 bool Julia::set_option(std::string option, void * value)
151 {
152 if (option.compare("k") == 0)
153 {
154 k_re = ((double *)value)[0];
155 k_im = ((double *)value)[1];
156 set_display_name();
157 }
158 else
159 {
160 return false;
161 }
163 return true;
164 }
167 ////////////////////////////////////////////////////////////////////////////////
168 ViewJulia * Julia::save_view()
169 {
170 return new ViewJulia(typeid(*this).name(), re_min, im_max, size, max_iter,
171 k_re, k_im);
172 }
174 ////////////////////////////////////////////////////////////////////////////////
175 void Julia::restore_view(View * v)
176 {
177 Fractal::restore_view(v);
178 k_re = static_cast<ViewJulia *>(v)->k_re;
179 k_im = static_cast<ViewJulia *>(v)->k_im;
180 set_display_name();
181 }
183 ////////////////////////////////////////////////////////////////////////////////
184 void Julia::set_display_name()
185 {
186 std::stringstream ss;
187 ss << display_name << " K = (" << k_re << "," << k_im << ")";
188 display_name = ss.str();
189 }