view src/Julia.cpp @ 2:0684158d38a8

Before cleanup. Still has experimental code in it.
author Eris Caffee <discordia@eldalin.com>
date Fri, 22 Oct 2010 02:21:52 -0500
parents 455406f5f021
children 0f4ad525f49a
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 <cstdlib>
13 #include <iostream>
14 #include <string>
15 #include <sstream>
17 #include "Julia.h"
18 #include "Options.h"
20 ////////////////////////////////////////////////////////////////////////////////
22 Julia::Julia() :
23 k_re (-0.565072),
24 k_im ( 0.491657)
25 {
26 re_min = -2.0;
27 im_max = 2.0;
28 size = 4.0;
29 max_iter = 50;
30 set_name();
31 calc_needed = true;
32 }
35 ////////////////////////////////////////////////////////////////////////////////
37 bool Julia::calc_set()
38 {
39 if (!surface) return false;
41 // Code from http://warp.povusers.org/Julia/
43 if (calc_needed)
44 {
45 double re_scale = (size)/(Options::width-1);
46 double im_scale = (size)/(Options::height-1);
48 int r, g, b;
50 Uint32 black = SDL_MapRGB(surface->format, 0, 0, 0);
51 Uint32 color;
52 int halfway = max_iter/2 - 1;
53 float step = (float) 255 / halfway;
55 unsigned x, y, n;
57 for(y = 0; y < Options::height; ++y)
58 {
59 double c_im = im_max - y * im_scale;
60 for(x = 0; x < Options::width; ++x)
61 {
62 double c_re = re_min + x * re_scale;
64 double z_re = c_re, z_im = c_im;
65 bool in_set = true;
66 for(n = 0; n < max_iter; ++n)
67 {
68 double z_re2 = z_re * z_re, z_im2 = z_im * z_im;
69 if(z_re2 + z_im2 > 4)
70 {
71 in_set = false;
72 break;
73 }
74 z_im = 2*z_re * z_im + k_im;
75 z_re = z_re2 - z_im2 + k_re;
76 }
77 if(in_set)
78 {
79 draw_pixel(x, y, &black);
80 }
81 else
82 {
83 if (n <= halfway)
84 {
85 r = n * step;
86 b = g = 0;
87 }
88 else
89 {
90 r = 255;
91 b = g = (n - halfway) * step;
92 }
93 color = SDL_MapRGB(surface->format, r, g, b);
94 draw_pixel(x, y, &color);
95 }
96 }
97 }
98 }
100 calc_needed = false;
101 return true;
102 }
105 ////////////////////////////////////////////////////////////////////////////////
106 bool Julia::init(SDL_Surface * surf)
107 {
108 if (!surf)
109 {
110 return false;
111 }
112 else
113 {
114 surface = surf;
115 }
116 calc_needed = true;
117 return true;
118 }
121 ////////////////////////////////////////////////////////////////////////////////
122 void Julia::get_k(double & re, double & im)
123 {
124 re = k_re;
125 im = k_im;
126 }
129 ////////////////////////////////////////////////////////////////////////////////
130 void Julia::set_k(double re, double im)
131 {
132 k_re = re;
133 k_im = im;
134 set_name();
135 }
138 ////////////////////////////////////////////////////////////////////////////////
140 void Julia::draw_pixel(int x, int y, Uint32 * color)
141 {
142 Fractal::draw_pixel(x, y, color);
143 }
146 ////////////////////////////////////////////////////////////////////////////////
147 void Julia::set_name()
148 {
149 std::stringstream ss;
150 ss <<"Julia set K = (" << k_re << "," << k_im << ")";
151 name = ss.str();
152 }