-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw.c
177 lines (151 loc) · 3.49 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#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) {
/*printf("WHERE ARE U SEG FAULT ft. add_point -1 \n");
grow_matrix(points, (points->cols) + 1);
printf("WHERE ARE U SEG FAULT ft. add_point 0 \n");
int c = points->cols;
printf("WHERE ARE U SEG FAULT ft. add_point 1 \n");
points->m[0][c] = x;
points->m[1][c] = y;
points->m[2][c] = z;
points->m[3][c] = 1;
printf("WHERE ARE U SEG FAULT ft. add_point 2 \n");*/
if (points->lastcol == points->cols) {
grow_matrix(points,points->cols + 1);
}
//I HAVE NO IDEA WHY I USE LASTCOL INSTEAD OF ?!?!?
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 = points->lastcol + 1;
}
/*======== 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) {
printf("WHERE ARE U SEG FAULT ft. add_edge \n");
add_point(points, x0, y0, z0);
add_point(points, x1, y1, z1);
printf("WHERE ARE U SEG FAULT ft. add_edge pt. 2\n");
}
/*======== 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
====================*/
//M[R][C]
void draw_lines( struct matrix * points, screen s, color c) {
//matrix is abbccdda
for (int co = 1; co < points->cols; co++) {
draw_line(points->m[0][co-1], points->m[1][co-1], points->m[0][co], points->m[1][co], 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;
}
}
}
}
}