首頁 >後端開發 >C++ >解釋像耳朵一樣老的甜甜圈第 3 部分

解釋像耳朵一樣老的甜甜圈第 3 部分

Barbara Streisand
Barbara Streisand原創
2024-12-22 15:47:11266瀏覽

Explaining donut like ears old Part-3

現在剩下的就是巢狀 for 迴圈內發生的事
您可能已經看到 r1sin θ 和 r1cos θ

這些用於在 2D 圖中製作圓

和 r2 用來保持圓圈之間的距離,這樣它們就不會重疊

所以,r2> r1 因為 r2 從原點開始到圓心

現在,為了進行壓倒性的矩陣乘法,我們將建立一個單行

C語言

singleRow circle = {2 + cos(theta), sin(theta), 0};

Java 語言

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

現在製作 3 個矩陣 Ry、Rx、Rz,這將幫助我們旋轉圓和甜甜圈

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)
);

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}};

使用我們之前建立的乘法函數,我們將得到旋轉的甜甜圈座標

C語言

singleRow donut = multiply(circle, Ry);
singleRow rotateX = multiply(donut, Rx);

// We will consider it as [Nx, Ny, Nz]
singleRow spinningDonut = multiply(rotateX, Rz);

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);

我們將製作reciNz,它將是Nz 5(距相機的距離)的倒數

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 應該是 11,但我們現在選擇 12
30 和 15 分別是多少?我不知道
並乘以 reciNz,為什麼?我不知道
甜甜圈代碼有太多未解之謎

現在要使其成為 3D,我們需要某些部分發光

為此,我們需要找到
N = Ny - Nz
- 2 sinB cosphi cosθ
- 2 sinB cosψ
2 cosB sinA sinphi
2 cosA sinψ

N 介於 0 到 √2 之間
現在將 N 乘以 8,最大值為 11

int L = N * 8

為了用亮度列印它,我們將建立一個從最低亮度到最高亮度的字元陣列

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


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

現在是最後一部分
檢查是否:
x y reciNz> zBuffer[0]
如果是,那麼

if (zBuffer[o] < reciNz && y < screen_height && x < screen_width) {
  buffer[o] = charOpts[L > 0 ? L : 0];
  zBuffer[o] = reciNz;
}

如果 L 為負數,則使用 charOpts[0]/ period(.)

以上是解釋像耳朵一樣老的甜甜圈第 3 部分的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn