-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcairo.c
77 lines (59 loc) · 1.56 KB
/
cairo.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
/*
cairo.c
Using the Cairo image manipulation library:
https://www.cairographics.org/documentation/
On Ubuntu, might require the package "libcairo2-dev"
Compile with:
gcc -o cairo cairo.c `pkg-config --cflags --libs cairo` -lm
*/
#include <stdio.h>
#include <stdlib.h>
#include <cairo.h>
#include <complex.h>
void mand(cairo_t *, cairo_surface_t *, int, int, double complex, double, int);
int level(double complex, int);
int main(int argc, char **argv) {
cairo_t *cr;
cairo_surface_t *surface;
int w, h;
w = 1000; h = 800;
surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
cr = cairo_create(surface);
mand(cr, surface, w, h, -2.5 + 1.5*I, 3.0, 60);
cairo_surface_write_to_png(surface, "test9.png");
cairo_destroy(cr);
cairo_surface_destroy(surface);
}
void mand(cairo_t *cr, cairo_surface_t *surface, int w, int h, double complex tl, double i_range, int max_its) {
int i, j;
double complex c;
double res;
int lev;
char col_str[80];
res = i_range/h;
for (i=0;i<w;i++) {
for (j=0;j<h;j++) {
c = tl + i*res - j*res*I;
lev = level(c, max_its);
if (lev == -1) { // in the set
cairo_set_source_rgb(cr, 0.0, 0.0, 0.1);
} else { // not in the set
cairo_set_source_rgb(cr, (double) lev/max_its, 0.0, 0.0);
}
cairo_rectangle(cr, i, j, 1, 1);
cairo_fill(cr);
}
}
}
int level(double complex c, int max_its) {
double complex z;
int i;
z = 0.0 + 0.0*I;
for (i=0;i<max_its;i++) {
z = z*z + c;
if (cabs(z) > 2.0) // not in the set
return(i);
}
return(-1);
}
// eof