Many programmers feel that their programming work is very tedious, all day to face is a number of written code and can not finish the bug. Today we are going to share some interesting C code with you, maybe it will be fun for your boring work. The code can complete several different love patterns, one of which can even print the beating 3D Love animation on a black background console, which is quite shocking.
I am using the Visual Studio development environment, if you also use this development environment, you can directly copy the following code for effect demonstration.
A common love pattern
C Language code:
#include "stdafx.h" #include <math.h> #include <stdio.h>int main () {float y, x, a;for (y = 1.5f;y > -1.5f;y-= 0.1f) {for (x = -1.5f;x < 1.5f;x + = 0.05f) {a = X*x + Y*y-1;putchar (a*a*a-x*x*y*y*y <= 0.0f?) ‘*‘ : ‘ ‘);} Putchar (' \ n ');} return 0;}
:
Pattern of love pattern
C Language code:
#include "stdafx.h" #include <math.h> #include <stdio.h>int main () {float y, x, z,f;for (y = 1.5f;y > -1.5f;y -= 0.1f) {for (x = -1.5f;x < 1.5f;x + = 0.05f) {z = x*x + y*y-1;f = Z*z*z-x*x*y*y*y;putchar (f <= 0.0f?) ".:-=+*#%@" [(int) (f*-8.0f)]: ');} Putchar (' \ n ');} GetChar (); return 0;}
:
Beating 3D Heart pattern
C Language code:
#include "stdafx.h" #include <stdio.h> #include <math.h> #include <windows.h> #include <tchar.h >float f (float x, float y, float z) {Float a = x * x + 9.0f/4.0f * y * y + z * Z-1;return A * A * a-x * x * z * z * z-9.0f/80.0f * y * y * z * z * z;} Float h (float x, float z) {for (float y = 1.0f; y >= 0.0f; y-= 0.001f) if (f (x, Y, z) <= 0.0f) return Y;return 0.0f;} int main () {HANDLE o = GetStdHandle (std_output_handle); _tchar buffer[25][80] = {_t (')};_tchar ramp[] = _t (".:-=+*#%@") ; for (float t = 0.0f;; t + = 0.1f) {int sy = 0;float s = sinf (t); float A = s * S * s * s * 0.2f;for (float z = 1.3f; z > -1.2f; Z-= 0.1f) {_tchar* p = &buffer[sy++][0];float TZ = Z * (1.2f-a); for (float x = -1.5f; x < 1.5f; x + + 0.05f) {Flo at tx = x * (1.2f + a), float v = f (TX, 0.0f, TZ), if (v <= 0.0f) {Float y0 = h (TX, TZ); float NY = 0.01f;float NX = h (TX + NY, TZ)-Y0;float NZ = h (TX, TZ + NY)-y0;float nd = 1.0F/SQRTF (NX * NX + NY * NY + NZ * nz); float D= (NX + ny-nz) * nd * 0.5f + 0.5f;*p++ = ramp[(int) (d * 5.0f)];} else*p++ = ';}} for (sy = 0; sy < sy++) {COORD COORD = {0, sy}; SetConsoleCursorPosition (o, coord); Writeconsole (o, Buffer[sy], (), NULL, 0);} Sleep (33);}}
The beating love animation requires you to run the program before you can see it.
This paper draws on http://www.codeceo.com/article/c-3d-heart.html
Copyright NOTICE: Welcome to reprint, hope to reprint the same time add the original address, thank you for your cooperation, learning happy!
C Language console print 3D love pattern