-
Notifications
You must be signed in to change notification settings - Fork 0
/
line.js
249 lines (235 loc) · 6.56 KB
/
line.js
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/**
* Ahnaf Hasan
* MKS66 - Computer Graphics
* Because it's time, you'll make a line
* 2/9/2018
*/
const fs = require('fs')
// CHECKS FOR INSTALLED DEPENDENCIES
const exec = require('child_process').execSync
var cmd = "npm ls colors"
var errs = ""
var e = false
try {
exec(cmd)
} catch (error) {
e = true
errs += "color\n"
}
if (e) {
console.log(`There are missing dependencies! Please have the following installed to continue:\n${errs}\n
Run "make install" to install packages`.trim())
process.exit(1)
}
var color = require('colors')
var grid = []
/**
* Clears the grid and creates a new one
* @param {*} size an int
*/
function clear_screen(size) {
grid = []
for (i = 0; i < size; i++) {
var row = []
for (w = 0; w < size; w++) {
row.push("0 0 0")
}
grid.push(row)
}
}
/**
* Plots the point on the grid. Color is prechosen
* @param {*} x x value of point (int)
* @param {*} y y value of point (int)
* @param {*} color color code for point (String)
*/
function plot(x, y, color) {
let size = grid.length - 1
if (color.toLowerCase() == "green") {
grid[size - y][x] = "0 255 0"
return true
}
if (color.toLowerCase() == "red") {
grid[size - y][x] = "255 0 0"
return true
}
if (color.toLowerCase() == "blue") {
grid[size - y][x] = "0 0 255"
return true
}
if (color.toLowerCase() == "aqua") {
grid[size - y][x] = "132 220 198"
return true
}
return false
}
/**
* Graphs the line connecting the points (x0, y0) and (x1, y1).
* **Works for octant 1**
* @param {Number} x0 x coordinate of first point (int)
* @param {Number} y0 y coordinate of first point (int)
* @param {Number} x1 x coordinate of second point (int)
* @param {Number} y1 y coordinate of second point (int)
*/
function octant_1(x0, y0, x1, y1) {
var x = x0
var y = y0
var A = y1 - y0
var B = -(x1 - x0)
var d = 2*A + B
while(x <= x1) {
// console.log(`x:${x}\ty:${y}\td:${d}`)
plot(x, y, "red")
if (d > 0) {
y++
d += 2*B
}
x++
d += 2*A
}
console.log("Octant 1 has been plotted (red)".red)
return true
}
/**
* Graphs the line connecting the points (x0, y0) and (x1, y1).
* **Works for octant 2**
* @param {Number} x0 x coordinate of first point (int)
* @param {Number} y0 y coordinate of first point (int)
* @param {Number} x1 x coordinate of second point (int)
* @param {Number} y1 y coordinate of second point (int)
*/
function octant_2(x0, y0, x1, y1) {
var x = x0
var y = y0
var A = y1 - y0
var B = -(x1 - x0)
var d = 2*A + B
while(y <= y1) {
// console.log(`x:${x}\ty:${y}\td:${d}`)
plot(x, y, "green")
if (d < 0) {
x++
d += 2*A
}
y++
d += 2*B
}
console.log("Octant 2 has been plotted (green)".green)
return true
}
/**
* Graphs the line connecting the points (x0, y0) and (x1, y1).
* **Works for octant 3**
* @param {Number} x0 x coordinate of first point (int)
* @param {Number} y0 y coordinate of first point (int)
* @param {Number} x1 x coordinate of second point (int)
* @param {Number} y1 y coordinate of second point (int)
*/
function octant_3(x0, y0, x1, y1) {
var x = x0
var y = y0
var A = y1 - y0
var B = -(x1 - x0)
var d = 2*A + B
while(y >= y1) {
// console.log(`x:${x}\ty:${y}\td:${d}\tA:${A}\tB:${B}`)
plot(x, y, "blue")
if (d < 0) {
x++
d -= 2*A
}
y--
d += 2*B
}
console.log("Octant 3 has been plotted (blue)".blue)
return true
}
/**
* Graphs the line connecting the points (x0, y0) and (x1, y1).
* **Works for octant 4**
* @param {Number} x0 x coordinate of first point (int)
* @param {Number} y0 y coordinate of first point (int)
* @param {Number} x1 x coordinate of second point (int)
* @param {Number} y1 y coordinate of second point (int)
*/
function octant_4(x0, y0, x1, y1) {
// console.log(`WARNING WARNING WARNING WARNING WARNING\nx1:${x1}\tx:${x0}`)
var x = x0
var y = y0
var A = y1 - y0
var B = -(x1 - x0)
var d = 2*A + B
while(x <= x1) {
// console.log(`x:${x}\ty:${y}\td:${d}\tA:${A}\tB:${B}`)
plot(x, y, "aqua")
if (d < 0) {
y--
d -= 2*B
}
x++
d += 2*A
}
console.log("Octant 4 has been plotted (cyan)".cyan)
return true
}
/**
* Graphs line using helper functions
* @param {Number} x0 x coordinate of first point (int)
* @param {Number} y0 y coordinate of first point (int)
* @param {Number} x1 x coordinate of second point (int)
* @param {Number} y1 y coordinate of second point (int)
*/
function make_line(x0, y0, x1, y1) {
if (x1 < x0) {
return make_line(x1, y1, x0, y0)
}
var A = y1 - y0
var B = -(x1 - x0)
// console.log(`A:${A}\tB:${B}\t-A/B:${-A/B}`)
var slope = -A/B
if (slope >= 0 && slope <= 1) { // 0 <= slope <= 1
return octant_1(x0, y0, x1, y1)
}
if (slope > 1) { // slope > 1
return octant_2(x0, y0, x1, y1)
}
if (slope < -1) { // large negative slope
return octant_3(x0, y0, x1, y1)
}
if (slope >= -1 && slope < 0) { // -1 <= slope < 0
return octant_4(x0, y0, x1, y1)
}
// code for other octants
}
console.log("Colors correspond to the lines drawn")
clear_screen(1000) // makes and clears the current grid
// make_line(0, 100, 400, 300) // Octant 1
// make_line(0, 150, 100, 499) // Octant 2
// make_line(0, 150, 10, 100) // Octant 3
// make_line(0, 250, 100, 200) // Octant 4
for (i = 0; i < 250; i++) {
make_line(
Math.ceil((Math.random()) * (grid.length - 1)),
Math.ceil((Math.random()) * (grid.length - 1)),
Math.ceil((Math.random()) * (grid.length - 1) % 100),
Math.ceil((Math.random()) * (grid.length - 1) % 100)
)
}
for (i =0; i < 250; i++) {
make_line(
i, (i * 2) % grid.length, (i * 3) % grid.length, (i * 4) % grid.length
)
make_line(
(i * 4) % grid.length, (i * 3) % grid.length, (i * 2) % grid.length, i
)
}
for (i =250; i >= 0; i--) {
make_line(
Math.ceil((i / 4)) % grid.length, Math.ceil((i / 2)) % grid.length, Math.ceil((i / 3)) % grid.length, i
)
}
fs.writeFileSync('image.ppm', `P3\n${grid.length} ${grid.length}\n255\n\n`) // write header
for(i = 0; i < grid.length; i++) {
var string = grid[i].toString().split(',').join(' ')
fs.appendFileSync('image.ppm', `${string}\n`)
}