-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path06_angles.js
More file actions
127 lines (106 loc) · 2.85 KB
/
06_angles.js
File metadata and controls
127 lines (106 loc) · 2.85 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
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 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 putParticle(screen, particle) {
const position = particle[0];
const symbol = particle[1];
const radius = particle[2];
const angle = particle[3];
const x = position[0] + radius * Math.cos(angle);
const y = position[1] + radius * Math.sin(angle);
putString(screen, 'C', position[0], position[1]);
putString(screen, symbol, x, y);
}
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);
}
function radiansToDegrees(radians) {
return radians * 180 / Math.PI;
}
function degreesToRadians(degrees) {
return degrees * Math.PI / 180;
}
// This example does not animate
// particle = [position, name, radius, angle]
// play with angle and radius
// Angle is mentioned in radians
// Functions are provided to convert between radians and degrees
function main() {
const WIDTH = 40;
const HEIGHT = 20;
const screen = createScreen(WIDTH, HEIGHT);
const particle = [[WIDTH / 2, HEIGHT / 2], 'P', 5, degreesToRadians(0)];
putParticle(screen, particle);
displayScreen(screen);
}
main();