C++入门——绘制樱花树

参考

  1. 《C和C++游戏趣味编程》

樱花树

通过鼠标交互设定樱花树的高度和分散程度,鼠标右键点击设置是否显示过程动画,鼠标左键点击开始绘制

在这里插入图片描述

绘制过程抽象

(1)绘制一个树干
(2)绘制其左边的子树干,绘制其右边的子树干
(3)当到第n代树干时停止生成子树干

在这里插入图片描述

#include <graphics.h>
#include <conio.h>
#include <stdio.h>
#include <math.h>
#define PI 3.1415926
#define WIDTH 800          // 画面宽度
#define HEIGHT 600         // 画面高度

void branch(float x_start, float y_start, float angle, int generation)
{
	float x_end, y_end;
	x_end = x_start + 150 * cos(angle);
	y_end = y_start + 150 * sin(angle);

	line(x_start, y_start, x_end, y_end);

	int childGeneration = generation + 1;   // 子枝干的代数
	if (childGeneration <= 4)               // 当子枝干代数小于等于4时,产生子枝干
	{
		branch(x_end, y_end, angle + PI / 6, childGeneration);
		branch(x_end, y_end, angle - PI / 6, childGeneration);
	}
}

int main()
{
	initgraph(WIDTH, HEIGHT);
	setbkcolor(RGB(255, 255, 255));
	setlinecolor(RGB(0, 0, 0));
	setlinestyle(PS_SOLID, 3);
	cleardevice();
	BeginBatchDraw();
	branch(WIDTH / 2, HEIGHT, -PI / 2, 1);
	FlushBatchDraw();
	_getch();
	return 0;
}

进一步,使子枝干的长度逐渐变短,画线逐渐变细:

在这里插入图片描述

#include <graphics.h>
#include <conio.h>
#include <stdio.h>
#include <math.h>
#define PI 3.1415926
#define WIDTH 800              // 画面宽度
#define HEIGHT 600             // 画面高度

float offsetAngle = PI / 6;    // 左右枝干和父枝干偏离的角度
float shortenRate = 0.65;      // 左右枝干长度与父枝干长度的比例

void branch(float x_start, float y_start, float length, float angle, float thickness, int generation)
{
	float x_end, y_end;
	x_end = x_start + length * cos(angle);
	y_end = y_start + length * sin(angle);

	setlinestyle(PS_SOLID, thickness);      // 设定当前枝干线宽
	setlinecolor(RGB(0, 0, 0));
	line(x_start, y_start, x_end, y_end);

	int childGeneration = generation + 1;   // 子枝干的代数
	float childLength = shortenRate * length; // 子枝干的长度逐渐变短

	if (childLength >= 2 && childGeneration <= 9)               // 当子枝干长度大于2,且代数小于等于9,绘制子枝干
	{
		float childThickness = thickness * 0.8;                 // 枝干逐渐变细
		if (childThickness < 2)
		{
			childThickness = 2;
		}

		branch(x_end, y_end, childLength, angle + offsetAngle, childThickness, childGeneration);
		branch(x_end, y_end, childLength, angle - offsetAngle, childThickness, childGeneration);
	}
	else
	{
		setlinestyle(PS_SOLID, 1);
		setlinecolor(HSVtoRGB(325, 0.3, 1));                    // 设定线条颜色为粉色
		setfillcolor(HSVtoRGB(325, 0.3, 1));                    // 设定填充颜色为粉色
		if (childLength <= 4)
		{
			fillcircle(x_end, y_end, 2);
		}
		else
		{
			fillcircle(x_end, y_end, childLength / 2);
		}
	}
}

int main()
{
	initgraph(WIDTH, HEIGHT);
	setbkcolor(RGB(255, 255, 255));
	setlinecolor(RGB(0, 0, 0));
	setlinestyle(PS_SOLID, 3);
	cleardevice();
	BeginBatchDraw();
	branch(WIDTH / 2, HEIGHT, 0.45 * HEIGHT * shortenRate, -PI / 2, 15 * shortenRate, 1);
	FlushBatchDraw();
	_getch();
	return 0;
}

绘制樱花树

当子枝干长度小于2,且代数大于9时,绘制一个粉色填充圆,表示樱花:

