-
Notifications
You must be signed in to change notification settings - Fork 0
/
ray.c
134 lines (105 loc) · 2.61 KB
/
ray.c
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
* plot ray vs. theta
*/
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
void
ray (double *bin, int nbin)
{
double re, im;
double dre, dim;
double phi;
double escape_radius, esq;
int i, j, loop, itermax;
int nx, ny;
double re_c, im_c;
double delta_re, delta_im;
double tmp, modulus;
int *cnt;
int ibin;
double u,v, r;
nx = ny = 1200;
itermax = 400000;
escape_radius = 1e10;
delta_re = 3.0/((double) nx);
delta_im = 3.0/((double) ny);
esq = escape_radius*escape_radius;
// malloc a local counter array
cnt = (int *) malloc (nbin * sizeof (int));
// initialize the bins
for (i=0; i<nbin; i++){
bin[i] = 0.0;
cnt[i] = -1;
}
// walk over pixel grid
re_c = -2.1;
for (i=0; i<nx; i++)
{
if (0 == i%10) printf ("# doing row %d\n", i);
im_c = -1.5;
for (j=0; j<ny; j++)
{
// if we are inside the cardiod, then don't even bother
// this saves a significant amount of cpu
re = 0.25 - re_c;
im = -im_c;
u = sqrt (0.5*(re + sqrt (re*re + im*im)));
v = 0.5 * im / u;
u = 0.5 - u;
r = sqrt (u*u + v*v);
if (0.5 > r) {
im_c += delta_im;
continue;
}
// initialize the iterators
re = 0.0;
im = 0.0;
dre = 0.0;
dim = 0.0;
// iterate until escape
for (loop=0; loop<itermax; loop++) {
tmp = 2.0 * (re*dre - im*dim) + 1.0;
dim = 2.0 * (re*dim + im*dre);
dre = tmp;
tmp = re*re - im*im + re_c;
im = 2.0 * re*im + im_c;
re = tmp;
modulus = re*re + im*im;
if (modulus > esq) break;
}
// do the math only is this point escaped
if (modulus > esq) {
phi = atan2 (im, re);
phi /= 2.0 * M_PI;
phi += 0.5;
phi *= nbin;
ibin = (int) phi;
if (ibin == nbin) ibin = 0;
// update only if we got closer to the edge
if (loop > cnt[ibin]) {
cnt[ibin] = loop;
bin[ibin] = atan2 (dim, dre) / (2.0*M_PI) + 0.5;
}
}
im_c += delta_im;
}
re_c += delta_re;
}
free (cnt);
}
int
main (int argc, char *argv[])
{
double *map;
int i, nbin;
double phi;
nbin = 1200;
map = (double *) malloc (nbin * sizeof(double));
ray (map, nbin);
for (i=0; i<nbin; i++) {
phi = ((double) i) / ((double) nbin);
printf ("%d %f %f\n", i, phi, map[i]);
}
return 0;
}