用C++画心形曲线: 送给母亲的心(母亲节2020.5.10)

递归随机输出版(本文中是涟漪状输出)
昨天是母亲节,本人在家无事,编写了一个在windows终端下画一颗心的C++程序,母上大人似乎甚为欣悦。

1 运行效果

1.1 低清版

启动计算中…
(Sleep 用来造势)在这里插入图片描述
计算完成:左上角有实时的计算结果刷新。
在这里插入图片描述
按下回车
在这里插入图片描述
按任意键画心,若涟漪般展开。
在这里插入图片描述
在这里插入图片描述
起始位置是随机发生的。在这里插入图片描述
在这里插入图片描述
按ESC结束程序。在这里插入图片描述

1.2 高清版

在这里插入图片描述
在这里插入图片描述
hpbl80NDk2MDcxNQ==,size_16,color_FFFFFF,t_70)
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
和低清版无别,只是分辨率更高(线性上是原来的5倍),清晰了25倍。

下面给出低清版的源码。
p.s. 低清版代码没有做任何优化,假如需要做高清版请自行优化。

2 源码

/************
   By Guhz
  2020.5.10
************/
#include <iostream>
#include <ctime>
#include <cmath>
#include <windows.h>
#include <conio.h>
#define WB "color 70" //WB: White backgroud & Black foregroud
#define WR "color 7C" //WR: White backgroud & Red foregroud
#define unit "* "
#define ESC 27
#define posHeartX 50
#define posHeartY 5
/****************************************************************************************
Color List:

 0=Black  1=Blue 2=Green 3=Ocean blue 4=Red 5=Purple 6=Yellow 7=White 8=Grey 9=Light blue
 A=Light green B=Sky blue C=Light red D=Light purple E=Light yellow F=Bright white

****************************************************************************************/
enum
{
	row = 46,
	col = 47
};
bool Heart[row * col];

void init(void);
void goto_xy(int x, int y);
void illuminate(void); //TODO: Visually illuminate the process of calculating.
void show(void);	   //Show the heart shape as if a drop of red ink in the water.

int main(void)
{
	using std::cin;
	using std::cout;
	int key = 0;

	system(WB); //WB: White backgroud & Black foregroud
	init();
	illuminate();
	cin.get();
	system(WR); //White backgroud & Red foregroud

	while ((key = getch()) && (key != ESC)) //Press ESC to quit.
	{
		system("cls");
		show();
	}

	cout << "\nPress any key to continue.\n";
	cin.get(); //Let everythinig just stop here. Untill you press any key.
	return 0;
}

void init(void)
{
	for (int i = 0; i < row; i++)
		for (int j = 0; j < col; j++)
			Heart[i * col + j] = false;
}

void goto_xy(int x, int y) //瀹氫綅鍏夋爣浣嶇疆鍒版寚瀹氬潗鏍?
{
	HANDLE hOut;
	hOut = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos = {x, y};
	SetConsoleCursorPosition(hOut, pos);
}

void illuminate(void)
{
	using std::cout;
	double x, y, leftValue, rightValue;
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			x = -1.15 + 0.05 * j;
			y = +1.25 - 0.05 * i;
			leftValue = x * x * y * y * y;
			rightValue = pow(x * x + y * y - 1, 3);

			if (leftValue >= rightValue) //Heart curve: x^2*y^3>=(x^2+y^2-1)^3
			{
				Heart[i * col + j] = true;
				goto_xy(0, 0);
				cout << "v positive " << leftValue << " >= " << rightValue;
				goto_xy(posHeartX + 2 * j, posHeartY + i);
				cout << "|";
			}
			else
			{
				Heart[i * col + j] = false;
				goto_xy(0, 0);
				cout << "x negative " << leftValue << " < " << rightValue;
				goto_xy(posHeartX + 2 * j, posHeartY + i);
				cout << "O";
			}
			Sleep(7);
		}
	}
}

void show(void)
{
	using std::cout;
	int fromWhere;
	int x, y;

	srand((unsigned int)clock()); //Let the random seed depends on time
	do
	{
		fromWhere = rand() % (row * col); //fromWhere: 0~row*col-1
	} while (Heart[fromWhere] == false);

	x = fromWhere / col;
	y = fromWhere % col;

	int Rx, Ry, Rmax;
	Rx = x > (row - 1 - x) ? x : (row - 1 - x);
	Ry = y > (col - 1 - y) ? y : (col - 1 - y);
	Rmax = (int)sqrt(Rx * Rx + Ry * Ry) + 1;

	for (int r = 1; r < Rmax; r++)
	{
		for (int i = 0; i < row; i++)
		{
			for (int j = 0; j < col; j++)
			{
				if ((((i - x) * (i - x) + (j - y) * (j - y)) < r * r) && Heart[i * col + j]) //In the circle && Be a part of the heart
				{
					goto_xy(posHeartX + 2 * j, posHeartY + i);
					cout << unit;
				}
			}
		}
		Sleep(20);
	}
}
  • 9
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值