时钟

来源:互联网 发布:java set用法 编辑:程序博客网 时间:2024/05/21 01:56
#include<stdio.h>#include<graphics.h>#include<conio.h>#include<math.h>#define WIDTH 480#define HIGH 640#define PI 3.1415926int main(){    initgraph(WIDTH, HIGH);    int center_x = WIDTH / 2;    int center_y = HIGH / 2;    int second_length = WIDTH / 5;    int minute_length = WIDTH / 7;    int hour_length = WIDTH / 9;    int second_end_x, second_end_y;    int minute_end_x, minute_end_y;    int hour_end_x, hour_end_y;    double second_angle, minute_angle, hour_angle;    SYSTEMTIME time;    setbkcolor(WHITE); //设定背景颜色    cleardevice();    BeginBatchDraw();//一次性输出    while (1)    {        setlinestyle(PS_SOLID, 1);        setcolor(BLACK);        circle(center_x, center_y, WIDTH / 4);        int x, y, i;        for (i = 0; i < 60; i++)        {            x = center_x + int(WIDTH / 4.3*sin(PI * 2 * i / 60));            y = center_y+ int(WIDTH / 4.3*cos(PI * 2 * i / 60));            if (x % 15 == 0)                bar(x - 5, y - 5, x + 5, y + 5);             if (i % 5 == 0)                circle(x, y, 3);            else putpixel(x, y, BLUE);        }        GetLocalTime(&time);        second_angle = time.wSecond * 2 * PI / 60;        minute_angle = time.wMinute * 2 * PI / 60 + second_angle/60;        hour_angle = time.wHour * 2 * PI / 12 + minute_angle/12;        minute_end_x = center_x + minute_length*sin(minute_angle);        minute_end_y = center_y - minute_length*cos(minute_angle);        second_end_x = center_x + second_length*sin(second_angle);        second_end_y = center_y - second_length*cos(second_angle);        hour_end_x = center_x + hour_length*sin(hour_angle);        hour_end_y = center_y - hour_length*cos(hour_angle);        setlinestyle(PS_SOLID, 2);        setcolor(YELLOW);        line(center_x, center_y, second_end_x, second_end_y);        setlinestyle(PS_SOLID, 4);        setcolor(BLUE);        line(center_x, center_y, minute_end_x, minute_end_y);        setlinestyle(PS_SOLID, 6);        setcolor(RED);        line(center_x, center_y, hour_end_x, hour_end_y);        FlushBatchDraw();        Sleep(10); //sleep函数不控制转动速度,其受到SYSTEMTIME控制,sleep只是对指针出现的速度有微调        //隐藏上一秒的指针        setcolor(WHITE);        setlinestyle(PS_SOLID, 2);        line(center_x, center_y, second_end_x, second_end_y);        setlinestyle(PS_SOLID, 5);        line(center_x, center_y, minute_end_x, minute_end_y);        setlinestyle(PS_SOLID, 10);        line(center_x, center_y, hour_end_x, hour_end_y);    }    EndBatchDraw();    _getch();    closegraph();    return 0;}
原创粉丝点击