setlinestyle(PS_SOLID, 1);
setlinecolor(HSVtoRGB(325, 0.3, 1));                    // 设定线条颜色为粉色
setfillcolor(HSVtoRGB(325, 0.3, 1));                    // 设定填充颜色为粉色
if (childLength <= 4)
{
	fillcircle(x_end, y_end, 2);
}
else
{
	fillcircle(x_end, y_end, childLength / 2);
}

再添加一个中间子枝干,使樱花树更稠密:

branch(x_end, y_end, childLength, angle, childThickness, childGeneration);

修改update(),当鼠标移动时调整递归函数参数,鼠标左键点击时绘制一棵新的樱花树:

if (e.message == WM_MOUSEMOVE)
{
	offsetAngle = mapValue(e.x, 0, WIDTH, PI / 10, PI / 4);      // 鼠标从左到右移动,左右枝干偏离父枝干的角度逐渐变大
	shortenRate = mapValue(e.y, 0, HEIGHT, 0.7, 0.3);            // 鼠标从上到下移动,子枝干的长度比父枝干的长度缩短得更快
}
if (e.message == WM_LBUTTONDOWN)
{
	cleardevice();
	branch(WIDTH / 2, HEIGHT, 0.45 * HEIGHT * shortenRate, -PI / 2, 15 * shortenRate, 1);
	FlushBatchDraw();
}

引入一些随机性,首先是子枝干的长度逐渐变短的随机性:

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);

然后是产生左、右、中的子枝干的随机性:

if (randBetween(0, 1) < 0.95)
{
	branch(x_end, y_end, leftChildLength, angle + offsetAngle * randBetween(0.5, 1), childThickness, childGeneration);
}
if (randBetween(0, 1) < 0.95)
{
	branch(x_end, y_end, childLength, angle - offsetAngle * randBetween(0.5, 1), childThickness, childGeneration);
}
if (randBetween(0, 1) < 0.85)
{
	branch(x_end, y_end, centerChildLength, angle + offsetAngle / 5 * randBetween(-1, 1), childThickness, childGeneration);
}

接着是树枝越向上,颜色越淡:

COLORREF color = HSVtoRGB(15, 0.75, 0.4 + generation * 0.05); // 枝干颜色

最后是樱花颜色的随机性:

COLORREF color = HSVtoRGB(randBetween(300, 350), randBetween(0.2, 0.3), 1);
setlinecolor(color);
setfillcolor(color);

显示绘制过程动画

设置全局变量isShowAnimation,在branch()中判断,如果isShowAnimation为1,则调用FlushBatchDraw()绘制:

if (isShowAnimation)
{
	FlushBatchDraw();
	Sleep(20);
}

在update()中添加代码,通过点击鼠标右键切换是否显示中间绘制动画:

if (e.message == WM_RBUTTONDOWN)
{
	if (isShowAnimation == 1)
	{
		isShowAnimation = 0;
	}
	else if (isShowAnimation == 1)
	{
		isShowAnimation = 1;
	}
}

完整代码

#include <graphics.h>
#include <conio.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
#define PI 3.1415926
#define WIDTH 800              // 画面宽度
#define HEIGHT 600             // 画面高度

float offsetAngle = PI / 6;    // 左右枝干和父枝干偏离的角度
float shortenRate = 0.65;      // 左右枝干长度与父枝干长度的比例
int isShowAnimation = 1;       // 是否显示树生成过程动画

float mapValue(float input, float inputMin, float inputMax, float outputMin, float outputMax)
{
	float output;
	if (fabs(input - inputMin) < 0.000001)   // 防止除0
	{
		output = outputMin;
	}
	else
	{
		output = (input - inputMin) * (outputMax - outputMin) / (inputMax - inputMin) + outputMin;
	}
	return output;
}

float randBetween(float min, float max)
{
	float t = rand() / double(RAND_MAX);      // 生成[0, 1]的随机小数
	float r = mapValue(t, 0, 1, min, max);
	return r;
}

