-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
207 lines (145 loc) · 3.7 KB
/
main.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
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
201
202
203
204
205
206
#include "plot.h"
#include <cairo.h>
#define SX 800
#define SY 800
#include <math.h>
t_plot_series s1;
double xs1[100];
double ys1[100];
void series1( t_plot * plot ) {
init_series( &s1);
for( int i = 0; i<100; i++ ) {
xs1[i] = -1.0 + i* 2.0/99;
ys1[i] = 3*xs1[i]*xs1[i];
}
s1.x = xs1;
s1.y = ys1;
s1.np = 100;
s1.lineStyle = DASHED;
add_series( &s1, & plot -> data);
}
t_plot_series s2;
double xs2[50];
double ys2[50];
void series2( t_plot * plot ) {
init_series( &s2);
for( int i = 0; i<50; i++ ) {
xs2[i] = -1.0 + i* 2.0/49;
ys2[i] = 0.5*(xs2[i] + 1.0);
}
s2.x = xs2;
s2.y = ys2;
s2.np = 50;
s2.lineColor = (t_color) {.r=0.1,.g=0.7,.b=.1,.a=0.8};
s2.symbolStyle = CIRCLE;
s2.symbolColor = (t_color) {.r=0.1,.g=0.7,.b=.1,.a=1.0};
add_series( &s2, & plot -> data);
}
t_plot_colormap cm;
double z[128*128];
void colormap( t_plot * plot ) {
init_colormap( &cm );
for( int j = 0; j < 128; j++ ) {
double y = (2 * M_PI * j)/64;
for( int i = 0; i < 128; i++ ) {
double x = (2 * M_PI * i)/64;
z[j*128 + i] = sin(x) * sin(y);
}
}
cm.data = z;
cm.xdim = 128;
cm.ydim = 128;
cm.x0 = -1;
cm.x1 = +1;
cm.y0 = 0;
cm.y1 = +1;
cm.coloraxis.axis.min = -1;
cm.coloraxis.axis.max = +1;
cm.coloraxis.palette.id = THERMAL;
cm.opacityType = OPACITY_LINEAR_ABS;
cm.opacityRange[0] = 0;
cm.opacityRange[1] = 0.4;
add_colormap( &cm, &plot -> data);
}
t_plot_polygon poly;
t_pointlist pos;
void polygon( t_plot * plot ) {
init_polygon( &poly );
add_pointlist( &pos, -1.0, 0.0 );
add_pointlist( &pos, +1.0, 0.0 );
add_pointlist( &pos, 0.0, 1.0 );
poly.pos = &pos;
poly.closed = 1;
poly.style = 3;
poly.fillColor = (t_color) {.r=0.8,.g=0.2,.b=0.1,.a=0.6};
add_polygon( &poly, & plot -> data);
}
t_plot_contour ctr;
double ctr_values[16];
double z2[64*64];
void contour( t_plot * plot ) {
for( int j = 0; j < 64; j++ ) {
double y = j/63.0;
for( int i = 0; i < 64; i++ ) {
double x = -1 + 2 * i/63.0;
z2[j*64 + i] = y*y+x*x;
}
}
// Draw a colormap below for reference
init_colormap( &cm );
cm.data = z2;
cm.xdim = 64;
cm.ydim = 64;
cm.x0 = -1;
cm.x1 = +1;
cm.y0 = 0;
cm.y1 = +1;
cm.coloraxis.axis.min = 0;
cm.coloraxis.axis.max = +1;
cm.coloraxis.palette.id = HALINE;
add_colormap( &cm, &plot -> data);
for( int i = 0; i < 16; i++ ) {
ctr_values[i] = (i+1)*2.0/17;
}
init_contour( &ctr );
ctr.data = z2;
ctr.xdim = 64;
ctr.ydim = 64;
ctr.x0 = -1;
ctr.x1 = +1;
ctr.y0 = 0;
ctr.y1 = +1;
ctr.values = ctr_values;
ctr.nvalues = 16;
add_contour( &ctr, & plot -> data);
}
int main (int argc, char *argv[])
{
t_plot plot;
init_plot( &plot );
// Plot title
plot.title.text = "Sample";
plot.axis[0].min = -1.1;
plot.axis[0].max = 1.1;
plot.axis[1].min = -0.1;
plot.axis[1].max = 1.1;
// Plot data
//series1( &plot );
//colormap( &plot );
//series2( &plot );
// polygon( &plot );
contour( &plot );
// Create surface
cairo_surface_t *surface =
cairo_image_surface_create(CAIRO_FORMAT_ARGB32, SX, SY);
// Create context
cairo_t *cr = cairo_create (surface);
// This sets a 1.0 x 1.0 coordinate space for the context
cairo_scale (cr, SX, SY);
// Draw on context
draw_plot( &plot, cr );
// Write output and clean up
cairo_surface_write_to_png (surface, "plot.png");
cairo_destroy (cr);
cairo_surface_destroy (surface);
}