用visual studio 编写爱心
时间: 2023-12-17 13:04:12 浏览: 82
以下是用 C++ 语言在 Visual Studio 中编写爱心的代码:
```cpp
#include <iostream>
#include <math.h>
#include <Windows.h>
using namespace std;
const int WIDTH = 80;
const int HEIGHT = 40;
int main()
{
// 设置命令行窗口大小和缓冲区大小
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SMALL_RECT rc = { 0, 0, WIDTH - 1, HEIGHT - 1 };
SetConsoleWindowInfo(hOut, TRUE, &rc);
COORD size = { WIDTH, HEIGHT };
SetConsoleScreenBufferSize(hOut, size);
// 清空屏幕
system("cls");
// 计算爱心曲线上的点坐标
double x, y, z;
for (double i = 0; i < 2 * M_PI; i += 0.05)
{
z = pow(sin(i), 2);
x = 16 * pow(sin(i), 3);
y = -(13 * cos(i) - 5 * cos(2 * i) - 2 * cos(3 * i) - cos(4 * i));
int posX = (int)(x * 2) + WIDTH / 2;
int posY = (int)(y * 2) + HEIGHT / 2 - 5;
if (posX >= 0 && posX < WIDTH && posY >= 0 && posY < HEIGHT)
{
SetConsoleCursorPosition(hOut, { (SHORT)posX, (SHORT)posY });
cout << "*";
}
}
return 0;
}
```
运行程序后,会在命令行窗口中绘制出一个爱心图案。效果如下:
```
**
******
**********
**************
****************
******************
********************
***********************
*************************
***************************
***************************
***************************
***************************
*************************
***********************
********************
******************
****************
**************
**********
******
**
```
阅读全文