aboutsummaryrefslogtreecommitdiffstats
path: root/cube.c
blob: a4fea78c5e44c1f23e9eb9adf337f5e8d1d493c6 (plain) (blame)
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
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

float x, y, z;
float step = 0.05;
float zbuffer[24][80];
float A = 0.0;
float B = 0.0;
char market[24][80];

int main()
{
	system("clear");
	while (1) {
		memset(market, ' ', sizeof(market));
		memset(zbuffer, 0, sizeof(zbuffer));

		for (float u = -1.0; u <= 1.0; u += step) {
			for (float v = -1.0; v <= 1.0; v += step) {
				float faces[6][3] = {
					{ u, v, 1.0 }, // z = 1
					{ u, v, -1.0 }, // z = -1
					{ u, 1.0, v }, // y = 1
					{ u, -1.0, v }, // y = -1
					{ 1.0, u, v }, // x = 1
					{ -1.0, u, v } //x = -1

				};

				for (int f = 0; f < 6; f++) {
					float x = faces[f][0];
					float y = faces[f][1];
					float z = faces[f][2];


					float y1 = y * cos(A) - z * sin(A);
					float z1 = y * sin(A) + z * cos(A);
			      
					float x_rot = x * cos(B) - z1 * sin(B);
					float z_rot = x * sin(B) + z1 * cos(B);
					
					float dis = 3.5;
					float ooZ = 1.0 / (z_rot + dis);

					int xp = (int)(40 + 40 * x_rot * ooZ);
					int yp = (int)(12 + 20 * y1 * ooZ);

					if (yp >= 0 && xp <= 80 && yp >= 0 && yp < 24 && xp >= 0) {
						if (ooZ > zbuffer[yp][xp]) {
							zbuffer[yp][xp] = ooZ;
							market[yp][xp] = '*';
						}
					}
				}
			}
		}
		printf("\x1b[H");
		for (int y = 0; y < 24; y++) {
			for (int x = 0; x < 80; x++) {
				putchar(market[y][x]);
			}
			putchar('\n');
		}

		A += 0.02;
		B += 0.02;
		usleep(11000);
	}
}