南通网站建设计划书,免费自助建站,网站百度推广方案,seo推广优化多少钱简介
在计算机图形学的世界中#xff0c;有很多方法可以使程序的界面更加吸引人。在本篇博客中#xff0c;我将向大家介绍如何使用 EasyX 图形库在 C 中创建一个动态的爱心背景。这不仅是一个简单的动画效果#xff0c;它还包括背景的星星、旋转的心形以及一个美观的背景渐…简介
在计算机图形学的世界中有很多方法可以使程序的界面更加吸引人。在本篇博客中我将向大家介绍如何使用 EasyX 图形库在 C 中创建一个动态的爱心背景。这不仅是一个简单的动画效果它还包括背景的星星、旋转的心形以及一个美观的背景渐变。 工具介绍EasyX 图形库
EasyX 是一个简单易用的 C 图形库特别适合初学者和那些想要快速为其应用程序添加图形的开发者。它提供了一系列函数可以帮助你绘制形状、设置颜色和实现动画效果。 设计目标
我们的目标是创建一个动态的爱心它会在背景中旋转。背景将有一个从深空蓝渐变到黑色的效果并散布有颜色各异的小星星。
图片展示 开始编码
定义星星的结构
每颗星星都有其坐标、颜色和亮度。我们使用一个 struct 来表示
struct Star {int x, y;COLORREF color;float intensity;float intensityChange;
};3.2 心形绘制函数
我们使用参数方程来绘制心形并为它添加了旋转效果
void DrawHeart(int x, int y, COLORREF color, float scale, float angle) {BeginBatchDraw(); // 开始批量绘制const int thickness 2; // 调整这个值来改变心形的粗细for (float t 0; t 2 * 3.14159; t 0.01) {float x_cord scale * (16 * pow(sin(t), 3));float y_cord scale * -(13 * cos(t) - 5 * cos(2 * t) - 2 * cos(3 * t) - cos(4 * t));// 旋转float rotatedX x_cord * cos(angle) - y_cord * sin(angle);float rotatedY x_cord * sin(angle) y_cord * cos(angle);for (int dx -thickness; dx thickness; dx) {for (int dy -thickness; dy thickness; dy) {putpixel(x rotatedX dx, y rotatedY dy, color);}}}EndBatchDraw(); // 结束批量绘制并显示在前台
}
主函数逻辑
在 main() 函数中我们首先初始化 EasyX 的图形窗口。接着我们随机生成一组星星并存储它们的属性。在主循环中我们绘制背景渐变、星星和心形并实现心形的动态效果。 源代码分享
#include graphics.h
#include conio.h
#include math.h
#include vectorstruct Star {int x, y;COLORREF color;float intensity;float intensityChange;
};// 修改后的心形公式函数
void DrawHeart(int x, int y, COLORREF color, float scale, float angle) {BeginBatchDraw(); // 开始批量绘制const int thickness 2; // 调整这个值来改变心形的粗细for (float t 0; t 2 * 3.14159; t 0.01) {float x_cord scale * (16 * pow(sin(t), 3));float y_cord scale * -(13 * cos(t) - 5 * cos(2 * t) - 2 * cos(3 * t) - cos(4 * t));// 旋转float rotatedX x_cord * cos(angle) - y_cord * sin(angle);float rotatedY x_cord * sin(angle) y_cord * cos(angle);for (int dx -thickness; dx thickness; dx) {for (int dy -thickness; dy thickness; dy) {putpixel(x rotatedX dx, y rotatedY dy, color);}}}EndBatchDraw(); // 结束批量绘制并显示在前台
}int main() {// 初始化图形窗口initgraph(640, 480);setbkcolor(BLACK); // 设置背景色为黑色cleardevice(); // 清空屏幕// 创建星星const int numStars 100;std::vectorStar stars;for (int i 0; i numStars; i) {Star star {rand() % 640,rand() % 480,RGB(rand() % 256, rand() % 256, rand() % 256),(rand() % 100) / 100.0f,(rand() % 5 1) / 500.0f};stars.push_back(star);}float scale 10;bool increase true;COLORREF heartColor RED;float angle 0;BeginBatchDraw(); // 开始批量绘制while (!_kbhit()) { // 直到有键被按下cleardevice(); // 清空屏幕// 绘制渐变背景for (int i 0; i 480; i) {float ratio (float)i / 480;COLORREF bgColor RGB(0, 0, ratio * 50);setlinecolor(bgColor);line(0, i, 640, i);}// 绘制星星for (auto star : stars) {star.intensity star.intensityChange;if (star.intensity 1 || star.intensity 0.5) {star.intensityChange -star.intensityChange;}COLORREF modifiedColor RGB(GetRValue(star.color) * star.intensity, GetGValue(star.color) * star.intensity, GetBValue(star.color) * star.intensity);putpixel(star.x, star.y, modifiedColor);}if (increase) {scale 0.1;}else {scale - 0.1;}if (scale 15 || scale 10) {increase !increase;}// 改变心的颜色int r GetRValue(heartColor);int g GetGValue(heartColor);int b GetBValue(heartColor);r (r 1) % 256;g (g 2) % 256;b (b 3) % 256;heartColor RGB(r, g, b);DrawHeart(320, 240, heartColor, scale, angle);angle 0.005; // 调整这个值来改变旋转速度Sleep(10);EndBatchDraw(); // 结束批量绘制并显示在前台}closegraph();return 0;
}结果
当你运行上述代码时你将看到一个美丽的动态背景上面有一个不断旋转的爱心和一群闪烁的星星。通过调整参数你可以轻松改变动画的速度、颜色和其他属性。 参考资源:
EasyX 官方文档心形参数方程