de v c++玫瑰花代码
时间: 2023-11-26 11:02:57 浏览: 167
基于c++实现的玫瑰花代码
以下是使用Dev-C++和EasyX绘图软件绘制玫瑰花的C++代码:
```c++
#include <graphics.h>
#include <math.h>
// 定义结构体
struct DOT {
double x;
double y;
double z;
double red; // 红色
double green; // 绿色
// blue(蓝色)通过red计算
};
// 定义常量
const double PI = 3.14159265358979323846;
const int WIDTH = 800;
const int HEIGHT = 600;
const int RADIUS = 200;
const int PETALS = 7;
const int LEAFS = 15;
// 定义函数
void drawPetal(DOT center, double angle, double radius, double width, double red, double green);
void drawLeaf(DOT center, double angle, double radius, double width, double red, double green);
void drawRose();
int main() {
initgraph(WIDTH, HEIGHT);
drawRose();
getch();
closegraph();
return 0;
}
// 绘制花瓣
void drawPetal(DOT center, double angle, double radius, double width, double red, double green) {
double x1, y1, x2, y2, x3, y3, x4, y4;
x1 = center.x + radius * cos(angle - PI / 2);
y1 = center.y + radius * sin(angle - PI / 2);
x2 = center.x + radius * cos(angle + PI / 2);
y2 = center.y + radius * sin(angle + PI / 2);
x3 = center.x + (radius + width) * cos(angle);
y3 = center.y + (radius + width) * sin(angle);
x4 = center.x + (radius - width) * cos(angle);
y4 = center.y + (radius - width) * sin(angle);
setfillcolor(RGB(red, green, 255 - red));
BeginPath();
MoveToEx(x1, y1, NULL);
LineTo(x3, y3);
LineTo(x2, y2);
LineTo(x4, y4);
EndPath();
FillPath();
}
// 绘制叶子
void drawLeaf(DOT center, double angle, double radius, double width, double red, double green) {
double x1, y1, x2, y2, x3, y3, x4, y4;
x1 = center.x + radius * cos(angle - PI / 2);
y1 = center.y + radius * sin(angle - PI / 2);
x2 = center.x + radius * cos(angle + PI / 2);
y2 = center.y + radius * sin(angle + PI / 2);
x3 = center.x + (radius + width) * cos(angle);
y3 = center.y + (radius + width) * sin(angle);
x4 = center.x + (radius - width) * cos(angle);
y4 = center.y + (radius - width) * sin(angle);
setfillcolor(RGB(red, green, 255 - red));
BeginPath();
MoveToEx(x1, y1, NULL);
LineTo(x3, y3);
LineTo(center.x, center.y);
LineTo(x4, y4);
EndPath();
FillPath();
}
// 绘制玫瑰花
void drawRose() {
DOT center;
center.x = WIDTH / 2;
center.y = HEIGHT / 2;
center.z = 0;
for (int i = 0; i < PETALS; i++) {
double angle = 2 * PI / PETALS * i;
drawPetal(center, angle, RADIUS, 20, 255, 0);
drawPetal(center, angle, RADIUS, 10, 255, 255);
}
for (int i = 0; i < LEAFS; i++) {
double angle = 2 * PI / LEAFS * i;
drawLeaf(center, angle, RADIUS * 0.8, 10, 0, 255);
}
}
```
阅读全文