C语言绘制时钟

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

C语言绘制时钟

云淡风轻ing   2022-06-19 我要评论

函数说明:

void line(
    int x1,
    int y1,
    int x2,
    int y2
);

参数

x1
直线的起始点的 x 坐标。

y1
直线的起始点的 y 坐标。

x2
直线的终止点的 x 坐标。

y2
直线的终止点的 y 坐标。

文件素材

源代码

#include <graphics.h>
#include <conio.h>
#include <math.h>
#define PI 3.1415926
int main()
{
    int high=500;
    int width=500;
    initgraph(width,high);        
    IMAGE img;                    
    loadimage(&img,"timg.jpg");        //加载图片
    putimage(0,0,&img);                //显示图片
    SYSTEMTIME ti;
    float angle_s = 0;                //秒针偏转角度
    float angle_m = 0;                //分针偏转角度
    float angle_h = 0;                //时针偏转角度
    BeginBatchDraw();
    outtextxy(width/2-30,10,"我的时钟");    //输出文字
    while(1)
    {
        GetLocalTime(&ti);                    //获得系统时间
        //根据系统时间获取时针、分针、秒针偏转角度
        angle_s = ti.wSecond*2*PI/60;        
        angle_m = ti.wMinute*2*PI/60;
        angle_h = ti.wHour*2*PI/12;
        //绘制秒针
        setcolor(RED);
        setlinestyle(PS_SOLID,2);
        line(width/2,high/2,width/2+120*sin(angle_s),high/2-120*cos(angle_s));
        setcolor(GREEN);
        //绘制分针
        setlinestyle(PS_SOLID,3);
        line(width/2,high/2,width/2+80*sin(angle_m),high/2-80*cos(angle_m));
        setcolor(BLACK);
        //绘制时针
        setlinestyle(PS_SOLID,4);
        line(width/2,high/2,width/2+50*sin(angle_h),high/2-50*cos(angle_h));
        FlushBatchDraw();
        //Sleep(50);
        //清除前一帧的绘图
        setcolor(WHITE);
        line(width/2,high/2,width/2+120*sin(angle_s),high/2-120*cos(angle_s));
        line(width/2,high/2,width/2+80*sin(angle_m),high/2-80*cos(angle_m));
        line(width/2,high/2,width/2+50*sin(angle_h),high/2-50*cos(angle_h));

    }
    EndBatchDraw();
    getch();
    closegraph();
    return 0;
}

效果:

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们