Dotcpp  >  编程教程  >  C/C++工具及其他类项目  >  C语言画国庆节国旗教程及源码

C语言画国庆节国旗教程及源码

点击打开在线编译器,边学边练

一、项目介绍

这是一个C语言绘制中国国旗的程序,为祖国母亲庆生!

编译环境:visual c++ 6.0

第三方库:Easyx2022  注意需要提前安装easyX,如没有基础可以先了解easyX图形编程


二、运行截图

添加红幕

添加五星

添加祝福语


三、代码思路

1.引入easyx头文件

#include <easyx.h>

2.宏定义

#define PI 3.14

3.角星的外接圆半径和起始角度作为参数

void fivePointedStar(int radius, double startAngle)
{
    double delta = 2 * PI / 5;      //  增量为一个圆的5分之一
    POINT points[5];                //  长度为5的POINT数组,用于存储5个点
    for (int i = 0; i < 5; i++)
    {
        points[i].x = cos(startAngle + i * delta * 2) * radius;   //  计算x坐标 
        points[i].y = sin(startAngle + i * delta * 2) * radius;   //  计算y坐标
    }
    solidpolygon(points, 5);
}

4.主函数

int main(void)
{
    int width = 900;
    int height = width / 3 * 2;    //  高度为宽度的2/3
    int grid = width / 3 / 15;    //  网格宽度
    initgraph(800, 420);    //  创建窗体设置背景色
    setbkcolor(BLACK);
    cleardevice();
setcolor(YELLOW);  //  文本颜色
setbkcolor(BLACK);  //  文本背景色
settextstyle(100,0,"楷体");  //  文本高度和字体
outtextxy(600, 10, "喜");  //  文本位置和内容
outtextxy(600, 110, "迎");  //  文本位置和内容
outtextxy(600, 210, "国");  //  文本位置和内容
outtextxy(600, 310, "庆");  //  文本位置和内容
outtextxy(700, 10, "举");  //  文本位置和内容
outtextxy(700, 110, "国");  //  文本位置和内容
outtextxy(700, 210, "欢");  //  文本位置和内容
outtextxy(700, 310, "庆");  //  文本位置和内容
setcolor(YELLOW);
setbkcolor(BLACK);
settextstyle(20,0,"楷体");
outtextxy(650, 400, "Dotcpp.com");
setfillcolor(RED);
solidrectangle(10,10,600,400);
    setaspectratio(1, -1);    //  翻转坐标轴,设置填充颜色为黄色
    setfillcolor(YELLOW);
    setpolyfillmode(WINDING);
    getchar();
    closegraph();
    return 0;
}

完成


四、完整源码

#include <stdio.h>
#include <easyx.h>
#include <math.h>
#include <graphics.h>        // 引用图形库头文件
#include <conio.h>
#include<time.h>
#define PI 3.14
void fivePointedStar(int radius, double startAngle)//  角星的外接圆半径和起始角度作为参数,由调用者决定
{
    double delta = 2 * PI / 5;      //  增量为一个圆的5分之一
    POINT points[5];                //  长度为5的POINT数组,用于存储5个点
    for (int i = 0; i < 5; i++)
    {
        points[i].x = cos(startAngle + i * delta * 2) * radius;   //  计算x坐标 
        points[i].y = sin(startAngle + i * delta * 2) * radius;   //  计算y坐标
    }
    solidpolygon(points, 5);
}
int main(void)
{
    int width = 900;
    int height = width / 3 * 2;    //  高度为宽度的2/3
    int grid = width / 3 / 15;    //  网格宽度
    initgraph(800, 420);    //  创建窗体设置背景色
    setbkcolor(BLACK);
    cleardevice();
setcolor(YELLOW);  //  文本颜色
setbkcolor(BLACK);  //  文本背景色
settextstyle(100,0,"楷体");  //  文本高度和字体
outtextxy(600, 10, "喜");  //  文本位置和内容
outtextxy(600, 110, "迎");  //  文本位置和内容
outtextxy(600, 210, "国");  //  文本位置和内容
outtextxy(600, 310, "庆");  //  文本位置和内容
outtextxy(700, 10, "举");  //  文本位置和内容
outtextxy(700, 110, "国");  //  文本位置和内容
outtextxy(700, 210, "欢");  //  文本位置和内容
outtextxy(700, 310, "庆");  //  文本位置和内容
setcolor(YELLOW);
setbkcolor(BLACK);
settextstyle(20,0,"楷体");
outtextxy(650, 400, "Dotcpp.com");
    setfillcolor(RED);
solidrectangle(10,10,600,400);
    setaspectratio(1, -1);    //  翻转坐标轴,设置填充颜色为黄色
    setfillcolor(YELLOW);
    setpolyfillmode(WINDING);
    setorigin(grid * 5, grid * 5);    //  大五角星
    fivePointedStar(grid * 3, PI / 2);
    setorigin(grid * 10, grid * 2);    //  小五角星1
    double startAngle = atan(3.0 / 5.0) + PI;
    fivePointedStar(grid, startAngle);
    setorigin(grid * 12, grid * 4);    //  小五角星2
    startAngle = atan(1.0 / 7.0) + PI;
    fivePointedStar(grid, startAngle);
    setorigin(grid * 12, grid * 7);    //  小五角星3
    startAngle = -atan(2.0 / 7.0) + PI;
    fivePointedStar(grid, startAngle);
    setorigin(grid * 10, grid * 9);    //  小五角星4
    startAngle = -atan(4.0 / 5.0) + PI;
    fivePointedStar(grid, startAngle);
    getchar();
    closegraph();
    return 0;
}



本文固定URL:https://www.dotcpp.com/course/1262

趣味项目教程
第一章 C/C++游戏类项目
第二章 C/C++工具及其他类项目
第三章 Python趣味项目
Dotcpp在线编译      (登录可减少运行等待时间)