Skip to content

Commit

Permalink
Problem in finding L
Browse files Browse the repository at this point in the history
  • Loading branch information
BhJaipal committed Nov 26, 2024
1 parent 91c6328 commit 0e51cb9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 23 deletions.
26 changes: 16 additions & 10 deletions donut-by-me.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,20 @@ singleRow multiply(singleRow m1, Matrix m2) {
int main() {
printf("\x1b[2J");
float A = 0, B = 0;
int R2 = 2, R1 = 1, K2 = 8;
int R2 = 2, R1 = 1;
int screen_height = 22, screen_width = 80;
float zBuffer[1760];
char buffer[1760];
char background = ' ';

printf("\x1b[2J");
while (1) {
int ind= 0;
memset(zBuffer, 0, 7040);
// 32 = space ' '
memset(buffer, 32, 1760);

for (int theta = 0; theta < 6.28; theta += 0.07) {
for (int phi = 0; phi < 6.28; phi++) {
for (float theta = 0; theta < 6.283058; theta += 0.07) {
for (float phi = 0; phi < 6.28; phi += 0.02) {
float circleX = R2 + R1 * cos(theta),
circleY = R1 * sin(theta);
singleRow circle = {circleX, circleY, 0};
Expand All @@ -49,13 +49,19 @@ int main() {
Matrix Rz = {{cos(B), sin(B), 0}, {-sin(B), cos(B), 0}, {0, 0, 1}};
singleRow donut = multiply(donut2, Rz);

donut.a1 += 40;
donut.a2 += 11;
int o = donut.a1 + screen_width * donut.a2;
donut.a3+= 5;
float reciNz = 1 / (donut.a3);

float x = 40 + 30 * donut.a1 * reciNz;
float y = 12 + 15 * donut.a2 * reciNz;
int o = x + screen_width * y;

int L = 8 * (donut.a2 - donut.a3);
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))));
char charOpts[] = ".,-~:;=!*#$@";
buffer[0] = charOpts[L > 0 ? L : 0];
if (x > 0 && x < screen_width && y < 0 && y < screen_height && zBuffer[o] < reciNz) {
buffer[o] = charOpts[L > 0 ? L : 0];
zBuffer[o] = reciNz;
}
}
}

Expand All @@ -65,7 +71,7 @@ int main() {
A += 0.00004;
B += 0.00002;
}
usleep(10000);
usleep(100000);
}
return 0;
}
19 changes: 14 additions & 5 deletions donut.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,27 @@ int main(int argc, char *argv[]) {
float sA = sin(A);
float circleY = sin(theta);
float cA = cos(A);

// (cos(θ) + 2)
float circleX = cos(theta) + 2;
float D = 1 / (sP * circleX * sA + circleY * cA + 5);

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

float cP = cos(phi);
float cB = cos(B);
float sB = sin(B);

float YsA_sPXcA = circleY * sA - sP * circleX * cA;
float YsA_sPXcA = circleY * cA - sP * circleX * sA;

// x = 40 * 30 * (
// x = 40 + 30 * (
// cos(φ) * circleX * cos(B)
// + (
// sin(φ) * circleX * cos(A)
// - sin(θ) * sin(A)
// ) * sin(B)
// ) / (sin(φ) * circleX * sin(A) * sin(θ) * cos(A) + 5)
// x = 40 + 30 * Nx / Nz
int x = 40 + 30 * D * (cP * circleX * cB - YsA_sPXcA * sB);

// y = 12 + 15 * (
Expand All @@ -46,10 +51,14 @@ int main(int argc, char *argv[]) {
// - sin(φ) * circleX * cos(A)
// ) * cos(B)
// ) / (sin(φ) * circleX * sin(A) * sin(θ) * cos(A) + 5)
// y = 12 + 15 * Ny / Nz
int y = 12 + 15 * D * (cP * circleX * sB + YsA_sPXcA * cB);

// o = (40 + 30 * Nx / Nz) + 80 * (12 + 15 * Ny / Nz)
// o = 10 * ((4 + 3 * Nx) + (96 + 120 * Ny)) / Nz
int o = x + 80 * y;

int N = 8 * ((circleY * sA - sP * cT * cA) * cB - sP * cT * sA - circleY * cA - cP * cT * sB);
int N = 8 * ((circleY * cA - sP * cT * sA) * cB - sP * cT * cA - circleY * sA - cP * cT * sB);

if (screen_height > y && y > 0 && x > 0 && screen_width > x && D > zBuffer[o]) {
zBuffer[o] = D;
Expand All @@ -63,7 +72,7 @@ int main(int argc, char *argv[]) {
A += 0.00004;
B += 0.00002;
}
usleep(10000);
usleep(20000);
}
return 0;
}
20 changes: 12 additions & 8 deletions rotational-matrix-calc.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
Make:
L = cosϕ cosθ sinB − cosA cosθ sinϕ − sinA sinθ
+ cosB cosA sinθ − cosB cosθ sinA sinϕ
L = sinθ cosA cosB
- cosθ cosϕ sinB
- 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θ

Nx:
cosB cosϕ cosθ
Expand All @@ -23,22 +30,19 @@ sinA sinθ // 3


Ny - Nz:
sinB cosϕ cosθ // 1
- 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θ // 4.1

+ 2 sinB cosϕ
+ 2 cosB sinA sinϕ
- 2 cosA sinϕ

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

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

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

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

0 comments on commit 0e51cb9

Please sign in to comment.