Andites

Untitled

Nov 22nd, 2022
82
0
Never
2
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. #include <math.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5.  
  6. float A, B, C;
  7.  
  8. float cubeWidth = 20;
  9. int width = 160, height = 44;
  10. float zBuffer[160 * 44];
  11. char buffer[160 * 44];
  12. int backgroundASCIICode = '.';
  13. int distanceFromCam = 100;
  14. float horizontalOffset;
  15. float K1 = 40;
  16.  
  17. float incrementSpeed = 0.6;
  18.  
  19. float x, y, z;
  20. float ooz;
  21. int xp, yp;
  22. int idx;
  23.  
  24. float calculateX(int i, int j, int k) {
  25. return j * sin(A) * sin(B) * cos(C) - k * cos(A) * sin(B) * cos(C) +
  26. j * cos(A) * sin(C) + k * sin(A) * sin(C) + i * cos(B) * cos(C);
  27. }
  28.  
  29. float calculateY(int i, int j, int k) {
  30. return j * cos(A) * cos(C) + k * sin(A) * cos(C) -
  31. j * sin(A) * sin(B) * sin(C) + k * cos(A) * sin(B) * sin(C) -
  32. i * cos(B) * sin(C);
  33. }
  34.  
  35. float calculateZ(int i, int j, int k) {
  36. return k * cos(A) * cos(B) - j * sin(A) * cos(B) + i * sin(B);
  37. }
  38.  
  39. void calculateForSurface(float cubeX, float cubeY, float cubeZ, int ch) {
  40. x = calculateX(cubeX, cubeY, cubeZ);
  41. y = calculateY(cubeX, cubeY, cubeZ);
  42. z = calculateZ(cubeX, cubeY, cubeZ) + distanceFromCam;
  43.  
  44. ooz = 1 / z;
  45.  
  46. xp = (int)(width / 2 + horizontalOffset + K1 * ooz * x * 2);
  47. yp = (int)(height / 2 + K1 * ooz * y);
  48.  
  49. idx = xp + yp * width;
  50. if (idx >= 0 && idx < width * height) {
  51. if (ooz > zBuffer[idx]) {
  52. zBuffer[idx] = ooz;
  53. buffer[idx] = ch;
  54. }
  55. }
  56. }
  57.  
  58. int main() {
  59. printf("\x1b[2J");
  60. while (1) {
  61. memset(buffer, backgroundASCIICode, width * height);
  62. memset(zBuffer, 0, width * height * 4);
  63. cubeWidth = 20;
  64. horizontalOffset = -2 * cubeWidth;
  65. // first cube
  66. for (float cubeX = -cubeWidth; cubeX < cubeWidth; cubeX += incrementSpeed) {
  67. for (float cubeY = -cubeWidth; cubeY < cubeWidth;
  68. cubeY += incrementSpeed) {
  69. calculateForSurface(cubeX, cubeY, -cubeWidth, '@');
  70. calculateForSurface(cubeWidth, cubeY, cubeX, '$');
  71. calculateForSurface(-cubeWidth, cubeY, -cubeX, '~');
  72. calculateForSurface(-cubeX, cubeY, cubeWidth, '#');
  73. calculateForSurface(cubeX, -cubeWidth, -cubeY, ';');
  74. calculateForSurface(cubeX, cubeWidth, cubeY, '+');
  75. }
  76. }
  77. cubeWidth = 10;
  78. horizontalOffset = 1 * cubeWidth;
  79. // second cube
  80. for (float cubeX = -cubeWidth; cubeX < cubeWidth; cubeX += incrementSpeed) {
  81. for (float cubeY = -cubeWidth; cubeY < cubeWidth;
  82. cubeY += incrementSpeed) {
  83. calculateForSurface(cubeX, cubeY, -cubeWidth, '@');
  84. calculateForSurface(cubeWidth, cubeY, cubeX, '$');
  85. calculateForSurface(-cubeWidth, cubeY, -cubeX, '~');
  86. calculateForSurface(-cubeX, cubeY, cubeWidth, '#');
  87. calculateForSurface(cubeX, -cubeWidth, -cubeY, ';');
  88. calculateForSurface(cubeX, cubeWidth, cubeY, '+');
  89. }
  90. }
  91. cubeWidth = 5;
  92. horizontalOffset = 8 * cubeWidth;
  93. // third cube
  94. for (float cubeX = -cubeWidth; cubeX < cubeWidth; cubeX += incrementSpeed) {
  95. for (float cubeY = -cubeWidth; cubeY < cubeWidth;
  96. cubeY += incrementSpeed) {
  97. calculateForSurface(cubeX, cubeY, -cubeWidth, '@');
  98. calculateForSurface(cubeWidth, cubeY, cubeX, '$');
  99. calculateForSurface(-cubeWidth, cubeY, -cubeX, '~');
  100. calculateForSurface(-cubeX, cubeY, cubeWidth, '#');
  101. calculateForSurface(cubeX, -cubeWidth, -cubeY, ';');
  102. calculateForSurface(cubeX, cubeWidth, cubeY, '+');
  103. }
  104. }
  105. printf("\x1b[H");
  106. for (int k = 0; k < width * height; k++) {
  107. putchar(k % width ? buffer[k] : 10);
  108. }
  109.  
  110. A += 0.05;
  111. B += 0.05;
  112. C += 0.01;
  113. usleep(8000 * 2);
  114. }
  115. return 0;
  116. }
Comments
Add Comment
Please, Sign In to add comment