废话不多说,直接上代码:

#include <cmath>
#include <tchar.h>
#include <windows.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 color(int c)//改变控制台的颜色
{
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c);
	return 0;
}
int main()
{
	color(13);
	HANDLE o = GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_CURSOR_INFO CursorInfo;
	GetConsoleCursorInfo(o, &CursorInfo);
	CursorInfo.bVisible = false;
	SetConsoleCursorInfo(o, &CursorInfo);
	_TCHAR buffer[25][80] = { _T(\' \') };
	_TCHAR ramp[] = _T(\".:-=+*#%@\");
	for (float t = 0.0f;; t += 0.1f) {
		COORD coord = { 20, 24 };
		SetConsoleCursorPosition(o, coord);
		coord.X = coord.Y = 0;
		SetConsoleCursorPosition(o, coord);
		int sy = 0;
		float s = sin(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 / sqrt(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(30);
	}
}

这就是生成一个爱心小程序的所有代码。侵权删谢谢大家。

收藏 打印