Home >Backend Development >C++ >Explaining donut like ears old Part-3
Now all that remains is what happens inside nested for-loop
You might have seen that r1sin θ and r1cos θ
These are used for making a circle in 2D graph
And r2 for keeping distance between circles so they don't overlap
So, r2 > r1 because r2 starts from origin to centre of circle
Now, to do that overwhelming seen matrix multiply, we will create a singleRow
In C
singleRow circle = {2 + cos(theta), sin(theta), 0};
In Java
singleRow circle = new singleRow(2 + Math.cos(theta), Math.sin(theta), 0);
Now make 3 matrix Ry, Rx, Rz that will help us in rotation of circle and donut
In Java
// rotation on Y-axis Matrix Ry = new Matrix( new singleRow(Math.cos(phi), 0, Math.sin(phi)), new singleRow(0, 1, 0), new singleRow(-Math.sin(phi), 0, Math.cos(phi)) ); // rotation on X-axis Matrix Rx = new Matrix( new singleRow(1, 0, 0), new singleRow(0, Math.cos(A), Math.sin(A)), new singleRow(0, -Math.sin(A), Math.cos(A)) ); // rotation on Z-axis Matrix Rz = new Matrix( new singleRow(Math.cos(B), Math.sin(B), 0), new singleRow(-Math.sin(B), Math.cos(B), 0), new singleRow(0, 0, 1) );
In C
// rotation on Y-axis Matrix Ry = {{cos(phi), 0, sin(phi)}, {0, 1, 0}, {-sin(phi), 0, cos(phi)}}; // rotation on X-axis Matrix Rx = {{1, 0, 0}, {0, cos(A), sin(A)}, {0, -sin(A), cos(A)}}; // rotation on Z-axis Matrix Rz = {{cos(B), sin(B), 0}, {-sin(B), cos(B), 0}, {0, 0, 1}};
On using multiply function, we created previously, we will get spinning donut coordinates
In C
singleRow donut = multiply(circle, Ry); singleRow rotateX = multiply(donut, Rx); // We will consider it as [Nx, Ny, Nz] singleRow spinningDonut = multiply(rotateX, Rz);
In Java
singleRow donut = Matrix.multiply(circle, Ry); singleRow rotateX = Matrix.multiply(donut, Rx); // We will consider it as [Nx, Ny, Nz] singleRow spinningDonut = Matrix.multiply(rotateX, Rz);
We will make reciNz which will be reciprocal of Nz 5 (distance from camera)
float reciNz = 1 / (spinningDonut.a3 + 5);
int x = 40 + 30 * spinningDonut.a1 * reciNz; int y = 12 + 15 * spinningDonut.a2 * reciNz; // o is index of current buffer int o = x + screen_width * y;
screen_height / 2 should have been 11 but we will go with 12 for Now
what are 30 and 15? IDK
and multiply reciNz, why? IDK
Donut code has too many unsolved mysteries
Now to find make it 3D, we need to some part luminous
For that, we need to find
N = Ny - Nz
- 2 sinB cosϕ cosθ
- 2 sinB cosϕ
2 cosB sinA sinϕ
2 cosA sinϕ
N is between 0 to √2
Now multiply N by 8 which will be to max 11
int L = N * 8
To print it with luminacity, we will create an array of character from lowest to highest luminacity
char charOpts[] = ".,-~:;=!*#$@";
or
char[] charOpts = {'.', ',', '-', '~', ':', ';', '=', '!', '*', '#', '$', '@'};
Now the last part
check if:
x < screen width
y < screen height
reciNz > zBuffer[0]
If yes, then
if (zBuffer[o] < reciNz && y < screen_height && x < screen_width) { buffer[o] = charOpts[L > 0 ? L : 0]; zBuffer[o] = reciNz; }
If L is negative, use charOpts[0]/ period(.)
The above is the detailed content of Explaining donut like ears old Part-3. For more information, please follow other related articles on the PHP Chinese website!