-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2d-transform.c
121 lines (102 loc) · 3.63 KB
/
2d-transform.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
#include <stdio.h>
#include <math.h>
#include <cairo.h>
#define f1(x,y) sin(x)
#define f2(x,y) y+sin(x)
#define dom_xmin -5
#define dom_xmax 5
#define dom_ymin -5
#define dom_ymax 5
#define codom_xmin -5
#define codom_xmax 5
#define codom_ymin -5
#define codom_ymax 5
#define gridlinegap 0.5
#define samplegap 0.1
#define tsenable 1
#define ts_xmin -2
#define ts_xmax 2
#define ts_ymin -2
#define ts_ymax 2
#define tsgap 0.4
#define ts_startsize 1
#define ts_endsize 3
#define ts_startr 255
#define ts_startg 0
#define ts_startb 0
#define ts_endr 0
#define ts_endg 0
#define ts_endb 255
#define pdfsize 100
#define outputfile "out.pdf"
cairo_surface_t *cairo_pdf_surface_create(const char *filename,
double width_in_points, double height_in_points);
cairo_surface_t *surface;
cairo_t *cr;
double pair[2];
void cartesian_to_cairo_coords(double x, double y) {
double tx = (x-codom_xmin)/(codom_xmax-codom_xmin);
double ty = (y-codom_ymax)/(codom_ymin-codom_ymax);
pair[0] = tx * pdfsize;
pair[1] = ty * pdfsize;
}
int main() {
surface = cairo_pdf_surface_create(outputfile, pdfsize, pdfsize);
cr = cairo_create(surface);
cairo_set_source_rgb(cr, 0, 0, 0);
cairo_set_line_width(cr, 0.1);
double xcur, ycur;
int n = dom_xmin / gridlinegap;
xcur = n * gridlinegap;
while (xcur <= dom_xmax) {
ycur = dom_ymin;
cartesian_to_cairo_coords(f1(xcur,ycur), f2(xcur,ycur));
cairo_move_to(cr, pair[0], pair[1]);
while (ycur <= dom_ymax) {
cartesian_to_cairo_coords(f1(xcur,ycur), f2(xcur,ycur));
cairo_line_to(cr, pair[0], pair[1]);
ycur += samplegap;
}
cairo_stroke(cr);
xcur += gridlinegap;
}
n = dom_ymin / gridlinegap;
ycur = n * gridlinegap;
while (ycur <= dom_ymax) {
xcur = dom_xmin;
cartesian_to_cairo_coords(f1(xcur,ycur), f2(xcur,ycur));
cairo_move_to(cr, pair[0], pair[1]);
while (xcur <= dom_xmax) {
cartesian_to_cairo_coords(f1(xcur,ycur), f2(xcur,ycur));
cairo_line_to(cr, pair[0], pair[1]);
xcur += samplegap;
}
cairo_stroke(cr);
ycur += gridlinegap;
}
if (tsenable) {
double curx, cury;
double tx, ty, r, g, b, size;
curx = ts_xmin;
while (curx <= ts_xmax) {
cury = ts_ymin;
while (cury <= ts_ymax) {
tx = (curx-ts_xmin)/(ts_xmax-ts_xmin);
ty = (cury-ts_ymin)/(ts_ymax-ts_ymin);
r = ((1-ty)*ts_startr + ty*ts_endr) / 255;
g = ((1-ty)*ts_startg + ty*ts_endg) / 255;
b = ((1-ty)*ts_startb + ty*ts_endb) / 255;
size = (1-tx)*ts_startsize + tx*ts_endsize;
cairo_set_source_rgba(cr, r, g, b, 0.5);
cartesian_to_cairo_coords(f1(curx,cury), f2(curx, cury));
cairo_arc(cr, pair[0], pair[1], size, 0, 2*M_PI);
cairo_fill(cr);
cury += tsgap;
}
curx += tsgap;
}
}
cairo_destroy(cr);
cairo_surface_destroy(surface);
return 0;
}