-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path08_image.js
More file actions
139 lines (118 loc) · 2.86 KB
/
08_image.js
File metadata and controls
139 lines (118 loc) · 2.86 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
133
134
135
136
137
138
139
function createScreen(width, height) {
const screen = [];
for (let i = 0; i < height; i++) {
const row = [];
for (let j = 0; j < width; j++) {
row.push(' ');
}
screen.push(row);
}
return screen;
}
function putPixel(screen, char, x, y) {
if (x < 0 || x >= screen[0].length || y < 0 || y >= screen.length) {
return;
}
screen[Math.floor(y)][Math.floor(x)] = char;
}
function drawHorizontalLine(screen, char, x1, x2, y) {
for (let i = x1; i <= x2; i++) {
putPixel(screen, char, i, y);
}
}
function drawVerticalLine(screen, char, x, y1, y2) {
for (let i = y1; i <= y2; i++) {
putPixel(screen, char, x, i);
}
}
function drawRectangle(screen, char, x1, y1, x2, y2) {
drawHorizontalLine(screen, char, x1, x2, y1);
drawHorizontalLine(screen, char, x1, x2, y2);
drawVerticalLine(screen, char, x1, y1, y2);
drawVerticalLine(screen, char, x2, y1, y2);
}
function putString(screen, str, x, y) {
for (let i = 0; i < str.length; i++) {
putPixel(screen, str[i], x + i, y);
}
}
function putVerticalString(screen, str, x, y) {
for (let i = 0; i < str.length; i++) {
putPixel(screen, str[i], x, y + i);
}
}
function putImage(screen, image, x, y) {
for (let i = 0; i < image.length; i++) {
for (let j = 0; j < image[i].length; j++) {
putPixel(screen, image[i][j], x + j, y + i);
}
}
}
function screenToString(screen) {
let frame = '';
for (let i = 0; i < screen.length; i++) {
frame += screen[i].join('');
}
return frame;
}
function displayScreen(screen) {
for (const line of screen) {
console.log(line.join(''));
}
}
function clearScreen(screen) {
for (let i = 0; i < screen.length; i++) {
for (let j = 0; j < screen[i].length; j++) {
screen[i][j] = ' ';
}
}
}
function radiansToDegrees(radians) {
return radians * 180 / Math.PI;
}
function degreesToRadians(degrees) {
return degrees * Math.PI / 180;
}
function animate(screen, particle, steps) {
let frames = '';
for (let i = 0; i < steps; i++) {
clearScreen(screen);
putParticle(screen, particle);
updateParticle(particle);
frames += screenToString(screen);
}
return frames;
}
function displayAnimFormat(width, height, frames) {
console.log(width, height);
console.log(frames);
}
// this example does not animate
// Run it on the terminal and see the output
function main() {
const WIDTH = 40;
const HEIGHT = 20;
const screen = createScreen(WIDTH, HEIGHT);
const image1 = [
'| |',
'| |',
'|-|',
'|-|',
'| |',
'| |',
];
// can also be array of arrays
const image2 = [
['\\', ' ', '/'],
[' ', '|', ' '],
[' ', '|', ' '],
['/', ' ', '\\'],
]
putImage(screen, image1, 5, 2);
putImage(screen, image1, 15, 2);
putImage(screen, image1, 5, 10);
putImage(screen, image1, 15, 10);
putImage(screen, image2, 10, 7);
displayScreen(screen);
}
main();