Skip to content

Commit

Permalink
Donut finally created
Browse files Browse the repository at this point in the history
  • Loading branch information
BhJaipal committed Nov 27, 2024
1 parent 0e51cb9 commit 369f504
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 35 deletions.
44 changes: 34 additions & 10 deletions donut-by-me.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ singleRow multiply(singleRow m1, Matrix m2) {
}

int main() {
printf("\x1b[2J");
float A = 0, B = 0;
int R2 = 2, R1 = 1;
int screen_height = 22, screen_width = 80;
Expand All @@ -34,7 +33,7 @@ int main() {
// 32 = space ' '
memset(buffer, 32, 1760);

for (float theta = 0; theta < 6.283058; theta += 0.07) {
for (float theta = 0; theta < 6.28; theta += 0.07) {
for (float phi = 0; phi < 6.28; phi += 0.02) {
float circleX = R2 + R1 * cos(theta),
circleY = R1 * sin(theta);
Expand All @@ -47,18 +46,42 @@ int main() {
singleRow donut2 = multiply(donut1, Rx);

Matrix Rz = {{cos(B), sin(B), 0}, {-sin(B), cos(B), 0}, {0, 0, 1}};
// final result of spinning donut
singleRow donut = multiply(donut2, Rz);

donut.a3+= 5;
float reciNz = 1 / (donut.a3);
// R1 / (Nz + 5)
// 5 is distance or so IDK correctly
float reciNz = R1 / (donut.a3 + 5);

float x = 40 + 30 * donut.a1 * reciNz;
float y = 12 + 15 * donut.a2 * reciNz;
// x position
int x = 40 + 30 * donut.a1 * reciNz;
// y position
int y = 12 + 15 * donut.a2 * reciNz;
// current buffer index where current char has to be set
int o = x + screen_width * y;

int L = 8 * (donut.a2 - donut.a3 + 2 * ((cos(B) * cos(A) * circleY) - cos(theta) * (cos(B) * cos(A) * sin(phi) + sin(B) * cos(phi))));

// L = 8 * (
// (circleY * cosA * cosB - sinϕ * cosB * cosθ * sinA)
// - (circleY * sinA)
// - (sinϕ * cosθ * cosA)
// - (cosϕ * cosθ * sinB)
// )

// L = Ny - Nz
// - 2 sinB cosϕ cosθ
// - 2 sinB cosϕ
// + 2 cosB sinA sinϕ
// + 2 cosA sinϕ
int L = 8 * (donut.a2 - donut.a3
+ 2 * cos(B) * sin(A) * sin(phi)
- 2 * cos(phi) * cos(theta) * sin(B)
- 2 * cos(phi) * sin(B)
+ 2 * cos(A) * sin(phi)
);

char charOpts[] = ".,-~:;=!*#$@";
if (x > 0 && x < screen_width && y < 0 && y < screen_height && zBuffer[o] < reciNz) {
if (zBuffer[o] < reciNz && y < screen_height && x < screen_width) {
// printf("%d ", L);
buffer[o] = charOpts[L > 0 ? L : 0];
zBuffer[o] = reciNz;
}
Expand All @@ -67,11 +90,12 @@ int main() {

printf("\x1b[H");
for (int i = 0; i < 1761; i++) {
// printf("%d ", buffer[i]);
putchar(i%80 ? buffer[i]: 10);
A += 0.00004;
B += 0.00002;
}
usleep(100000);
usleep(20000);
}
return 0;
}
12 changes: 10 additions & 2 deletions donut.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ int main(int argc, char *argv[]) {
// (cos(θ) + 2)
float circleX = cos(theta) + 2;

// D = R1 / Nz
// D = R1 / (Nz + 5)
float D = 1 / (sP * circleX * cA + circleY * sA + 5);

float cP = cos(phi);
Expand Down Expand Up @@ -58,7 +58,15 @@ int main(int argc, char *argv[]) {
// o = 10 * ((4 + 3 * Nx) + (96 + 120 * Ny)) / Nz
int o = x + 80 * y;

int N = 8 * ((circleY * cA - sP * cT * sA) * cB - sP * cT * cA - circleY * sA - cP * cT * sB);
// N = Ny - Nz
// - 2 sinB cosϕ cosθ
// - 2 sinB cosϕ
// + 2 cosB sinA sinϕ
// + 2 cosA sinϕ
int N = 8 * ((circleY * cA * cB) - (sP * cT * sA * cB) - sP * cT * cA - circleY * sA - cP * cT * sB);
if (N > 0) {
printf("%d", N);
return 0;}

if (screen_height > y && y > 0 && x > 0 && screen_width > x && D > zBuffer[o]) {
zBuffer[o] = D;
Expand Down
45 changes: 22 additions & 23 deletions rotational-matrix-calc.txt
Original file line number Diff line number Diff line change
@@ -1,48 +1,47 @@
Make:
L = sinθ cosA cosB
- cosθ cosϕ sinB
L = sinθ cosB cosA
- sinϕ sinA cosθ cosB
- cosA cosθ sinϕ - sinθ sinA

L = cosB cosA sinθ
- cosϕ cosθ sinB
− cosB cosθ sinA sinϕ
− cosA cosθ sinϕ − sinA sinθ
- cosA cosθ sinϕ
- sinθ sinA
- cosθ cosϕ sinB

Nx:
cosB cosϕ cosθ
+ 2 cosB cosϕ
- sinB cosA sinθ
+ sinB sinA sinϕ cosθ // 4.2
+ sinB sinA sinϕ cosθ
+ 2 sinB sinA sinϕ

Ny:
sinB cosϕ cosθ // 1
sinB cosϕ cosθ // 5
+ 2 sinB cosϕ
- cosB cosA sinθ // 4.1
+ cosB sinA sinϕ cosθ // 4.2
+ 2 cosB sinA sinϕ
+ cosB cosA sinθ // 1
- cosB sinA sinϕ cosθ // 2
- 2 cosB sinA sinϕ

Nz:
sinA sinθ // 3
+ cosA sinϕ cosθ // 2
sinA sinθ // 4
+ cosA sinϕ cosθ // 3
+ 2 cosA sinϕ


Ny - Nz:
- cosB cosA sinθ // 4.1
+ sinB cosϕ cosθ // 1
- cosA sinϕ cosθ // 2
- sinA sinθ // 3
+ cosB sinA sinϕ cosθ // 4.2

+ cosB cosA sinθ // 1
- cosB sinA sinϕ cosθ // 2
- cosA sinϕ cosθ // 3
- sinA sinθ // 4
+ sinB cosϕ cosθ // 5 * -1
+ 2 sinB cosϕ
+ 2 cosB sinA sinϕ
- 2 cosB sinA sinϕ
- 2 cosA sinϕ

rotational matrix of torus = (Nx, Ny, Nz)

L = Ny - Nz + 2(cosB cosA sinθ - cosB sinA sinϕ cosθ - sinB.cosϕ.cosθ)
L = Ny - Nz
- 2 sinB cosϕ cosθ
- 2 sinB cosϕ
+ 2 cosB sinA sinϕ
+ 2 cosA sinϕ

l_index = L * 8
buffer[o] = ".,-~:;=!*#$@"[l_index]

0 comments on commit 369f504

Please sign in to comment.