景苒的博客

导航

基于easyx的小时钟

#include <graphics.h>
#include <math.h>
#include <conio.h>
#define PI 3.141592654
void Draw_Dial();
void Draw_Hand(int hour, int minute, int secend);
int main()
{
    initgraph(640, 480);
    Draw_Dial();
    setwritemode(R2_XORPEN);
    SYSTEMTIME time;
    while (!_kbhit())
    {
        GetLocalTime(&time);
        Draw_Hand(time.wHour, time.wMinute, time.wSecond);
        Sleep(1000);
        Draw_Hand(time.wHour, time.wMinute, time.wSecond);
    }
    closegraph();
    return 0;
}

void Draw_Dial()
{
    setcolor(GREEN);
    circle(320, 240, 160);
    circle(320, 240, 60);
    circle(320, 240, 2);
    setcolor(WHITE);
    int x, y;
    for (int i = 0; i < 60; i++)
    {
        x = 320 + int(145 * sin(PI * 2 * i / 60));
        y = 240 + int(145 * cos(PI * 2 * i / 60));
        if (i % 15 == 0)
            bar(x - 5, y - 5, x + 5, y + 5);
        else if (i % 5 == 0)
            circle(x, y, 3);
        else
            putpixel(x, y, WHITE);
    }
}

void Draw_Hand(int hour, int minute, int second)
{
    double h_hour, h_minute, h_second;
    int x_hour, y_hour, x_minute, y_minute, x_second, y_second;
    h_second = second * 2 * PI / 60;
    h_minute = minute * 2 * PI / 60 + h_second / 60;
    h_hour = hour * 2 * PI / 12 + h_minute / 12;
    x_second = int(120 * sin(h_second)); y_second = int(120 * cos(h_second));
    x_minute = int(100 * sin(h_minute)); y_minute = int(100 * cos(h_minute));
    x_hour = int(70 * sin(h_hour)); y_hour = int(70 * cos(h_hour));
    setlinestyle(PS_SOLID, 2);
    setcolor(RED);
    line(320 + x_second, 240 - y_second, 320 - x_second / 3, 240 + y_second / 3);
    setlinestyle(PS_SOLID, 6);
    setcolor(YELLOW);
    line(320 + x_minute, 240 - y_minute, 320 - x_minute / 5, 240 + y_minute / 5);
    setlinestyle(PS_SOLID, 7);
    setcolor(GREEN);
    line(320 + x_hour, 240 - y_hour, 320 - x_hour / 5, 240 + y_hour / 5);
}

 

 运行的时候需要时cpp文件运行,且需要easyx插件。

posted on 2022-03-24 20:50  景苒  阅读(106)  评论(0编辑  收藏  举报