///
// 程序名称:钟表模拟程序(表针形式)
/2022.04.13///
///需求:圆盘时钟
#include <stdio.h>
#include <math.h>
#include <conio.h>
#include <graphics.h>
#include <windows.h>
#define PI 3.1415926
void yuanpan(){
//表盘
fillcircle(320, 240, 160); //320*240
outtextxy(296, 310, _T("Daybreak。")); //指定位置输出字符
//刻度
setfillcolor(BLACK);
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)
{
fillrectangle(x - 2, y -2 , x + 2, y + 2); //长方形的参数
//fillcircle(x, y, 3);
}
else if (i % 5 == 0)
fillcircle(x, y, 2);
else
fillcircle(x, y, 1);
}
}
void biaozhen(int hour, int minute, int second){
double a_hour, a_min, a_sec; // 时、分、秒针的弧度值
int x_hour, y_hour, x_min, y_min, x_sec, y_sec; // 时、分、秒针的末端位置
// 计算时、分、秒针的弧度值
a_sec = second * 2 * PI / 60; //2 * PI / 60 一秒的弧度
a_min = minute * 2 * PI / 60 + a_sec / 60;
a_hour = hour * 2 * PI / 12 + a_min / 12;
// 计算时、分、秒针的末端位置
x_sec = int(120 * sin(a_sec)); y_sec = int(120 * cos(a_sec));
x_min = int(100 * sin(a_min)); y_min = int(100 * cos(a_min));
x_hour = int(70 * sin(a_hour)); y_hour = int(70 * cos(a_hour));
// 时针(320,240是表中心的坐标)
setlinestyle(PS_SOLID, 9);
setlinecolor(WHITE);
line(320 + x_hour, 240 - y_hour, 320 - x_hour / 7, 240 +y_hour / 7);//起始位置
// 分针
setlinestyle(PS_SOLID, 6);
setlinecolor(WHITE);
line(320 + x_min, 240 - y_min, 320 - x_min / 5, 240 + y_min / 5);
// 秒针
setlinestyle(PS_SOLID, 3);
setlinecolor(WHITE);
line(320 + x_sec, 240 - y_sec, 320 - x_sec / 3, 240 + y_sec / 3);
}
//圆盘上的数字
void shuzi()
{
outtextxy(450,235,"3");
outtextxy(185,235,"9");
outtextxy(315,105,"12");
outtextxy(315,360,"6");
outtextxy(385,125,"1");
outtextxy(250,125,"11");
outtextxy(385,340,"5");
outtextxy(250,340,"7");
outtextxy(430,170,"2");
outtextxy(205,170,"10");
outtextxy(430,300,"4");
outtextxy(205,300,"8");
}
int main( )
{
int hour1, minute1, hour2, minute2, hour3, minute3, hour4, minute4; // 闹钟变量
initgraph(640, 480);
cleardevice();
setfillcolor(BLACK);
yuanpan();
setwritemode(R2_XORPEN); //设置XOR绘图模式,如果没有,会留下幻影
SYSTEMTIME time; //定义时间结构体 用来保存当前的时间
while (1)
{
GetLocalTime(&time); // API 函数,获取当地的当前系统日期和时间
shuzi();
biaozhen(time.wHour, time.wMinute, time.wSecond); //把表针绘制出来
shuzi();
Sleep(1000); //1000毫秒
biaozhen(time.wHour, time.wMinute, time.wSecond); //----
}
closegraph();
return 0;
}
效果图: