diff options
| author | verdant <im@verdant.ee> | 2026-06-03 15:56:14 +0800 |
|---|---|---|
| committer | verdant <im@verdant.ee> | 2026-06-03 15:56:14 +0800 |
| commit | 2e29f7586445cd2db2bfb0b54b6e44a7640b43fa (patch) | |
| tree | 218ac9a7304d3211b85283435cf26575673c2847 /cube.c | |
| download | cube-2e29f7586445cd2db2bfb0b54b6e44a7640b43fa.tar.gz cube-2e29f7586445cd2db2bfb0b54b6e44a7640b43fa.zip | |
Initial commit
Diffstat (limited to 'cube.c')
| -rw-r--r-- | cube.c | 76 |
1 files changed, 76 insertions, 0 deletions
@@ -0,0 +1,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); + } +} + + + + |