void branch(float x_start, float y_start, float length, float angle, float thickness, int generation)
{
	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;   // 子枝干的代数
	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);

	if (childLength >= 2 && childGeneration <= 9)               // 当子枝干长度大于2,且代数小于等于9,绘制子枝干
	{
		float childThickness = thickness * 0.8;                 // 枝干逐渐变细
		if (childThickness < 2)
		{
			childThickness = 2;
		}

		if (randBetween(0, 1) < 0.95)
		{
			branch(x_end, y_end, leftChildLength, angle + offsetAngle * randBetween(0.5, 1), childThickness, childGeneration);
		}
		if (randBetween(0, 1) < 0.95)
		{
			branch(x_end, y_end, childLength, angle - offsetAngle * randBetween(0.5, 1), childThickness, childGeneration);
		}
		if (randBetween(0, 1) < 0.85)
		{
			branch(x_end, y_end, centerChildLength, angle + offsetAngle / 5 * randBetween(-1, 1), childThickness, childGeneration);
		}
	}
	else
	{
		setlinestyle(PS_SOLID, 1);
		COLORREF color = HSVtoRGB(randBetween(300, 350), randBetween(0.2, 0.3), 1);
		setlinecolor(color);
		setfillcolor(color);
		if (childLength <= 4)
		{
			fillcircle(x_end, y_end, 2);
		}
		else
		{
			fillcircle(x_end, y_end, childLength / 2);
		}
	}

	if (isShowAnimation)
	{
		FlushBatchDraw();
		Sleep(20);
	}
}

void startup()
{
	srand(time(0));
	initgraph(WIDTH, HEIGHT);
	setbkcolor(RGB(255, 255, 255));
	cleardevice();
	BeginBatchDraw();
	branch(WIDTH / 2, HEIGHT, 0.45 * HEIGHT * shortenRate, -PI / 2, 15 * shortenRate, 1);
	FlushBatchDraw();
}

void update()
{
	ExMessage e;
	if (peekmessage(&e))
	{
		if (e.message == WM_MOUSEMOVE)
		{
			offsetAngle = mapValue(e.x, 0, WIDTH, PI / 10, PI / 4);      // 鼠标从左到右移动,左右枝干偏离父枝干的角度逐渐变大
			shortenRate = mapValue(e.y, 0, HEIGHT, 0.7, 0.3);            // 鼠标从上到下移动,子枝干的长度比父枝干的长度缩短得更快
		}
		if (e.message == WM_LBUTTONDOWN)
		{
			cleardevice();
			branch(WIDTH / 2, HEIGHT, 0.45 * HEIGHT * shortenRate, -PI / 2, 15 * shortenRate, 1);
			FlushBatchDraw();
		}
		if (e.message == WM_RBUTTONDOWN)
		{
			if (isShowAnimation == 1)
			{
				isShowAnimation = 0;
			}
			else if (isShowAnimation == 1)
			{
				isShowAnimation = 1;
			}
		}
	}
}
int main()
{
	startup();
	while (1)
	{
		update();
	}
	return 0;
}
  • 19
    点赞
  • 122
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
贪心算法是一种问题求解方法,它在每一步总是做出当前情况下的最优选择,以期望获得最优解。而"最大整数"同样可以使用贪心算法来求解。 对于"最大整数"的问题,我们可以考虑如下的贪心策略:从高位开始,尽可能选择较大的数字。具体步骤如下: 1. 对于给定的整数,我们首先将其转化为一个数组,其中每个元素表示整数的一个位数。 2. 从最高位(最左侧)开始,遍历数组。 3. 对于当前位上的数字,从9开始递减,找到第一个小于等于当前数字的最大数字。 4. 如果找到了符合条件的最大数字,将其放在当前位。否则,不做任何操作。 5. 继续向下遍历,重复步骤3-4。 6. 最终,得到的数组即为满足条件的最大整数。 以一个具体的例子说明上述算法:假设给定的整数为5372。 1. 将整数转化为数组[5, 3, 7, 2]。 2. 从最高位开始遍历。 3. 对于第一位5,从9开始递减,找到第一个小于等于5的数字,为7。 4. 将7放在第一位,得到[7, 3, 7, 2]。 5. 对于第二位3,从9开始递减,找到第一个小于等于3的数字,为3(与当前数字相等)。 6. 不做任何操作,得到[7, 3, 7, 2]。 7. 对于第三位7,从9开始递减,找到第一个小于等于7的数字,为7。 8. 将7放在第三位,得到[7, 3, 7, 2]。 9. 对于第四位2,从9开始递减,找到第一个小于等于2的数字,为2。 10. 将2放在第四位,得到[7, 3, 7, 2]。 11. 遍历结束,最终得到的数组为[7, 3, 7, 2],转化为整数为7372。 通过上述贪心算法,我们得到了满足条件的最大整数7372。证明了贪心算法在"最大整数"问题中的有效性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值