jeremybanks (owner)

Revisions

gist: 156359 Download_button fork
public
Public Clone URL: git://gist.github.com/156359.git
Embed All Files: show embed
fractals.cpp #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
 
struct complex {
  double r;
  double i;
  complex(double r_, double i_): r(r_), i(i_) {}
  complex(double r_) : r(r_), i(0) {}
};
 
complex operator+ (complex a, complex b) {
  return complex(a.r + b.r, a.i + b.i);
}
 
complex operator* (complex a, complex b) {
  return complex(a.r * b.r - a.i * b.i,
a.i * b.r + a.r * b.i);
}
 
bool operator> (complex a, double b) {
  return (a.r * a.r + a.i + a.i) > (b * b);
}
 
int escapeTime(complex c, int limit) {
  complex z (0, 0);
  for(int i = limit; i > 0; --i) {
    z = z * z + c;
    if(z > 2) return i;
  }
  return 0;
}
 
int main(int argc, char** argv) {
  using std::cout;
  using std::cin;
  using std::cerr;
  cerr<<"enter horizontal pixels: ";
  int width;
  cin>>width;
  cerr<<"enter vertical pixels: ";
  int height;
  cin>>height;
  cerr<<"enter limit: ";
  int limit;
  cin>>limit;
  cerr<<"enter centre: ";
  double cr, ci;
  cin>>cr>>ci;
  complex offset (cr, ci);
  cerr<<"enter horizontal range: ";
  double hrange, vrange;
  cin>>hrange;
  cerr<<"enter vertical range: ";
  cin>>vrange;
  complex h = hrange/width;
  complex v (0, vrange/height);
  cout<<"P2\n"<<width<<" "<<height<<"\n"<<limit<<"\n";
  for(int i = 0; i < height; ++i) {
    for(int j = 0; j < width; ++j) {
      cout<<escapeTime(offset + v * (i-height/2) + h * (j-width/2), limit)<<" ";
    }
  }
  return 0;
}