当前位置:   article > 正文

用C++画心形曲线: 送给母亲的心(母亲节2020.5.10)_c++怎么用10做心

c++怎么用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);
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/空白诗007/article/detail/804457
推荐阅读
相关标签
  

闽ICP备14008679号