一个时钟程序C语言绘制表针

https://m.toutiao.com/is/eLmpb8s/

/*

功能:显示时间

文中有注释

编译器以及环境:VS2013/WINDOWS7

完整代码如下:

*/

#include<graphics.h>

#include<time.h>

#include<math.h>

#include<conio.h>

#include<stdio.h>

#define PI 3.141592653589793

//画时钟盘

void draw();

//画表针

void drawHand(int hour, int minute, int second, int Milliseconds);

//显示年月日 星期

void showDate(int year, int month, int date, int day);

int main()

{

//创建窗口

initgraph(600, 600);

setwritemode(R2_XORPEN);

//画时钟盘

draw();

//画表针

//drawHand();

//定义时间变量保存时间

SYSTEMTIME ti;

while(1)

{

//获取当前时间

GetLocalTime(&ti);

//动态画时钟指针

drawHand(ti.wHour, ti.wMinute, ti.wSecond, ti.wMilliseconds);

//显示上午AM 或下午PM

setbkmode(TRANSPARENT);

if (ti.wHour>0 && ti.wHour<12)

{

outtextxy(75, 70, _T('AM.'));

}

else

{

outtextxy(75, 70, _T('PM.'));

}

//显示年月日 星期

showDate(ti.wYear, ti.wMonth,ti.wDay, ti.wDayOfWeek);

//每隔1毫秒重画时钟指针

Sleep(999);

drawHand(ti.wHour, ti.wMinute, ti.wSecond, ti.wMilliseconds);

}

/*

typedef struct _SYSTEMTIME {

WORD wYear;

WORD wMonth;

WORD wDayOfWeek;

WORD wDay;

WORD wHour;

WORD wMinute;

WORD wSecond;

WORD wMilliseconds;

} SYSTEMTIME, *PSYSTEMTIME, *LPSYSTEMTIME;

*/

closegraph();

return 0;

}

//绘制表针

void drawHand(int hour, int minute, int second,int Milliseconds)

{

//对应表盘偏转弧度制 12点位置定了0度 h m s分别表示时/分/秒对应弧度

double h, m, s;

//以中心为起点针的绘制的结束点位置坐标

double xh, yh, xm, ym, xs, ys;

//每毫秒所有针转过弧度值

double a = PI / 30000.0;

//表盘中最小刻度弧度值

double b = PI/30.0;

//设置坐标原点

setorigin(300, 300);

//设置分针长180 / 时针长155 / 秒针长190

//利用三角函数求出针所在位置

//y轴正方向朝下

s = second*1000.0 * a;

m = minute*b+second*50/3*a;

h = hour*b*5+minute*b/12;

xh = 155.0 * sin(h);

yh = 155.0 * cos(h);

xm = 180.0 * sin(m);

ym = 180.0 * cos(m);

xs = 190.0 * sin(s);

ys = 190.0 * cos(s);

//绘制秒针

setlinestyle(PS_SOLID | PS_JOIN_ROUND | PS_ENDCAP_ROUND, 3);

setlinecolor(RED);

line(0, 0, xs, -ys);

//绘制分针

setlinestyle(PS_SOLID | PS_JOIN_ROUND | PS_ENDCAP_ROUND, 5);

setlinecolor(GREEN);

line(0, 0, xm, -ym);

//绘制时针

setlinestyle(PS_SOLID | PS_JOIN_ROUND | PS_ENDCAP_ROUND, 10);

setlinecolor(GREEN);

line(0, 0, xh, -yh);

}

//画时钟盘

void draw()

{

//这种窗口中心为原点

setorigin(300, 300);

//绘制外圆

circle(0, 0, 250);

//绘制内圆

circle(0, 0, 225);

//绘制针轴小圆点

circle(0, 0, 2);

//辅助圆

//circle(0, 0, 190);

//绘制刻度

int x, y,x1,y1;

for (int i = 0; i < 60;i++)//2*PI=360度 时刻度间隔弧度为2*PI/12即PI/6 ; 分刻度间隔弧度为2*PI/60

{

//设置字体

settextstyle(22, NULL, _T('楷体'));

x = 220*sin(i*PI/30);

y = 220*cos(i*PI/30);

x1 = 210 * sin(i*PI/30);

y1 = 210 * cos(i*PI/30);

//翻转坐标成数学坐标

y = -1 * y;

y1 = -1 * y1;

switch(i)

{ //绘制3,6,9,12数字对应的位置

//不用使用for循环因为了方便微调各个数字文本坐标

case 0:

outtextxy(x1-10, y1-4, _T('12'));

break;

case 15:

outtextxy(x1-8, y1-10, _T('3'));

break;

case 30:

outtextxy(x1-5, y1-15, _T('6'));

break;

case 45:

outtextxy(x1, y1-10, _T('9'));

break;

case 5:

outtextxy(x1-8 , y1-6 , _T('1'));

break;

case 10:

outtextxy(x1-8 , y1-7, _T('2'));

break;

case 20:

outtextxy(x1-7 , y1-14 , _T('4'));

break;

case 25:

outtextxy(x1-6 , y1-14 , _T('5'));

break;

case 35:

outtextxy(x1-3 , y1-13 , _T('7'));

break;

case 40:

outtextxy(x1-1 , y1-12 , _T('8'));

break;

case 50:

outtextxy(x1-2 , y1-7 , _T('10'));

break;

case 55:

outtextxy(x1-2 , y1-7 , _T('11'));

break;

default:

break;

}

if (i % 5 == 0)

{

setfillcolor(RGB(255,255,0));

fillrectangle(x - 4, y - 4, x +4, y +4);

}

else

fillcircle(x, y, 2);

}

}

//显示年/月/日 星期几

void showDate(int year, int month, int date ,int day)

{

setorigin(0, 0);

//struct dday

//{

wchar_t y[10];

wchar_t m[10];

wchar_t d[10];

wchar_t wd[7][10] = {_T('星期日'),_T('星期一'),_T('星期二'),_T('星期三'),_T('星期四'),_T('星期五'),_T('星期六')};

//}D;

_itow_s(year, y,10);

wcscat(y, _T('年'));

_itow_s(month, m, 10);

wcscat(m,_T('月'));

_itow_s(date, d, 10);

wcscat(d,_T('日'));

//去除文字背景色

setbkmode(TRANSPARENT);

outtextxy(205, 370, y);

outtextxy(285, 370, m);

outtextxy(320, 370, d);

switch (day)

{

case 0:

outtextxy(355, 20, wd[0]);

break;

case 1:

outtextxy(275, 400, wd[1]);

break;

case 2:

outtextxy(355, 20, wd[2]);

break;

case 3:

outtextxy(355, 20, wd[3]);

break;

case 4:

outtextxy(355, 20, wd[4]);

break;

case 5:

outtextxy(355, 20, wd[5]);

break;

case 6:

outtextxy(355, 20, wd[6]);

break;

default:

outtextxy(355, 20, wd[7]);

break;

}

}

//成品截图,因为时钟是动态的,图是静态所以图是不会动的

//流程图

(0)

相关推荐