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

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

来源:花匠小妙招 时间:2024-12-08 00:41

最新推荐文章于 2024-10-07 21:37:08 发布

水元素法师 于 2020-05-11 09:50:25 发布

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

递归随机输出版(本文中是涟漪状输出)
昨天是母亲节,本人在家无事,编写了一个在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 foregroudinit();illuminate();cin.get();system(WR); //White backgroud & Red foregroudwhile ((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 timedo{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);} }

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141

相关知识

“母亲节”最适合送给母亲的花
母亲节送给母亲的十佳礼物 母亲节送给母亲礼物排行榜
母亲节最适合送给母亲的花
“乐享园艺 心献爱意”种植多肉盆栽花送给母亲 庆祝母亲节
如何画母亲节花朵简笔画
心形花束(心形花束包装教程)
母亲节送什么花,母亲节最适合送给母亲的花
母亲节送什么礼物最好 母亲节送给母亲十佳礼物
母亲节到了,这些小盆栽非常适合送给送母亲!
幼儿园母亲节送给妈妈的礼物教案(通用15篇)

网址: 用C++画心形曲线: 送给母亲的心(母亲节2020.5.10) https://www.huajiangbk.com/newsview954222.html

所属分类:花卉
上一篇: 大学生母亲节庆祝活动方案(3篇)
下一篇: 互动设计素材

推荐分享