[C] 纯文本查看 复制代码
#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);//初始化一个图形界面 640*480
//circle(320,240,150);//画圆的函数
//rectangle(0, 0, 50, 50);//画正方体的函数
Draw_Dial();
setwritemode(R2_XORPEN);//设置XOR绘图模式
SYSTEMTIME time;//定义一个时间结构体 用来保存当前的时间
while (!_kbhit())
{
GetLocalTime(&time);//获取当前的时间
Draw_Hand(time.wHour,time.wMinute,time.wSecond); //把表针绘制出来
Sleep(1000);//间隔1秒 1000毫秒
Draw_Hand(time.wHour, time.wMinute, time.wSecond); //把表针绘制出来
}
closegraph();//关闭图形界面
return 0;
}
//绘制静态的表盘
void Draw_Dial()
{
//绘制边界
setcolor(GREEN);
circle(320, 240, 160);//画圆函数的参数 320 240坐标 160是圆的半径大小
circle(320, 240, 60);
circle(320, 240, 2);
//图形界面的输出文本信息 txt text 文字
outtextxy(260,420,L"GOOD GOOD STUDY");
outtextxy(283, 440, L"DAY DAY UP");
//line(0, 0, 639, 479);
//绘制刻度
//circle(320, 85, 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); //PS_SOILD 实线 2像素 线条的粗细
setcolor(RED);
line(320 + x_second, 240 - y_second, 320 - x_second / 3, 240 + y_second / 3);
//绘制分针
setlinestyle(PS_SOLID, 6); //PS_SOILD 实线 2像素 线条的粗细
setcolor(YELLOW);
line(320 + x_minute, 240 - y_minute, 320 - x_minute / 5, 240 + y_minute/5);
//绘制时针
setlinestyle(PS_SOLID, 7); //PS_SOILD 实线 2像素 线条的粗细
setcolor(GREEN);
line(320 + x_hour, 240 - y_hour, 320 - x_hour / 5, 240 + y_hour/5);
}