网站建设各单位强化沟通协作,郑州做网站的企业,销售网站建设公司,国内站长做国外网站目录
写在前面
系列文章
C简介
完整代码
代码分析
写在后面 写在前面
C实现精美的樱花树#xff0c;只需这100行代码#xff01;
系列文章
序号目录直达链接1爱心代码https://want595.blog.csdn.net/article/details/1363606842李峋同款跳动的爱心https://want595.b…
目录
写在前面
系列文章
C简介
完整代码
代码分析
写在后面 写在前面
C实现精美的樱花树只需这100行代码
系列文章
序号目录直达链接1爱心代码https://want595.blog.csdn.net/article/details/1363606842李峋同款跳动的爱心https://want595.blog.csdn.net/article/details/1397222493满屏飘字代码https://want595.blog.csdn.net/article/details/1363424764大雪纷飞代码5新春烟花代码6黑客帝国字母雨https://want595.blog.csdn.net/article/details/1399237427樱花树https://want595.blog.csdn.net/article/details/140690893
C简介
C是一种通用编程语言特点是高效、灵活、强大和可移植性强。它是从C语言发展而来的但在语法和功能上有很多扩展和改进。C支持面向对象编程允许使用类和对象来封装数据和功能并通过继承、多态等技术实现代码的重用和扩展性。与C语言相比C还引入了一些特性如模板和异常处理以提供更高级的编程能力。C被广泛应用于系统开发、游戏开发、图形界面开发等领域。
完整代码
#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) {return (input - inputMin) * (outputMax - outputMin) / (inputMax - inputMin) outputMin;
}float randBetween(float min, float max) {return mapValue(rand() / (double)RAND_MAX, 0, 1, min, max);
}void drawBranch(float x_start, float y_start, float length, float angle, float thickness, int generation) {float x_end x_start length * cos(angle);float y_end y_start length * sin(angle);setlinestyle(PS_SOLID, (int)thickness);COLORREF color HSVtoRGB(15, 0.75, 0.4 generation * 0.05);setlinecolor(color);line(x_start, y_start, x_end, y_end);if (length 2 || generation 9) {setlinestyle(PS_SOLID, 1);color HSVtoRGB(randBetween(300, 350), randBetween(0.2, 0.3), 1);setlinecolor(color);setfillcolor(color);fillcircle(x_end, y_end, length 4 ? 2 : length / 2);return;}float childLength shortenRate * length;float childThickness thickness * 0.8;childThickness childThickness 2 ? 2 : childThickness;int childGeneration generation 1;if (randBetween(0, 1) 0.95)drawBranch(x_end, y_end, childLength * randBetween(0.9, 1.1), angle offsetAngle * randBetween(0.5, 1), childThickness, childGeneration);if (randBetween(0, 1) 0.95)drawBranch(x_end, y_end, childLength * randBetween(0.9, 1.1), angle - offsetAngle * randBetween(0.5, 1), childThickness, childGeneration);if (randBetween(0, 1) 0.85)drawBranch(x_end, y_end, childLength * randBetween(0.8, 1.1), angle offsetAngle / 5 * randBetween(-1, 1), childThickness, childGeneration);if (isShowAnimation) {FlushBatchDraw();Sleep(0);}
}void startup() {srand(time(0));initgraph(WIDTH, HEIGHT);setbkcolor(RGB(255, 192, 203));cleardevice();BeginBatchDraw();drawBranch(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();drawBranch(WIDTH / 2, HEIGHT, 0.45 * HEIGHT * shortenRate, -PI / 2, 15 * shortenRate, 1);FlushBatchDraw();}if (e.message WM_RBUTTONDOWN) {isShowAnimation !isShowAnimation;}}
}int main() {startup();while (1) {update();}return 0;
}
代码分析
这段代码是一个利用图形库绘制樱花树的程序。它使用了C语言的图形库和一些数学函数来实现绘制效果。
主要函数
mapValue函数用于将一个输入值映射到指定区间的输出值。randBetween函数用于生成一个指定范围内的随机数。drawBranch函数用于绘制樱花树的一条分支。startup函数用于初始化绘图环境和绘制初始的樱花树。update函数用于根据用户的鼠标操作更新樱花树的参数。
代码中定义了一些常量和参数如PI表示圆周率WIDTH和HEIGHT表示窗口的宽度和高度offsetAngle表示分支的偏移角度shortenRate表示分支的缩短比例isShowAnimation表示是否展示动画效果。
代码中还使用了一些辅助函数如mapValue函数用于将一个值从一个范围映射到另一个范围randBetween函数用于生成一个指定范围内的随机数。
drawBranch函数是绘制樱花树的核心函数它使用递归的方式绘制树的各个分支。参数x_start和y_start表示起始点的坐标length表示分支的长度angle表示分支的角度thickness表示分支的粗细generation表示分支的代数。通过计算得到分支的终点坐标并根据参数设置绘制直线并根据长度和代数判断是否绘制叶子。
在drawBranch函数中还使用了三次递归调用来绘制下一级的分支每次绘制分支时根据随机数和参数设置分支的长度、角度和粗细。同时drawBranch函数还可以根据全局变量isShowAnimation来控制是否展示动画效果。
startup函数是程序的初始化函数其中调用了initgraph函数初始化图形窗口并进行一些初始化设置如设置背景色、清空画面、开启批量绘制模式。然后调用drawBranch函数绘制主干分支并刷新屏幕。
update函数是程序的更新函数通过调用peekmessage函数来获取鼠标事件根据不同的事件来更新樱花树的参数或重新绘制树。其中鼠标移动事件会改变offsetAngle和shortenRate的值左键点击事件会重新绘制树右键点击事件会切换是否展示动画效果。
main函数是程序的入口函数其中调用了startup函数进行初始化然后进入一个无限循环中不断调用update函数来更新程序的状态。
写在后面
我是一只可爱的兔子感谢你的喜欢