Created
          September 11, 2024 03:21 
        
      - 
      
- 
        Save nicolas-oliveira/0516e53e710b3f017c20bd89094bbcc5 to your computer and use it in GitHub Desktop. 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | #include <math.h> | |
| #include <stdio.h> | |
| #include <string.h> | |
| #ifndef _WIN32 | |
| #include <unistd.h> | |
| #else | |
| #include <windows.h> | |
| void usleep(__int64 usec) | |
| { | |
| HANDLE timer; | |
| LARGE_INTEGER ft; | |
| ft.QuadPart = -(10 * usec); | |
| timer = CreateWaitableTimer(NULL, TRUE, NULL); | |
| SetWaitableTimer(timer, &ft, 0, NULL, NULL, 0); | |
| WaitForSingleObject(timer, INFINITE); | |
| CloseHandle(timer); | |
| } | |
| #endif | |
| float A, B, C; | |
| float cubeWidth = 25; | |
| int width = 130, height = 30; | |
| float zBuffer[160 * 30]; | |
| char buffer[160 * 30]; | |
| int backgroundASCIICode = ' '; | |
| int distanceFromCam = 150; | |
| float horizontalOffset; | |
| float K1 = 40; | |
| float incrementSpeed = 0.4; | |
| float x, y, z; | |
| float ooz; | |
| int xp, yp; | |
| int idx; | |
| float calculateX(int i, int j, int k) { | |
| return j * sin(A) * sin(B) * cos(C) - k * cos(A) * sin(B) * cos(C) + | |
| j * cos(A) * sin(C) + k * sin(A) * sin(C) + i * cos(B) * cos(C); | |
| } | |
| float calculateY(int i, int j, int k) { | |
| return j * cos(A) * cos(C) + k * sin(A) * cos(C) - | |
| j * sin(A) * sin(B) * sin(C) + k * cos(A) * sin(B) * sin(C) - | |
| i * cos(B) * sin(C); | |
| } | |
| float calculateZ(int i, int j, int k) { | |
| return k * cos(A) * cos(B) - j * sin(A) * cos(B) + i * sin(B); | |
| } | |
| void calculateForSurface(float cubeX, float cubeY, float cubeZ, int ch) { | |
| x = calculateX(cubeX, cubeY, cubeZ); | |
| y = calculateY(cubeX, cubeY, cubeZ); | |
| z = calculateZ(cubeX, cubeY, cubeZ) + distanceFromCam; | |
| ooz = 1 / z; | |
| xp = (int)(width / 2 + horizontalOffset + K1 * ooz * x * 2); | |
| yp = (int)(height / 2 + K1 * ooz * y); | |
| idx = xp + yp * width; | |
| if (idx >= 0 && idx < width * height) { | |
| if (ooz > zBuffer[idx]) { | |
| zBuffer[idx] = ooz; | |
| buffer[idx] = ch; | |
| } | |
| } | |
| } | |
| int main() { | |
| printf("\x1b[2J"); | |
| while (1) { | |
| memset(buffer, backgroundASCIICode, width * height); | |
| memset(zBuffer, 0, width * height * 4); | |
| cubeWidth = 20; | |
| horizontalOffset = -2 * cubeWidth; | |
| for (float cubeX = -cubeWidth; cubeX < cubeWidth; cubeX += incrementSpeed) { | |
| for (float cubeY = -cubeWidth; cubeY < cubeWidth; | |
| cubeY += incrementSpeed) { | |
| calculateForSurface(cubeX, cubeY, -cubeWidth, '@'); | |
| calculateForSurface(cubeWidth, cubeY, cubeX, '$'); | |
| calculateForSurface(-cubeWidth, cubeY, -cubeX, '~'); | |
| calculateForSurface(-cubeX, cubeY, cubeWidth, '#'); | |
| calculateForSurface(cubeX, -cubeWidth, -cubeY, ';'); | |
| calculateForSurface(cubeX, cubeWidth, cubeY, '+'); | |
| } | |
| } | |
| printf("\x1b[H"); | |
| for (int k = 0; k < width * height; k++) { | |
| putchar(k % width ? buffer[k] : 10); | |
| } | |
| A += 0.05; | |
| B += 0.05; | |
| C += 0.01; | |
| usleep(8000 * 2); | |
| } | |
| return 0; | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment