The complete code for C is
#include <math.h> #include <stdio.h> #include <string.h> #include <unistd.h> typedef struct { double a1; double a2; double a3; } singleRow; typedef struct { singleRow a1; singleRow a2; singleRow a3; } Matrix; singleRow multiply(singleRow m1, Matrix m2) { singleRow res; res.a1 = m1.a1 * m2.a1.a1 + m1.a2 * m2.a2.a1 + m1.a3 * m2.a3.a1; res.a2 = m1.a1 * m2.a1.a2 + m1.a2 * m2.a2.a2 + m1.a3 * m2.a3.a2; res.a3 = m1.a1 * m2.a1.a3 + m1.a2 * m2.a2.a3 + m1.a3 * m2.a3.a3; return res; } int main() { int screen_width = 80, height = 22; char buffer[1760]; float zBuffer[1760]; float A = 0, B = 0; int R2 = 2, R1 = 1; printf("\x1b[2J"); while (1) { memset(buffer, ' ', 1760); memset(zBuffer, 0, 7040); for (float theta = 0; theta < 6.28; theta += 0.07) { for (float phi = 0; phi < 6.28; phi += 0.02) { singleRow circle = {2 + cos(theta), sin(theta), 0}; // 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}}; singleRow donut = multiply(circle, Ry); singleRow rotateX = multiply(donut, Rx); // We will consider it as [Nx, Ny, Nz] singleRow spinningDonut = multiply(rotateX, Rz); 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; int L = 8 * (spinningDonut.a2 - spinningDonut.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) ); // donut luminicity will be seen by these characters // these 12 char charOut[] = ".,-~:;=!*#$@"; if (x < screen_width && y < height && zBuffer[o] < reciNz) { zBuffer[o] = reciNz; // If L < 0, . will be buffer buffer[o] = charOut[L > 0 ? L : 0]; } } } // Clear screen printf("\x1b[H"); for (int i = 0; i <1761; i++) { // On every 80th character, new line will be printed // If there's a reminder then buffer printed putchar(i % 80 ? buffer[i] : 10); A += 0.00004; B += 0.00002; } // Timer to slow down speed a bit usleep(10000); } return 0; }
The complete code for Java is
import java.util.Arrays; class singleRow { public double a1; public double a2; public double a3; public singleRow(double a1, double a2, double a3) { this.a1 = a1; this.a2 = a2; this.a3 = a3; } } class Matrix { public singleRow a1; public singleRow a2; public singleRow a3; public Matrix(singleRow a1, singleRow a2, singleRow a3) { this.a1 = new singleRow(a1.a1, a1.a2, a1.a3); this.a2 = new singleRow(a2.a1, a2.a2, a2.a3); this.a3 = new singleRow(a3.a1, a3.a2, a3.a3); } public static singleRow multiply(singleRow m1, Matrix m2) { singleRow res = new singleRow(0, 0, 0); res.a1 = (m1.a1 * m2.a1.a1) + (m1.a2 * m2.a2.a1) + (m1.a3 * m2.a3.a1); res.a2 = (m1.a1 * m2.a1.a2) + (m1.a2 * m2.a2.a2) + (m1.a3 * m2.a3.a2); res.a3 = (m1.a1 * m2.a1.a3) + (m1.a2 * m2.a2.a3) + (m1.a3 * m2.a3.a3); return res; } } public class Main { public static void main() { int screen_width = 80, height = 22; char[] buffer = new char[1760]; double[] zBuffer = new double[1760]; double A = 0, B = 0; int R2 = 2, R1 = 1; System.out.print("\u001b[2J"); while (true) { Arrays.fill(buffer, 0, 1760, ' '); Arrays.fill(zBuffer, 0, 1760, 0); for (float theta = 0; theta < 6.28; theta += 0.07) { for (float phi = 0; phi < 6.28; phi += 0.02) { singleRow circle = {2 + Math.cos(theta), Math.sin(theta), 0}; // 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) ); 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); 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; int L = 8 * (spinningDonut.a2 - spinningDonut.a3 + 2 * Math.cos(B) * Math.sin(A) * Math.sin(phi) - 2 * Math.cos(phi) * Math.cos(theta) * Math.sin(B) - 2 * Math.cos(phi) * Math.sin(B) + 2 * Math.cos(A) * Math.sin(phi) ); // donut luminicity will be seen by these characters // these 12 char[] charOpts = {'.', ',', '-', '~', ':', ';', '=', '!', '*', '#', '$', '@'}; if (x < screen_width && y < height && zBuffer[o] < reciNz) { zBuffer[o] = reciNz; // If L < 0, . will be buffer buffer[o] = charOut[L > 0 ? L : 0]; } } } // Clear screen System.out.print("\u001b[H"); for (int i = 0; i <1761; i++) { // On every 80th character, new line will be printed // If there's a reminder then buffer printed System.out.print(i % 80 ? buffer[i] : 10); A += 0.00004; B += 0.00002; } } } }
The above is the detailed content of Explaining donut like ears old Part-Last). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Yes, function overloading is a polymorphic form in C, specifically compile-time polymorphism. 1. Function overload allows multiple functions with the same name but different parameter lists. 2. The compiler decides which function to call at compile time based on the provided parameters. 3. Unlike runtime polymorphism, function overloading has no extra overhead at runtime, and is simple to implement but less flexible.

C has two main polymorphic types: compile-time polymorphism and run-time polymorphism. 1. Compilation-time polymorphism is implemented through function overloading and templates, providing high efficiency but may lead to code bloating. 2. Runtime polymorphism is implemented through virtual functions and inheritance, providing flexibility but performance overhead.

Yes, polymorphisms in C are very useful. 1) It provides flexibility to allow easy addition of new types; 2) promotes code reuse and reduces duplication; 3) simplifies maintenance, making the code easier to expand and adapt to changes. Despite performance and memory management challenges, its advantages are particularly significant in complex systems.

C destructorscanleadtoseveralcommonerrors.Toavoidthem:1)Preventdoubledeletionbysettingpointerstonullptrorusingsmartpointers.2)Handleexceptionsindestructorsbycatchingandloggingthem.3)Usevirtualdestructorsinbaseclassesforproperpolymorphicdestruction.4

