Initial commit
[simple-window.git] / example.cc
1
2 ///////////////////////////////////////////////////////////////////////////
3 // This program is free software: you can redistribute it and/or modify  //
4 // it under the terms of the version 3 of the GNU General Public License //
5 // as published by the Free Software Foundation.                         //
6 //                                                                       //
7 // This program is distributed in the hope that it will be useful, but   //
8 // WITHOUT ANY WARRANTY; without even the implied warranty of            //
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      //
10 // General Public License for more details.                              //
11 //                                                                       //
12 // You should have received a copy of the GNU General Public License     //
13 // along with this program. If not, see <http://www.gnu.org/licenses/>.  //
14 //                                                                       //
15 // Written by and Copyright (C) Francois Fleuret                         //
16 // Contact <francois.fleuret@idiap.ch> for comments & bug reports        //
17 ///////////////////////////////////////////////////////////////////////////
18
19 #include <iostream>
20
21 #include "simple_window.h"
22
23 using namespace std;
24
25 int main() {
26   SimpleWindow window("Mandelbrot", 600, 600);
27   int n;
28   int depth = 100;
29
30   const int width = window.get_width();
31   const int height = window.get_height();
32
33   window.map();
34
35   for(int i = 0; i < width; i++)
36     for(int j = 0; j < height; j++) {
37       double cr = -2.0 + 3.0 * (double(i)/double(width));
38       double ci = -1.5 + 3.0 * (double(j)/double(height));
39       double zr = 0, zi = 0;
40       for(n = 0; (n < depth) && (zr*zr + zi*zi < 100); n++) {
41         double t = zr*zr -zi*zi + cr;
42         zi = 2*zr*zi + ci;
43         zr = t;
44       }
45
46       float a = float(n)/float(depth);
47
48       if(a < 0.5) window.color(0, 0, a*2);
49       else        window.color((a-0.5)*2, (a-0.5)*2, 1.0);
50
51       window.draw_point(i, j);
52     }
53
54   window.show();
55
56   cout << "Press [ENTER] to close the window.\n";
57
58   cin.get();
59 }