/
draw.c
163 lines (137 loc) · 2.93 KB
/
draw.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
#include <stdio.h>
#include <stdlib.h>
#include "ml6.h"
#include "display.h"
#include "draw.h"
#include "matrix.h"
/*======== void add_point() ==========
Inputs: struct matrix * points
int x
int y
int z
Returns:
adds point (x, y, z) to points and increment points.lastcol
if points is full, should call grow on points
====================*/
void add_point( struct matrix * points, int x, int y, int z) {
if (points->lastcol == points->cols) {
grow_matrix(points, points->lastcol + 10);
}
points->m[0][points->lastcol] = x;
points->m[1][points->lastcol] = y;
points->m[2][points->lastcol] = z;
points->m[3][points->lastcol] = 1;
points->lastcol ++;
}
/*======== void add_edge() ==========
Inputs: struct matrix * points
int x0, int y0, int z0, int x1, int y1, int z1
Returns:
add the line connecting (x0, y0, z0) to (x1, y1, z1) to points
should use add_point
====================*/
void add_edge( struct matrix * points,
int x0, int y0, int z0,
int x1, int y1, int z1) {
add_point(points, x0, y0, z0);
add_point(points, x0, y0, z0);
}
/*======== void draw_lines() ==========
Inputs: struct matrix * points
screen s
color c
Returns:
Go through points 2 at a time and call draw_line to add that line
to the screen
====================*/
void draw_lines( struct matrix * points, screen s, color c) {
int i;
for (i = 0; i < points->lastcol - 1; i += 2) {
draw_line(points->m[0][i], points->m[1][i], points->m[0][i+1], points->m[1][i+1], s, c);
}
}
void draw_line(int x0, int y0, int x1, int y1, screen s, color c) {
int x, y, d, dx, dy;
x = x0;
y = y0;
//swap points so we're always draing left to right
if ( x0 > x1 ) {
x = x1;
y = y1;
x1 = x0;
y1 = y0;
}
//need to know dx and dy for this version
dx = (x1 - x) * 2;
dy = (y1 - y) * 2;
//positive slope: Octants 1, 2 (5 and 6)
if ( dy > 0 ) {
//slope < 1: Octant 1 (5)
if ( dx > dy ) {
d = dy - ( dx / 2 );
while ( x <= x1 ) {
plot(s, c, x, y);
if ( d < 0 ) {
x = x + 1;
d = d + dy;
}
else {
x = x + 1;
y = y + 1;
d = d + dy - dx;
}
}
}
//slope > 1: Octant 2 (6)
else {
d = ( dy / 2 ) - dx;
while ( y <= y1 ) {
plot(s, c, x, y );
if ( d > 0 ) {
y = y + 1;
d = d - dx;
}
else {
y = y + 1;
x = x + 1;
d = d + dy - dx;
}
}
}
}
//negative slope: Octants 7, 8 (3 and 4)
else {
//slope > -1: Octant 8 (4)
if ( dx > abs(dy) ) {
d = dy + ( dx / 2 );
while ( x <= x1 ) {
plot(s, c, x, y);
if ( d > 0 ) {
x = x + 1;
d = d + dy;
}
else {
x = x + 1;
y = y - 1;
d = d + dy + dx;
}
}
}
//slope < -1: Octant 7 (3)
else {
d = (dy / 2) + dx;
while ( y >= y1 ) {
plot(s, c, x, y );
if ( d < 0 ) {
y = y - 1;
d = d + dx;
}
else {
y = y - 1;
x = x + 1;
d = d + dy + dx;
}
}
}
}
}