Skip to content

Commit fdcd461

Browse files
committed
Refactor
1 parent 5f7de97 commit fdcd461

File tree

1 file changed

+33
-44
lines changed

1 file changed

+33
-44
lines changed

src/main.rs

Lines changed: 33 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
// Define the screen and speed of rotations around x, y, z axises
22
const SCREEN_H: usize = 80;
33
const SCREEN_W: usize = 160;
4-
const HALF_KSIZE: f64 = 20.0;
4+
const HALF_CUBE_SIZE: f64 = 20.0;
55
const K1: usize = 40;
66
const INCREAMENT_SPEED: f64 = 0.6;
7-
const ROTATE_X_SPEED: f64 = 0.04;
8-
const ROTATE_Y_SPEED: f64 = 0.04;
9-
const ROTATE_Z_SPEED: f64 = 0.04;
7+
const ROTATE_AROUND_X_AXIS_SPEED: f64 = 0.04;
8+
const ROTATE_AROUND_Y_AXIS_SPEED: f64 = 0.04;
9+
const ROTATE_AROUND_Z_AXIS_SPEED: f64 = 0.04;
1010

1111
fn rotate_x(x: f64, y: f64, z: f64, a: f64, b: f64, c: f64) -> f64 {
1212
let (sina, cosa) = f64::sin_cos(a);
@@ -33,17 +33,13 @@ fn rotate_z(x: f64, y: f64, z: f64, a: f64, b: f64, c: f64) -> f64 {
3333
return z * cosa * cosb - y * sina * cosb + x * sinb;
3434
}
3535

36-
//This require update 2d array from a function in RUST, very tricky
37-
fn update<V: AsMut<[f64]>, K: AsMut<[char]>>(
36+
//This require set_character_at_coordinate 2d array from a function in RUST, very tricky
37+
fn set_character_at_coordinate<V: AsMut<[f64]>, K: AsMut<[char]>>(
3838
ch: char,
3939
zbuffer: &mut [V],
4040
output: &mut [K],
4141
(x, y, ooz, idx): (usize, usize, f64, usize),
4242
) {
43-
// if x < SCREEN_H as usize && y < SCREEN_W as usize {
44-
// output[x].as_mut()[y] = ch;
45-
// }
46-
4743
//Add luminance effect to the kube
4844
if idx > 0
4945
&& idx < (SCREEN_H * SCREEN_W) as usize
@@ -71,8 +67,8 @@ fn calculate_for_surface(
7167
let z = rotate_z(cx, cy, cz, a, b, c) + distance_from_eyes;
7268

7369
let ooz = 1.0 / z;
74-
let xp = (30.0 + HALF_KSIZE + K1 as f64 * ooz * x) as usize;
75-
let yp = (30.0 + HALF_KSIZE + K1 as f64 * ooz * y) as usize;
70+
let xp = (30.0 + HALF_CUBE_SIZE + K1 as f64 * ooz * x) as usize;
71+
let yp = (30.0 + HALF_CUBE_SIZE + K1 as f64 * ooz * y) as usize;
7672
let idx = xp + yp * SCREEN_W;
7773
return (xp, yp, ooz, idx);
7874
}
@@ -82,44 +78,37 @@ fn main() {
8278
let mut a = 0.0;
8379
let mut b = 0.0;
8480
let mut c = 0.0;
85-
loop {
81+
loop {
8682
let mut output = [[' '; SCREEN_W]; SCREEN_H]; //SCREEN
8783
let mut zbuffer = [[0.0; SCREEN_W]; SCREEN_H]; //SCREEN
88-
let mut cx = -HALF_KSIZE;
89-
while cx < HALF_KSIZE {
90-
let mut cy = -HALF_KSIZE;
91-
while cy < HALF_KSIZE {
84+
let mut cx = -HALF_CUBE_SIZE;
85+
while cx < HALF_CUBE_SIZE {
86+
let mut cy = -HALF_CUBE_SIZE;
87+
while cy < HALF_CUBE_SIZE {
9288
// Start calculate 6 surfaces of Kube
93-
let (x, y, ooz, idx) = calculate_for_surface(cx, cy, -HALF_KSIZE, a, b, c); // a, b, c are angles of rotations
94-
// Update the the screen with character use about output
95-
update('.', &mut zbuffer, &mut output, (x, y, ooz, idx));
96-
97-
let (x, y, ooz, idx) = calculate_for_surface(cx, cy, HALF_KSIZE, a, b, c); // a, b, c are angles of rotations
98-
// Update the the screen with character use about output
99-
update('#', &mut zbuffer, &mut output, (x, y, ooz, idx));
100-
101-
let (x, y, ooz, idx) = calculate_for_surface(HALF_KSIZE, cx, cy, a, b, c); // a, b, c are angles of rotations
102-
// Update the the screen with character use about output
103-
update('$', &mut zbuffer, &mut output, (x, y, ooz, idx));
104-
105-
let (x, y, ooz, idx) = calculate_for_surface(-HALF_KSIZE, cx, cy, a, b, c); // a, b, c are angles of rotations
106-
// Update the the screen with character use about output
107-
update('~', &mut zbuffer, &mut output, (x, y, ooz, idx));
108-
109-
let (x, y, ooz, idx) = calculate_for_surface(cx, HALF_KSIZE, cy, a, b, c); // a, b, c are angles of rotations
110-
// Update the the screen with character use about output
111-
update(';', &mut zbuffer, &mut output, (x, y, ooz, idx));
112-
113-
let (x, y, ooz, idx) = calculate_for_surface(cx, -HALF_KSIZE, cy, a, b, c); // a, b, c are angles of rotations
114-
// Update the the screen with character use about output
115-
update('+', &mut zbuffer, &mut output, (x, y, ooz, idx));
89+
let (x, y, ooz, idx) = calculate_for_surface(cx, cy, -HALF_CUBE_SIZE, a, b, c); // a, b, c are angles of rotations
90+
set_character_at_coordinate('.', &mut zbuffer, &mut output, (x, y, ooz, idx));
91+
92+
let (x, y, ooz, idx) = calculate_for_surface(cx, cy, HALF_CUBE_SIZE, a, b, c);
93+
set_character_at_coordinate('#', &mut zbuffer, &mut output, (x, y, ooz, idx));
94+
95+
let (x, y, ooz, idx) = calculate_for_surface(HALF_CUBE_SIZE, cx, cy, a, b, c);
96+
set_character_at_coordinate('$', &mut zbuffer, &mut output, (x, y, ooz, idx));
97+
98+
let (x, y, ooz, idx) = calculate_for_surface(-HALF_CUBE_SIZE, cx, cy, a, b, c);
99+
set_character_at_coordinate('~', &mut zbuffer, &mut output, (x, y, ooz, idx));
100+
101+
let (x, y, ooz, idx) = calculate_for_surface(cx, HALF_CUBE_SIZE, cy, a, b, c);
102+
set_character_at_coordinate(';', &mut zbuffer, &mut output, (x, y, ooz, idx));
103+
104+
let (x, y, ooz, idx) = calculate_for_surface(cx, -HALF_CUBE_SIZE, cy, a, b, c);
105+
set_character_at_coordinate('+', &mut zbuffer, &mut output, (x, y, ooz, idx));
116106

117107
cy += INCREAMENT_SPEED;
118108
}
119109
cx += INCREAMENT_SPEED;
120110
}
121111

122-
//SHow to SCREEN
123112
print!("\x1b[H"); //clear screen
124113
for i in 0..SCREEN_H as usize {
125114
for j in 0..SCREEN_W as usize {
@@ -128,9 +117,9 @@ fn main() {
128117
print!("\n");
129118
}
130119

131-
a += ROTATE_X_SPEED;
132-
b += ROTATE_Y_SPEED;
133-
c += ROTATE_Z_SPEED;
120+
a += ROTATE_AROUND_X_AXIS_SPEED;
121+
b += ROTATE_AROUND_Y_AXIS_SPEED;
122+
c += ROTATE_AROUND_Z_AXIS_SPEED;
134123

135124
std::thread::sleep(std::time::Duration::new(0, 30000000));
136125
}

0 commit comments

Comments
 (0)