您的位置:首页 > 其它

钟表

2015-07-13 01:17 309 查看
很遗憾没有想到好方法解决刻度的问题,明天希望能解决这个问题。

-------------------------------------------------------------------------------------------

今天改进了,刻度实现了,虽然很丑

,继续进步吧,下面为改进后的代码


#include <graphics.h>

#include <conio.h>

#include <math.h>

#define PI 3.14159265359

void Draw(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;
a_min = minute * 2 * PI / 60 + a_sec / 60;
a_hour= hour * 2 * PI / 12 + a_min / 12;

// 计算时、分、秒针的末端位置
x_sec = 320 + (int)(120 * sin(a_sec));
y_sec = 240 - (int)(120 * cos(a_sec));
x_min = 320 + (int)(100 * sin(a_min));
y_min = 240 - (int)(100 * cos(a_min));
x_hour= 320 + (int)(70 * sin(a_hour));
y_hour= 240 - (int)(70 * cos(a_hour));

// 画时针
setlinestyle(PS_SOLID, 10, NULL);
setlinecolor(WHITE);
line(320, 240, x_hour, y_hour);
line(320, 240, ((-(x_hour-320))/10+320), ((-(y_hour-240))/10+240));

// 画分针
setlinestyle(PS_SOLID, 6, NULL);
setlinecolor(LIGHTGRAY);
line(320, 240, x_min, y_min);
line(320, 240, ((-(x_min-320))/10+320), ((-(y_min-240))/10+240));

// 画秒针
setlinestyle(PS_SOLID, 2, NULL);
setlinecolor(RED);
line(320, 240, x_sec, y_sec);
line(320, 240, ((-(x_sec-320))/10+320), ((-(y_sec-240))/10+240));

}

void main()

{
initgraph(640, 480);
// 初始化 640 x 480 的绘图窗口

// 绘制一个简单的表盘
circle(320, 240, 2);
circle(320, 240, 60);
circle(320, 240, 160);
outtextxy(296, 310, _T("Hust_AP"));

TCHAR s[3];
int x0,y0;
for(double  i = 1 ; i <= 12 ;i++)
{
_stprintf(s,_T("%d"),(int)i);
x0 = 320 + (int)(180 * sin(i * 30 * PI / 180.0));
y0 = 240 - (int)(180 * cos(i * 30 * PI / 180.0));
outtextxy(x0,y0,s);
}
// 设置 XOR 绘图模式
setwritemode(R2_XORPEN);
// 设置 XOR 绘图模式

// 绘制表针
SYSTEMTIME ti;
// 定义变量保存当前时间
while(!kbhit())
// 按任意键退出钟表程序
{
GetLocalTime(&ti);
// 获取当前时间
Draw(ti.wHour, ti.wMinute, ti.wSecond);
// 画表针
Sleep(1000);
// 延时 1 秒
Draw(ti.wHour, ti.wMinute, ti.wSecond);
// 擦表针(擦表针和画表针的过程是一样的)
}

closegraph();
// 关闭绘图窗口

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: