#include <string.h>
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
#include "map.h"
#include<mmsystem.h>
#pragma comment(lib,"winmm.lib")
#define PI 3.1415926
#define WIDTH 800 // 画面宽度
#define HEIGHT 600 // 画面高度度
float offsetAngle = PI / 6; // 左右枝干和父枝干偏离的角度
float shortenRate = 0.65; // 子枝干比父枝干变短的倍数
int isShowAnimation = 1; // 是否显示树生成的过程动画
void brunch(float x_start, float y_start, float length, float angle, float thickness, int generation) {
// 利用三角函数求出当前枝干的终点x,y坐标
float x_end, y_end;
x_end = x_start + length * cos(angle);
y_end = y_start + length * sin(angle);
// 画线条枝干
setlinestyle(PS_SOLID, thickness); // 设定当前枝干线宽
COLORREF color = HSVtoRGB(15, 0.75, 0.4 + generation * 0.05);
setlinecolor(color); // 设定当前枝干颜色
line(x_start, y_start, x_end, y_end); // 画出当前枝干(画线)
// 求出子枝干的代数
int childGeneration = generation + 1;
// 生成左、右、中间三个子枝干的长度,逐渐变短,并有一定随机性
// childLength 子枝干得个数
// 蓝桥杯
float childLength = shortenRate * length;
float leftChildLength = childLength * randBetween(0.9, 1.1);
float rightChildLength = childLength * randBetween(0.9, 1.1);
float centerChildLength = childLength * randBetween(0.8, 1.1);
// 当子枝干长度大于2,并且代数小于等于10,递归调用产生子枝干
if (childLength >= 2 && childGeneration <= 20)
{
// 生成子枝干的粗细,逐渐变细
float childThickness = thickness * 0.8;
if (childThickness < 2) // 枝干绘图最细的线宽为2
childThickness = 2;
// 一定概率产生左、右、中子枝干
if (randBetween(0, 1) < 0.95)
brunch(x_end, y_end, leftChildLength, angle + offsetAngle * randBetween(0.5, 1), childThickness, childGeneration);
if (randBetween(0, 1) < 0.95)
brunch(x_end, y_end, rightChildLength, angle - offsetAngle * randBetween(0.5, 1), childThickness, childGeneration);
if (randBetween(0, 1) < 0.85)
brunch(x_end, y_end, centerChildLength, angle + offsetAngle / 5 * randBetween(-1, 1), childThickness, childGeneration);
}
else // 最末端绘制樱花,画一个粉色填充圆
{
setlinestyle(PS_SOLID, 1); // 线宽
// 樱花粉色HSVtoRGB(325,0.3,1),有一定随机性
COLORREF color = HSVtoRGB(randBetween(300, 350), randBetween(0.2, 0.3), 1);
// 设置园边线得颜色
setlinecolor(color); // 设定线条颜色
// 设置园填充得颜色
setfillcolor(color); // 设定填充颜色
// 如果子枝干长度小于等于4 开始生产园
if (childLength <= 4) // 如果子枝干长度小于等于4
// fillcircle 填充园得函数
fillcircle(x_end, y_end, 4); // 圆的半径为2(再小就看不清了)
}
if (isShowAnimation) // 如果为1,绘制樱花树生成的过程动画
{
FlushBatchDraw(); // 批量绘制
Sleep(5); // 暂停
}
}void startup() // 初始化
{
srand(time(0)); // 随机初始化
initgraph(WIDTH, HEIGHT, 1); // 新开一个画面
setbkcolor(RGB(255, 255, 255)); // 白色背景
cleardevice(); // 清屏
BeginBatchDraw(); // 开始批量绘制
mciSendString("open yinyue/yinghuacao.mp3 ", 0, 0, 0);
mciSendString("play yinyue/yinghuacao.mp3 ", NULL, 0, 0);
brunch(WIDTH / 2, HEIGHT, 0.45 * HEIGHT * shortenRate, -PI / 2, 15 * shortenRate, 1); // 递归函数调用
FlushBatchDraw(); // 批量绘制
}
int main()
{
startup(); // 初始化
while (1); // 重复循环
_getch();
return 0;
}
樱花树完整代码 .7z
需积分: 10 109 浏览量
2022-12-03
15:38:12
上传
评论
收藏 15.61MB 7Z 举报
吃什么香蕉
- 粉丝: 0
- 资源: 1