view src/Mandelbrot.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 // Mandelbrot.cpp
24 // Mandelbrot set calculator class implementation
25 //
26 ////////////////////////////////////////////////////////////////////////////////
28 #include "Mandelbrot.h"
29 #include <typeinfo>
31 ////////////////////////////////////////////////////////////////////////////////
32 ViewMandelbrot::ViewMandelbrot(std::string t,
33 double re, double im, double s,
34 unsigned int max) :
35 View(t, re, im, s, max)
36 {
37 }
39 ////////////////////////////////////////////////////////////////////////////////
41 Mandelbrot::Mandelbrot()
42 {
43 re_min = -2.5;
44 im_max = 2.5;
45 size = 5.0;
46 max_iter = 50;
47 calc_needed = true;
48 fractal_name = "Mandelbrot set";
49 display_name = fractal_name;
50 }
53 ////////////////////////////////////////////////////////////////////////////////
55 bool Mandelbrot::calc_set()
56 {
57 if (!surface) return false;
59 // See http://warp.povusers.org/Mandelbrot/ for explanation of the algorithm.
61 if (calc_needed)
62 {
63 double re_scale = (size)/(surface->w-1);
64 double im_scale = (size)/(surface->h-1);
66 int r, g, b;
68 Uint32 black = SDL_MapRGB(surface->format, 0, 0, 0);
69 Uint32 color;
70 int halfway = max_iter/2 - 1;
71 float step = (float) 255 / halfway;
73 int x, y, n;
75 for(y = 0; y < surface->h; ++y)
76 {
77 double c_im = im_max - y * im_scale;
78 for(x = 0; x < surface->w; ++x)
79 {
80 double c_re = re_min + x * re_scale;
82 double z_re = c_re, z_im = c_im;
83 bool in_set = true;
84 for(n=0; n < (int) max_iter; ++n)
85 {
86 double z_re2 = z_re * z_re, z_im2 = z_im * z_im;
87 if(z_re2 + z_im2 > 4)
88 {
89 in_set = false;
90 break;
91 }
92 z_im = 2 * z_re * z_im + c_im;
93 z_re = z_re2 - z_im2 + c_re;
94 }
95 if(in_set)
96 {
97 draw_pixel(x, y, &black);
98 }
99 else
100 {
101 if (n <= halfway)
102 {
103 b = n * step;
104 r = g = 0;
105 }
106 else
107 {
108 b = 255;
109 r = g = (n - halfway) * step;
110 }
111 color = SDL_MapRGB(surface->format, r, g, b);
112 draw_pixel(x, y, &color);
113 }
114 }
115 }
116 }
118 calc_needed = false;
119 return true;
120 }
123 ////////////////////////////////////////////////////////////////////////////////
124 ViewMandelbrot * Mandelbrot::save_view()
125 {
126 return new ViewMandelbrot(typeid(*this).name(), re_min, im_max, size,
127 max_iter);
128 }
130 ////////////////////////////////////////////////////////////////////////////////
131 void Mandelbrot::restore_view(View * v)
132 {
133 Fractal::restore_view(v);
134 }