forked from fltk/fltk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmandelbrot.cxx
200 lines (183 loc) · 4.66 KB
/
mandelbrot.cxx
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include "mandelbrot_ui.C"
#include <FL/fl_draw.H>
#include <stdio.h>
#include <stdlib.h>
Drawing_Window mbrot;
Drawing_Window jbrot;
void idle() {
if (!mbrot.d->idle() && !(jbrot.d && jbrot.d->idle())) Fl::set_idle(0);
}
void set_idle() {
Fl::set_idle(idle);
}
static void window_callback(Fl_Widget*, void*) {exit(0);}
int main(int argc, char **argv) {
make_window(mbrot);
mbrot.d->X = -.75;
mbrot.d->scale = 2.5;
mbrot.update_label();
int i = 0;
if (Fl::args(argc,argv,i) < argc) Fl::fatal(Fl::help);
Fl::visual(FL_RGB);
mbrot.window->callback(window_callback);
mbrot.window->show(argc,argv);
Fl::run();
return 0;
}
void Drawing_Window::update_label() {
char buffer[128];
sprintf(buffer, "%+.10f", d->X); x_input->value(buffer);
sprintf(buffer, "%+.10f", d->Y); y_input->value(buffer);
sprintf(buffer, "%.2g", d->scale); w_input->value(buffer);
}
void Drawing_Area::draw() {
draw_box();
drawn = 0;
set_idle();
}
int Drawing_Area::idle() {
if (!window()->visible()) return 0;
if (drawn < nextline) {
window()->make_current();
int yy = drawn+y()+4;
if (yy >= sy && yy <= sy+sh) erase_box();
fl_draw_image_mono(buffer+drawn*W,x()+3,yy,W,1,1,W);
drawn++;
return 1;
}
if (nextline < H) {
if (!buffer) buffer = new uchar[W*H];
double yy = Y+(H/2-nextline)*scale/W;
double yi = yy; if (julia) yy = jY;
uchar *p = buffer+nextline*W;
for (int xi = 0; xi < W; xi++) {
double xx = X+(xi-W/2)*scale/W;
double wx = xx; double wy = yi;
if (julia) xx = jX;
for (int i=0; ; i++) {
if (i >= iterations) {*p = 0; break;}
double t = wx*wx - wy*wy + xx;
wy = 2*wx*wy + yy;
wx = t;
if (wx*wx + wy*wy > 4) {
wx = t = 1-double(i)/(1<<10);
if (t <= 0) t = 0; else for (i=brightness; i--;) t*=wx;
*p = 255-int(254*t);
break;
}
}
p++;
}
nextline++;
return nextline < H;
}
return 0;
}
void Drawing_Area::erase_box() {
window()->make_current();
fl_overlay_clear();
}
int Drawing_Area::handle(int event) {
static int ix, iy;
static int dragged;
static int button;
int x2,y2;
switch (event) {
case FL_PUSH:
erase_box();
ix = Fl::event_x(); if (ix<x()) ix=x(); if (ix>=x()+w()) ix=x()+w()-1;
iy = Fl::event_y(); if (iy<y()) iy=y(); if (iy>=y()+h()) iy=y()+h()-1;
dragged = 0;
button = Fl::event_button();
return 1;
case FL_DRAG:
dragged = 1;
erase_box();
x2 = Fl::event_x(); if (x2<x()) x2=x(); if (x2>=x()+w()) x2=x()+w()-1;
y2 = Fl::event_y(); if (y2<y()) y2=y(); if (y2>=y()+h()) y2=y()+h()-1;
if (button != 1) {ix = x2; iy = y2; return 1;}
if (ix < x2) {sx = ix; sw = x2-ix;} else {sx = x2; sw = ix-x2;}
if (iy < y2) {sy = iy; sh = y2-iy;} else {sy = y2; sh = iy-y2;}
window()->make_current();
fl_overlay_rect(sx,sy,sw,sh);
return 1;
case FL_RELEASE:
if (button == 1) {
erase_box();
if (dragged && sw > 3 && sh > 3) {
X = X + (sx+sw/2-x()-W/2)*scale/W;
Y = Y + (-sy-sh/2+y()+H/2)*scale/W;
scale = sw*scale/W;
} else if (!dragged) {
scale = 2*scale;
if (julia) {
if (scale >= 4) {
scale = 4;
X = Y = 0;
}
} else {
if (scale >= 2.5) {
scale = 2.5;
X = -.75;
Y = 0;
}
}
} else return 1;
((Drawing_Window*)(user_data()))->update_label();
new_display();
} else if (!julia) {
if (!jbrot.d) {
make_window(jbrot);
jbrot.d->julia = 1;
jbrot.d->X = 0;
jbrot.d->Y = 0;
jbrot.d->scale = 4;
jbrot.update_label();
}
jbrot.d->jX = X + (ix-x()-W/2)*scale/W;
jbrot.d->jY = Y + (H/2-iy+y())*scale/W;
static char buffer[128];
sprintf(buffer, "Julia %.7f %.7f",jbrot.d->jX,jbrot.d->jY);
jbrot.window->label(buffer);
jbrot.window->show();
jbrot.d->new_display();
}
return 1;
}
return 0;
}
void Drawing_Area::new_display() {
drawn = nextline = 0;
set_idle();
}
void Drawing_Area::resize(int X,int Y,int W,int H) {
if (W != w() || H != h()) {
this->W = W-6;
this->H = H-8;
if (buffer) {delete[] buffer; buffer = 0; new_display();}
}
Fl_Box::resize(X,Y,W,H);
}
void brightness_slider_cb(Fl_Slider* o, Drawing_Window* s) {
s->d->brightness = int(o->value());
s->d->new_display();
}
void iterations_slider_cb(Fl_Slider* o, Drawing_Window* s) {
s->d->iterations = 1<<int(o->value());
s->d->new_display();
}
void x_callback(Fl_Input* o, Drawing_Window* s) {
double v = atof(o->value());
s->d->X = v;
s->d->new_display();
}
void y_callback(Fl_Input* o, Drawing_Window* s) {
double v = atof(o->value());
s->d->Y = v;
s->d->new_display();
}
void w_callback(Fl_Input* o, Drawing_Window* s) {
double v = atof(o->value());
s->d->scale = v;
s->d->new_display();
}