Logo

dev-resources.site

for different kinds of informations.

Explaining donut like 5 years old Part-3

Published at
12/11/2024
Categories
c
cpp
java
Author
bhjaipal
Categories
3 categories in total
c
open
cpp
open
java
open
Author
8 person written this
bhjaipal
open
Explaining donut like 5 years 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};
Enter fullscreen mode Exit fullscreen mode

In Java

singleRow circle = new singleRow(2 + Math.cos(theta), Math.sin(theta), 0);
Enter fullscreen mode Exit fullscreen mode

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)
);
Enter fullscreen mode Exit fullscreen mode

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}};
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

We will make reciNz which will be reciprocal of Nz + 5 (distance from camera)

float reciNz = 1 / (spinningDonut.a3 + 5);
Enter fullscreen mode Exit fullscreen mode
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;
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

To print it with luminacity, we will create an array of character from lowest to highest luminacity

char charOpts[] = ".,-~:;=!*#$@";
Enter fullscreen mode Exit fullscreen mode

or

char[] charOpts = {'.', ',', '-', '~', ':', ';', '=', '!', '*', '#', '$', '@'};
Enter fullscreen mode Exit fullscreen mode

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;
}
Enter fullscreen mode Exit fullscreen mode

If L is negative, use charOpts[0]/ period(.)

c Article's
30 articles in total
Favicon
Top 5 Backend Programming Languages to Learn in 2024
Favicon
Week 2: Diving Deeper into Dynamic Memory, Structures, and Beyond in C Programming
Favicon
The 10 fastest programming languages in the world
Favicon
As 10 Linguagens de Programação mais velozes do mundo
Favicon
This turned out to be my best-performing technical article. Unfortunately I do not have the time to write more like it.
Favicon
Parsing command-line arguments in C
Favicon
Develop a weather application code using c language with date
Favicon
Working with Matter Team Membership Using the IntApp Walls API
Favicon
Reading UTF-8 char by char in C
Favicon
[Rust Self-Study] 1.0. Intro
Favicon
Gone back to learn C Programming 23 years later.
Favicon
Data access in code, using repositories, even with ORMs
Favicon
MockManager in unit tests - a builder pattern used for mocks
Favicon
Explaining donut like 5 years old Part-4 (Last)
Favicon
Explaining donut like 5 years old Part-3
Favicon
How to 100% CPU
Favicon
Explaining donut like 5 years old Part-3
Favicon
How do you print in c language?
Favicon
Tester c'est tricher, compiler c'est douter
Favicon
Pointers in C Programming - Lay Man's Analogy
Favicon
LogInsight
Favicon
Rust in Systems Programming: Why Devs Are Choosing Rust Over C and C++
Favicon
Discover File Splitter & Merger: A Revolutionary Tool for Managing Large Files
Favicon
Unused variables in C/C++: why and how?
Favicon
Jas - My x64 assembler
Favicon
How Does Deep Learning Work? Can You Write Simple Deep Learning Code at Home?
Favicon
Day 1 : Introduction of DSA
Favicon
Cybersecurity: The Shielding the Virtual Universe
Favicon
OKMX8MP-C GDB Remote Debugging Skills
Favicon
Publishing My First AUR Package: CPIG

Featured ones: