cube animation in c++ with setconsolecursoreposiotion

18 Views Asked by At

what errors are there (in the algorithm or in the code)

#include <iostream>
#include <cmath>
#include <windows.h>

void rotateCube(int pos[][3], double alpha) {
    double rotateCube[3][3] = {
        {1, 0, 0},
        {0, cos(alpha), -sin(alpha)},
        {0, sin(alpha), cos(alpha)}
    };

    for (int i = 0; i < 8; i++) {
        int x = pos[i][0];
        int y = pos[i][1];
        int z = pos[i][2];

        pos[i][0] = round(rotateCube[0][0] * x + rotateCube[0][1] * y + rotateCube[0][2] * z);
        pos[i][1] = round(rotateCube[1][0] * x + rotateCube[1][1] * y + rotateCube[1][2] * z);
        pos[i][2] = round(rotateCube[2][0] * x + rotateCube[2][1] * y + rotateCube[2][2] * z);
    }

    double d = 5.0;

    for (int i = 0; i < 8; i++) {
        pos[i][0] = round(pos[i][0] * d / (pos[i][2] + d)) + 40;
        pos[i][1] = round(pos[i][1] * d / (pos[i][2] + d)) + 12;
    }
}

void printCube(int cubePositions[][3], HANDLE hConsole, COORD pos) {
    for (int i = 0; i < 8; i++) {
        pos.X = cubePositions[i][0];
        pos.Y = cubePositions[i][1];
        SetConsoleCursorPosition(hConsole, pos);
        std::cout << "o";
    }
}

int main() {
    COORD pos;
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

    double alpha = 0;
    pos.X = 0;
    pos.Y = 0;
    SetConsoleCursorPosition(hConsole, pos);

    int cubePositions[8][3] = { {0,0,0},{0,0,1}, {0,1,0}, {0,1,1}, {1,0,0}, {1,0,1}, {1,1,0}, {1,1,1}};

    while (true) {
        printCube(cubePositions, hConsole, pos);
        Sleep(100);
        alpha += 1;
        rotateCube(cubePositions, alpha);
        system("cls");
    }

    return 0;
}

the first problem is the coordinates, I don’t understand how to set the coordinates so that the cube fits in the window and there are no problems when rotating, the animation does not work correctly, I also want to understand whether I am multiplying the matrices correctly, and what other potential problems there are because coordinates can only be natural numbers

0

There are 0 best solutions below