#include <graphics.h>
#include <conio.h>
#include <math.h>
#include <time.h>
#define high 480
#define width 640
#define pi 3.141592
int main()
{
initgraph(high, width);
int center_x, center_y;
center_y = width / 2; // 320
center_x = high / 2; // 240
SYSTEMTIME ti; //获取系统的时间
// 秒针属性
int secondEnd_x, secondEnd_y;
int secondLenth = 120;
secondEnd_x = center_x + secondLenth;
secondEnd_y = center_y;
float secondAngle = 0;
// 分钟属性
int minuteEnd_x, minuteEnd_y;
float minuteAngle = 0;
int minuteLenth = 90;
minuteEnd_x = center_x + minuteLenth;
minuteEnd_y = center_y + minuteLenth;
// 时钟属性
int hoursEnd_x, hoursEnd_y;
float hoursAngle = 0;
int hoursLenth = 60;
hoursEnd_x = center_x + hoursLenth;
hoursEnd_y = center_y + hoursLenth;
BeginBatchDraw();
while (1)
{
// 获取时间
GetLocalTime(&ti);
//绘制中心坐标
//setlinecolor(WHITE);
//fillcircle(center_x, center_y, 2);
// 绘制一个表盘
setbkcolor(BLACK);
setlinestyle(PS_SOLID, 1);
setlinecolor(WHITE);
circle(center_x, center_y, 130);
//outtextxy(center_x - 25, center_y + width / 6, "我的时钟");
// 输出字符串 (VC6)
TCHAR s[] = _T("我的时钟");
outtextxy(210, 400, s);
// 绘制表盘刻度
int x, y, i;
for (i = 0; i < 60; i++)
{
x
= center_x
+ 125 * sin(i
* 2 * pi
/ 60);
y
= center_y
- 125 * cos(i
* 2 * pi
/ 60);
// 一刻钟
if (i % 15 == 0)
{
bar(x - 5, y - 5, x + 5, y + 5);
}
else if ( i % 5 == 0) //5分钟
{
circle(x, y, 3);
}
else
{
putpixel(x, y, WHITE); // 小白点
}
}
//转动秒针
secondAngle = ti.wSecond * 2 * pi / 60; // 2 * pi / 60 =一秒钟走的角度 ti.wSecond =系统当前秒
secondEnd_x
= center_x
+ secondLenth
* sin(secondAngle
);
secondEnd_y
= center_y
- secondLenth
* cos(secondAngle
);
//转动分钟
minuteAngle = ti.wMinute * 2 * pi / 60 + secondAngle / 60;
minuteEnd_x
= center_x
+ minuteLenth
* sin(minuteAngle
);
minuteEnd_y
= center_y
- minuteLenth
* cos(minuteAngle
);
//转动时钟
hoursAngle = ti.wHour * 2 * pi / 12 + minuteAngle / 60;
hoursEnd_x
= center_x
+ hoursLenth
* sin(hoursAngle
);
hoursEnd_y
= center_y
+ hoursLenth
* cos(hoursAngle
);
// 绘制秒针
setlinecolor(YELLOW);
setlinestyle(PS_SOLID, 1);
line(center_x, center_y, secondEnd_x, secondEnd_y);
// 绘制分钟
setlinecolor(BLUE);
setlinestyle(PS_SOLID, 3);
line(center_x, center_y, minuteEnd_x, minuteEnd_y);
// 绘制时钟
setlinecolor(RED);
setlinestyle(PS_SOLID, 5);
line(center_x, center_y, hoursEnd_x, hoursEnd_y);
FlushBatchDraw();
Sleep(50);
//隐藏 秒针
setlinecolor(BLACK);
line(center_x, center_y, secondEnd_x, secondEnd_y);
//隐藏 分针
setlinecolor(BLACK);
line(center_x, center_y, minuteEnd_x, minuteEnd_y);
//隐藏 时针
setlinecolor(BLACK);
line(center_x, center_y, hoursEnd_x, hoursEnd_y);
}
EndBatchDraw();
_getch();
closegraph();
return 0;
}