Repository navigation
Expand file tree
/
Copy pathlines.c
More file actions
204 lines (191 loc) · 6.01 KB
/
Copy pathlines.c
File metadata and controls
204 lines (191 loc) · 6.01 KB
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
#include "lines.h"
line new_line(point start, point end) {
line l = {start, end};
return l;
}
double line_slope(line l) {
return (double) (l.end.y - l.start.y) / (l.end.x - l.start.x);
}
/* Naive line drawing algorithm: assumes line in 1st octant */
point_table naive(line l) {
double m = line_slope(l);
point_table tbl = {NULL, 0};
tbl.num_elems = abs( l.end.x - l.start.x ) + 1;
tbl.data = calloc(tbl.num_elems, sizeof *(tbl.data));
if (tbl.data != NULL) {
int x, nt;
double b = (double) l.start.y - ((double) l.start.x * m);
(tbl.data)[0] = l.start;
(tbl.data)[tbl.num_elems - 1] = l.end;
for (x = l.start.x + 1, nt = 1; nt < tbl.num_elems - 1; x++, nt++)
(tbl.data)[nt] = new_point( x, (int) ( m*x + b ) );
}
return tbl;
}
/* DDA line drawing algorithm: assumes line in 1st octant */
point_table dda(line l) {
double m = line_slope(l);
point_table tbl = {NULL, 0};
tbl.num_elems = abs( l.end.x - l.start.x ) + 1;
tbl.data = calloc(tbl.num_elems, sizeof *(tbl.data));
if (tbl.data != NULL) {
int x, nt;
double ny = (double) l.start.y;
(tbl.data)[0] = l.start;
(tbl.data)[tbl.num_elems - 1] = l.end;
for (x = l.start.x + 1, nt = 1; nt < tbl.num_elems - 1; x++, nt++) {
ny += m;
(tbl.data)[nt] = new_point( x, (int) ny );
}
}
return tbl;
}
/* Bresenham line drawing algorithm: assumes line in 1st octant */
point_table bresenham(line l) {
point_table tbl = {NULL, 0};
/* parameters for Bresenham line drawing algorithm */
int dx = l.end.x - l.start.x;
int dy = l.end.y - l.start.y;
tbl.num_elems = abs( dx ) + 1;
/* initial values for: */
int d = 2 * dy - dx; /*<< the d value */
int incE = 2 * dy; /*<< the increment when in East direction */
int incNE = 2 * (dy - dx); /*<< the increment when in North East direction */
int x, y, nt;
tbl.data = calloc(tbl.num_elems, sizeof *(tbl.data));
if (tbl.data != NULL) {
(tbl.data)[0] = l.start;
(tbl.data)[tbl.num_elems - 1] = l.end;
for (x = l.start.x + 1, y = l.start.y, nt = 1; nt < tbl.num_elems - 1; x++, nt++) {
if (d <= 0) d += incE; /* East direction adjustement to the parameter 'd' */
else {
d += incNE; /* North East direction adjustment to the parameter 'd' */
++y; /* y increment */
}
(tbl.data)[nt] = new_point( x, y );
}
}
return tbl;
}
/* Generate special line */
point_table line_isspecial(line l, short int *ind) {
point_table tbl = {NULL, 0};
int dx = l.end.x - l.start.x;
int dy = l.end.y - l.start.y;
// Determine special type
short int sp = -1;
if ( points_equal(l.start, l.end) ) sp = 0; // Point
else if (l.start.y == l.end.y) sp = 1; // Horizontal
else if (l.start.x == l.end.x) sp = 2; // Vertical
else if (abs(dx) == abs(dy)) sp = 3; // Diagonal
// Is special?
if (sp != -1) {
// Adjust increments accordingly
int incy = (sp & 2) >> 1, incx = sp & 1;
if (incx != 0 && dx < 0) incx = -incx;
if (incy != 0 && dy < 0) incy = -incy;
// Allocate space for the table
tbl.num_elems = (!incx) ? abs(dy) : abs(dx);
++(tbl.num_elems);
tbl.data = calloc(tbl.num_elems, sizeof *(tbl.data));
// Allocated?
if (tbl.data != NULL) {
// Generate tabe of points
uint ne;
(tbl.data)[0] = l.start;
(tbl.data)[tbl.num_elems - 1] = l.end;
for (ne = 1; ne < tbl.num_elems - 1; ne++) {
(tbl.data)[ne].x = (tbl.data)[ne-1].x + incx;
(tbl.data)[ne].y = (tbl.data)[ne-1].y + incy;
}
} else sp = -2;
}
// Set final indicator
if (ind != NULL) *ind = sp;
return tbl;
}
/* Move given line to first octant */
short int line_firstoct(line *l) {
// Get line slope
int dx = l->end.x - l->start.x;
int dy = l->end.y - l->start.y;
double m = (double) dy / dx;
// Determine transform actions to move line to 1st octant
short int transf = 0;
if (dy < 0) transf |= 4;
if ( ((dy < 0) && !(m < 0)) || (!(dy < 0) && (m < 0)) ) transf |= 2;
if ( (m < -1) || (m > 1) ) transf |= 1;
if ( (transf & 4) == 4 ) {
l->start = flipX( l->start );
l->end = flipX( l->end );
}
if ( (transf & 2) == 2 ) {
l->start = flipY( l->start );
l->end = flipY( l->end );
}
if ( (transf & 1) == 1 ) {
l->start = swap( l->start );
l->end = swap( l->end );
}
return transf;
}
/* Reverse transformations done over point data table */
void table_reverse(point_table *tbl, short int transf) {
if (tbl == NULL) return;
// Reverse transformation over the table of points
uint ne;
for (ne = 0; ne < tbl->num_elems; ne++) {
if ( (transf & 1) == 1 ) (tbl->data)[ne] = swap( (tbl->data)[ne] );
if ( (transf & 2) == 2 ) (tbl->data)[ne] = flipY( (tbl->data)[ne] );
if ( (transf & 4) == 4 ) (tbl->data)[ne] = flipX( (tbl->data)[ne] );
}
}
/* Given a line, raster it using the algorithm provided */
point_table line_raster(const line l, point_table (*algo) (line) ) {
point_table tbl = {NULL, 0};
// Check for special case: if special, return point table
short int sp = -1;
tbl = line_isspecial(l, &sp);
// Isn't special?
if (sp < 0) {
// 1) Transform line to first octant
line lt = l;
short int transf = line_firstoct(<);
// 2) Apply the line drawing algorithm wanted
tbl = algo(lt);
// 3) Reverse trasnformation over the table of points
table_reverse(&tbl, transf);
}
// Return table of points or NULL if error.
return tbl;
}
void put_line(raster r, line l, color c, point_table (*algo) (line) ) {
point_table tbl = line_raster(l, algo);
if (tbl.data != NULL) {
uint ne;
for (ne = 0; ne < tbl.num_elems; ne++)
put_pixel(r, (tbl.data)[ne], c);
free(tbl.data);
}
}
void put_line_z(raster r, line l, color c, point_table (*algo) (line), double **zmat, double zs, double ze) {
point_table tbl = line_raster(l, algo);
if (tbl.data != NULL) {
uint n = 0;
double inc = (ze - zs) / (double) tbl.num_elems;
double z = zs;
for (n = 0; n < tbl.num_elems; n++) {
fprintf(stderr, "(%d,%d) ", (tbl.data)[n].x, (tbl.data)[n].y);
if (z < zmat[(tbl.data)[n].y][(tbl.data)[n].x]) {
put_pixel(r, (tbl.data)[n], c);
fprintf(stderr, "drawn!");
} else {
fprintf(stderr, "not drawn!");
}
z += inc;
fprintf(stderr, "\n");
}
free(tbl.data);
fprintf(stderr, "\n");
}
}