People who study Python transfer to C The most direct confusion is: Why can't you write like Python? Because C, although the syntax is more complex, provides underlying control capabilities and performance advantages. 1. In terms of syntax structure, C uses curly braces {} instead of indentation to organize code blocks, and variable types must be explicitly declared; 2. In terms of type system and memory management, C does not have an automatic garbage collection mechanism, and needs to manually manage memory and pay attention to releasing resources. RAII technology can assist resource management; 3. In functions and class definitions, C needs to explicitly access modifiers, constructors and destructors, and supports advanced functions such as operator overloading; 4. In terms of standard libraries, STL provides powerful containers and algorithms, but needs to adapt to generic programming ideas; 5

Polymorphisms in C are divided into runtime polymorphisms and compile-time polymorphisms. 1. Runtime polymorphism is implemented through virtual functions, allowing the correct method to be called dynamically at runtime. 2. Compilation-time polymorphism is implemented through function overloading and templates, providing higher performance and flexibility.

C polymorphismincludescompile-time,runtime,andtemplatepolymorphism.1)Compile-timepolymorphismusesfunctionandoperatoroverloadingforefficiency.2)Runtimepolymorphismemploysvirtualfunctionsforflexibility.3)Templatepolymorphismenablesgenericprogrammingfo

C polymorphismisuniqueduetoitscombinationofcompile-timeandruntimepolymorphism,allowingforbothefficiencyandflexibility.Toharnessitspowerstylishly:1)Usesmartpointerslikestd::unique_ptrformemorymanagement,2)Ensurebaseclasseshavevirtualdestructors,3)Emp
