-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw.c
80 lines (68 loc) · 1.59 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
#include <stdio.h>
#include <stdlib.h>
#include "ml6.h"
#include "display.h"
#include "draw.h"
//Insert your line algorithm here
void draw_line(int x0, int y0, int x1, int y1, screen s, color c) {
int A = y1 - y0;
int B = -x1 + x0;
int x, y;
x = x0;
y = y0;
int d;
if ((A >= 0 && -B >= 0) || (A <= 0 && -B <= 0)) {
// octants I, II
if (abs(B) >= abs(A)) {
// octant I
d = 2*A + B;
while (x <= x1) {
plot(s, c, x, y);
if (d > 0) {
y++;
d += 2*B;
}
x++;
d += 2*A;
}
} else {
// octant II
d = A + 2*B;
while (y <= y1) {
plot(s, c, x, y);
if (d < 0) {
x++;
d += 2*A;
}
y++;
d += 2*B;
}
}
} else {
if (abs(B) >= abs(A)) {
// octant VIII
d = 2*A - B;
while (x <= x1) {
plot(s, c, x, y);
if (d < 0) {
y--;
d -= 2*B;
}
x++;
d += 2*A;
}
} else {
// octant VII
d = A - 2*B;
while (x <= x1) {
plot(s, c, x, y);
if (d > 0) {
x++;
d += 2*A;
}
y--;
d -= 2*B;
}
}
}
}