Anonymous
Почему моя анимация ASCII продолжает прокручивать вниз вместо того, чтобы обновляться на консоли Windows?
Сообщение
Anonymous » 08 окт 2025, 15:58
Я пытаюсь создать простую трехмерную анимацию вращающегося куба ASCII в консоли Windows с помощью C++:
Код: Выделить всё
#include
#include
#include
#include
#include
using namespace std;
const int WIDTH = 80;
const int HEIGHT = 40;
const float cubeWidth = 20;
float A = 0, B = 0, C = 0;
float distanceFromCamera = 100;
float K1 = 40;
char buffer[WIDTH * HEIGHT];
float zBuffer[WIDTH * HEIGHT];
float cosA, sinA, cosB, sinB, cosC, sinC; // biến lượng giác dùng chung
void gotoxy(HANDLE hConsole, int x, int y) {
COORD pos{(SHORT)x, (SHORT)y};
SetConsoleCursorPosition(hConsole, pos);
}
void rotate(float i, float j, float k, float &x, float &y, float &z) {
// rotation quanh 3 trục
float x1 = i;
float y1 = j * cosA - k * sinA;
float z1 = j * sinA + k * cosA;
float x2 = x1 * cosB + z1 * sinB;
float y2 = y1;
float z2 = -x1 * sinB + z1 * cosB;
x = x2 * cosC - y2 * sinC;
y = x2 * sinC + y2 * cosC;
z = z2;
}
void calculatePoint(float i, float j, float k, char ch) {
float x, y, z;
rotate(i, j, k, x, y, z);
z += distanceFromCamera;
float ooz = 1 / z;
int xp = int(WIDTH / 2 + K1 * ooz * x * 2);
int yp = int(HEIGHT / 2 - K1 * ooz * y);
int idx = xp + yp * WIDTH;
if (idx >= 0 && idx < WIDTH * HEIGHT) {
if (ooz > zBuffer[idx]) {
zBuffer[idx] = ooz;
buffer[idx] = ch;
}
}
}
int main() {
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
// Ẩn con trỏ nhấp nháy
CONSOLE_CURSOR_INFO cursorInfo;
GetConsoleCursorInfo(hConsole, &cursorInfo);
cursorInfo.bVisible = FALSE;
SetConsoleCursorInfo(hConsole, &cursorInfo);
while (true) {
fill(buffer, buffer + WIDTH * HEIGHT, ' ');
fill(zBuffer, zBuffer + WIDTH * HEIGHT, 0);
// tính lượng giác 1 lần mỗi frame
cosA = cosf(A); sinA = sinf(A);
cosB = cosf(B); sinB = sinf(B);
cosC = cosf(C); sinC = sinf(C);
for (float i = -cubeWidth; i < cubeWidth; i += 0.5f) {
for (float j = -cubeWidth; j < cubeWidth; j += 0.5f) {
calculatePoint(i, j, -cubeWidth, '@');
calculatePoint(cubeWidth, j, i, '$');
calculatePoint(-cubeWidth, j, -i, '~');
calculatePoint(-i, j, cubeWidth, '#');
calculatePoint(i, -cubeWidth, -j, ';');
calculatePoint(i, cubeWidth, j, '+');
}
}
gotoxy(hConsole, 0, 0);
for (int k = 0; k < WIDTH * HEIGHT; k++) {
cout
Подробнее здесь: [url]https://stackoverflow.com/questions/79783528/why-does-my-ascii-animation-keep-scrolling-down-instead-of-updating-in-place-on[/url]
1759928289
Anonymous
Я пытаюсь создать простую трехмерную анимацию вращающегося куба ASCII в консоли Windows с помощью C++: [code]#include #include #include #include #include using namespace std; const int WIDTH = 80; const int HEIGHT = 40; const float cubeWidth = 20; float A = 0, B = 0, C = 0; float distanceFromCamera = 100; float K1 = 40; char buffer[WIDTH * HEIGHT]; float zBuffer[WIDTH * HEIGHT]; float cosA, sinA, cosB, sinB, cosC, sinC; // biến lượng giác dùng chung void gotoxy(HANDLE hConsole, int x, int y) { COORD pos{(SHORT)x, (SHORT)y}; SetConsoleCursorPosition(hConsole, pos); } void rotate(float i, float j, float k, float &x, float &y, float &z) { // rotation quanh 3 trục float x1 = i; float y1 = j * cosA - k * sinA; float z1 = j * sinA + k * cosA; float x2 = x1 * cosB + z1 * sinB; float y2 = y1; float z2 = -x1 * sinB + z1 * cosB; x = x2 * cosC - y2 * sinC; y = x2 * sinC + y2 * cosC; z = z2; } void calculatePoint(float i, float j, float k, char ch) { float x, y, z; rotate(i, j, k, x, y, z); z += distanceFromCamera; float ooz = 1 / z; int xp = int(WIDTH / 2 + K1 * ooz * x * 2); int yp = int(HEIGHT / 2 - K1 * ooz * y); int idx = xp + yp * WIDTH; if (idx >= 0 && idx < WIDTH * HEIGHT) { if (ooz > zBuffer[idx]) { zBuffer[idx] = ooz; buffer[idx] = ch; } } } int main() { HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); // Ẩn con trỏ nhấp nháy CONSOLE_CURSOR_INFO cursorInfo; GetConsoleCursorInfo(hConsole, &cursorInfo); cursorInfo.bVisible = FALSE; SetConsoleCursorInfo(hConsole, &cursorInfo); while (true) { fill(buffer, buffer + WIDTH * HEIGHT, ' '); fill(zBuffer, zBuffer + WIDTH * HEIGHT, 0); // tính lượng giác 1 lần mỗi frame cosA = cosf(A); sinA = sinf(A); cosB = cosf(B); sinB = sinf(B); cosC = cosf(C); sinC = sinf(C); for (float i = -cubeWidth; i < cubeWidth; i += 0.5f) { for (float j = -cubeWidth; j < cubeWidth; j += 0.5f) { calculatePoint(i, j, -cubeWidth, '@'); calculatePoint(cubeWidth, j, i, '$'); calculatePoint(-cubeWidth, j, -i, '~'); calculatePoint(-i, j, cubeWidth, '#'); calculatePoint(i, -cubeWidth, -j, ';'); calculatePoint(i, cubeWidth, j, '+'); } } gotoxy(hConsole, 0, 0); for (int k = 0; k < WIDTH * HEIGHT; k++) { cout Подробнее здесь: [url]https://stackoverflow.com/questions/79783528/why-does-my-ascii-animation-keep-scrolling-down-instead-of-updating-in-place-on[/url]