国庆,国旗,小兔子

教育   2024-10-02 02:45   宁夏  

今天使用C语言调用EasyX图形库绘制一只小兔子。

难点在于这个小兔子背景的五角红旗,用五星国旗为背景,这里肯定不能弄错了。

中华人民共和国国旗法:中华人民共和国国旗是五星红旗。中华人民共和国国旗是中华人民共和国的象征和标志。每个公民和组织,都应当尊重和爱护国旗。

先展示一下效果:

源码:

/////////////////////////////////////////////////// 程序名称:绘制小兔子// 编译环境:Mictosoft Visual Studio 2013, EasyX_20200315(beta)// 作  者:luoyh <2864292458@qq.com><vx:luoyh1207>// 最后修改:2023-9-6
#include<graphics.h>#include<conio.h>#include<math.h>
#define PI acos(-1.0)#define WIDTH 480#define HEIGHT 800double th = PI / 180;#define CELL WIDTH / 20 // 一个小格子的大小
struct star_p // 五角星的结构体{ int x; // x 位置 int y; // y 位置 int r; // 半径 double angle; // 旋转角度};void DrawStar(star_p star); // 绘制五角星double GetAngle(int x1, int y1, int x2, int y2); // 计算方位角
// 绘制背景void DrawBK(); // 绘制背景void DrawYZ(); // 绘制影子void DarwEDMZED(); // 绘制耳朵帽子耳朵void DarwSZ(); // 绘制身子void DrawTOU(); // 绘制头void DrawXJ(); // 添加一点细节// 一些可以重复调用的图形void DrawEllipse(int x0, int y0, int a, int b, int k, COLORREF color); // 绘制倾斜椭圆
int main(){ initgraph(WIDTH, HEIGHT); DrawBK(); // 绘制背景 DrawYZ(); // 绘制影子 DarwEDMZED(); DarwSZ(); DrawTOU(); DrawXJ(); _getch(); return 0;}
void DrawBK() // 绘制背景{ setbkcolor(RGB(254, 5, 1)); cleardevice(); star_p Star[5];
Star[0].x = 5 * CELL; Star[0].y = 5 * CELL; Star[0].r = 3 * CELL;
Star[1].x = 10 * CELL; Star[1].y = 2 * CELL; Star[1].r = 1 * CELL;
Star[2].x = 12 * CELL; Star[2].y = 4 * CELL; Star[2].r = 1 * CELL;
Star[3].x = 12 * CELL; Star[3].y = 7 * CELL; Star[3].r = 1 * CELL;
Star[4].x = 10 * CELL; Star[4].y = 9 * CELL; Star[4].r = 1 * CELL;
for (int i = 0; i < 5; i++) { Star[i].angle = GetAngle(Star[0].x, Star[0].y, Star[i].x, Star[i].y); DrawStar(Star[i]); }}void DrawYZ() // 绘制影子{ setfillcolor(BLACK); solidellipse(209,630,470,690);}void DarwEDMZED() // 绘制耳朵帽子耳朵{ // 右耳朵 setlinecolor(BLACK); setlinestyle(PS_SOLID,2); DrawEllipse(200, 318, 80, 40, 70, BLACK); setfillcolor(RGB(247,247,247)); floodfill(200,318,BLACK); DrawEllipse(200, 318, 40, 20, 70, RGB(246, 167, 173)); setfillcolor(RGB(246, 167, 173)); floodfill(200, 318, RGB(246, 167, 173)); // 绘制帽子 setfillcolor(RGB(88,132,56)); setlinecolor(BLACK); fillcircle(269,372,77); // 画五角星 star_p Mstar; Mstar.x = 264; Mstar.y = 315; Mstar.r = 18; Mstar.angle = 1.029; DrawStar(Mstar); setfillcolor(RGB(255, 48, 17)); floodfill(264, 317, RGB(88, 132, 56)); setfillcolor(RGB(88, 132, 56)); setlinecolor(BLACK); fillellipse(175,315,309,388);
// 左耳朵
DrawEllipse(348, 383, 105, 50, 40, RGB(0,1,0)); setfillcolor(RGB(247, 247, 247)); floodfill(348, 383, RGB(0, 1, 0)); DrawEllipse(348, 380, 55, 25, 40, RGB(246, 167, 173)); setfillcolor(RGB(246, 167, 173)); floodfill(348, 383, RGB(246, 167, 173)); /*fillcircle()*/}void DarwSZ() // 绘制身子{ setlinecolor(RGB(0,1,0)); setfillcolor(RGB(247,247,247)); fillellipse(250, 678, 323, 727); fillcircle(200,663,89); setlinecolor(RGB(0, 1, 0)); line(259,625,267,724); setlinecolor(RGB(0, 1, 0)); line(159,603,127,671);
star_p Mstar; Mstar.x = 180; Mstar.y = 658; Mstar.r = 40; Mstar.angle = -1.40; DrawStar(Mstar); setfillcolor(RGB(255,48,17)); floodfill(181, 658, RGB(247, 247, 247));
setlinecolor(RGB(0, 1, 0)); setfillcolor(RGB(247, 247, 247)); fillcircle(123, 708, 45); fillcircle(196, 712, 48);
setlinecolor(RGB(247,247,247)); setlinestyle(PS_SOLID, 4); arc(148, 664, 244, 760, 5.829, 0.47); arc(200 - 89, 663 - 89, 289, 663 + 89, 5.584, 6.069);}void DrawTOU() // 绘制头{ setlinestyle(PS_SOLID, 2); setlinecolor(RGB(0,2,1)); setfillcolor(RGB(247,247,247)); fillellipse(63, 334, 380, 596); setlinecolor(RGB(247, 247, 247)); setlinestyle(PS_SOLID, 4); arc(63, 334, 380, 596, 4.35, 4.90);}void DrawXJ() // 添加一点细节{ // 化眼睛 DrawEllipse(149,380,24,14,50,BLACK); setfillcolor(BLACK); floodfill(149, 380, BLACK);
DrawEllipse(283,396,23,15,55,BLACK); setfillcolor(BLACK); floodfill(283, 396, BLACK);
DrawEllipse(150, 369, 10, 4, 40, WHITE); setfillcolor(WHITE); floodfill(150, 369,WHITE); DrawEllipse(278, 390, 10, 4, 43, WHITE); setfillcolor(WHITE); floodfill(278, 390, WHITE); setlinecolor(BLACK); setlinestyle(PS_SOLID, 2); arc(168,421,208,460,0.697,2.87);}
void DrawEllipse(int x0, int y0, int a, int b, int k, COLORREF color){ double i; double x, y, tx, ty; for (i = -180; i <= 180; i = i + 0.5) { x = a * cos(i * th); y = b * sin(i * th); tx = x; ty = y; x = tx * cos(k * th) - ty * sin(k * th) + x0; y = y0 - (ty * cos(k * th) + tx * sin(k * th)); setfillcolor(color); solidcircle((int)x, (int)y, 1); }}void DrawStar(star_p star){ POINT pts[5]; star.angle = 2 * PI - star.angle; for (int i = 0; i < 5; i++) { pts[i].x = int(star.x + cos(star.angle) * star.r); pts[i].y = int(star.y - sin(star.angle) * star.r); star.angle += PI * 4 / 5; } setpolyfillmode(WINDING); setfillcolor(YELLOW); setlinecolor(YELLOW); solidpolygon(pts, 5);}
double GetAngle(int x1, int y1, int x2, int y2){ double DX = (x1 - x2) * 1.0; double DY = (y1 - y2) * 1.0; double aAB = atan((fabs)(DY) / (fabs)(DX)); if (DX > 0 && DY >= 0) { return aAB; } if (DX < 0 && DY >= 0) { return PI - aAB; } if (DX < 0 && DY < 0) { return PI + aAB; } if (DX > 0 && DY < 0) { return PI * 2 - aAB; } if (DX == 0 && DY > 0) { return PI / 2; } if (DX == 0 && DY <= 0) { return PI / 2 * 3; } return 0;}

C语言研究
写给自己的笔记,时常写写,时常看看,仅此而已。