七夕节要到了~在网上看到篇文章很有意思,现在分享给大家~
用C语言打印会跳动的心,送给最爱的那个她!
这篇文章中的代码可以完成几个不同的爱心图案,其中有一个例子甚至可以在黑色背景的控制台上打印出跳动的3D爱心动画,效果相当震撼!说不定可以给她一个惊喜(吓?)噢~
情人节送什么给她- =大家可以一起探讨噢!
一、普通的爱心图案
c语言代码是这样子的:
#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;
}
二、带花纹的爱心图案
C语言代码是这样子的:
#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;
}
#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) {
float 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 < 25; sy++) {
COORD coord = { 0, sy };
SetConsoleCursorPosition(o, coord);
WriteConsole(o, buffer[sy], 79, NULL, 0);
}
Sleep(33);
}
}
三、跳动的心形图案
c语言代码是这样的
#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) {
float 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 < 25; sy++) {
COORD coord = { 0, sy };
SetConsoleCursorPosition(o, coord);
WriteConsole(o, buffer[sy], 79, NULL, 0);
}
Sleep(33);
}
} |
|