-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdraw.js
More file actions
132 lines (100 loc) · 2.99 KB
/
draw.js
File metadata and controls
132 lines (100 loc) · 2.99 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
var currentDrawColor = {r: 255, g: 0, b: 255};
var defaultDrawColor = {r: 255, g: 255, b: 0};
export function drawLine(x0, y0, x1, y1, s) {
x0 = Math.floor(x0);
y0 = Math.floor(y0);
x1 = Math.floor(x1);
y1 = Math.floor(y1);
let z = -1000;
console.log(x0)
if (x0 > x1) {
let temp = [x1, y1];
x1 = x0;
y1 = y0;
x0 = temp[0];
y0 = temp[1];
//Switch coordinates
}
let deltaX = x1 - x0;
let deltaY = y1 - y0;
let A = deltaY;
let B = - (deltaX);
if (deltaY > 0) {
//Octants 1 and 2
if (deltaX > deltaY) {
//Octant 1
let d = 2*A + B;
let x = x0;
let y = y0;
while (x <= x1) {
s.plot(x, y, z, defaultDrawColor);
if (d > 0) {
y++;
d += 2*B;
}
x++;
d += 2*A;
}
} else {
//Octant 2
let d = A + 2*B;
let x = x0;
let y = y0;
while (y <= y1) {
s.plot(x, y, z, defaultDrawColor);
if (d < 0) {
x++;
d += 2*A;
}
y++;
d += 2*B;
}
}
} else {
//Octants 7 and 8
if (deltaX > Math.abs(deltaY)) {
//Octant 8
let d = 2*A - B;
let x = x0;
let y = y0;
while (x <= x1) {
s.plot(x, y, z, defaultDrawColor);
if (d < 0) {
y--;
d -= 2*B;
}
x++;
d += 2*A;
}
} else {
//Octant 7
let d = A - 2*B;
let x = x0;
let y = y0;
while (y >= y1) {
s.plot(x, y, z, defaultDrawColor);
if (d > 0) {
x++;
d += 2*A;
}
y--;
d -= 2*B;
}
}
}
}
export function drawScanline(y, x0, x1, z0, z1, s) {
let [p0, p1] = [[x0, z0], [x1, z1]]
let order = [p0, p1].sort((a, b) => a[0] - b[0]).map(coord => [Math.floor(coord[0]), coord[1]]);
let delz = Math.floor(order[1][1] - order[0][1])/(order[1][0] - order[0][0]);
y = Math.floor(y)
// let count = 0;
for (let x = order[0][0], z = order[0][1]; x <= order[1][0]; x++, z+=delz) {
s.plot(x, y, z, currentDrawColor);
// count++;
// if (count == 1) break
}
}
export function setColor (rr, gg, bb) {
currentDrawColor = {r: Math.floor(rr), g: Math.floor(gg), b: Math.floor(bb)};
}