首页 » 技术分享 » 带指针的【七彩时钟】(C语言)

带指针的【七彩时钟】(C语言)

 

/*=================================================
编译环境: VC6.0

头文件: graphics.h  可以在http:www.easyx.cn下到 

名称: 时钟

BY:huazai
=================================================*/
#include <graphics.h>
#include <cmath>
#include <ctime>

const double Radian = 0.104719; // 走一个刻度的弧度
const int R3 = 150;
const int R2 = R3 - 3;
const int R1 = R2 - 8;		

const int FontSize = 25;
const int R4 = R3 + FontSize +4;

const int RS = R1 - 5;
const int RM = RS - 30;
const int RH = RM - 40;

const int OriginX = 300;
const int OriginY = 200;

int colors = RED;
int count = 0;

void DrawCircleAndScale (void);
void ShowText (void);
void ShowTime (void);
void ShowSecondHand (tm tms);
void ShowMinuteHand (tm tms);
void ShowHourHand (tm tms);

int main()
{
	using namespace std;
	
	initgraph(640, 480);	// 初始化绘图窗口
	setcolor(YELLOW);
	setorigin(OriginX,OriginY);
	DrawCircleAndScale();
	ShowText();
	ShowTime();
	closegraph();
	return 0;
}

void DrawCircleAndScale (void)
{
	// 设置线条的粗度
	setlinestyle (PS_SOLID, NULL, 2);

	circle (0, 0, R3);	// 画内圆

	int i;
	for (i=0; i<=60; i++)
	{
		int tempx;
		int tempy;
		if (i%5 == 0)
		{
			tempx = int(R1*cos(Radian*i));
			tempy = int(R1*sin(Radian*i));
		}
		else
		{
			tempx = int(R2*cos(Radian*i));
			tempy = int(R2*sin(Radian*i));
		}
		line (
				int(tempx), 
				int(tempy), 
				int(R3*cos(Radian*i)), 
				int(R3*sin(Radian*i))
			);
	}

	circle (0, 0, R4);	//画外圆
	setlinestyle (PS_SOLID, NULL, 1);	// 还原线条粗度
}

void ShowText (void)
{
	setfont(FontSize, 0, "Arial");
	RECT r1 = {-R3, -(2*R3+FontSize), R3, 0};
	drawtext("12", &r1, DT_CENTER | DT_VCENTER | DT_SINGLELINE);

	RECT r2 = {0, -R3, 2*R3+FontSize, R3};
	drawtext("3", &r2, DT_CENTER | DT_VCENTER | DT_SINGLELINE);

	RECT r3 = {-R3, 0, R3, 2*R3+FontSize+4};
	drawtext("6", &r3, DT_CENTER | DT_VCENTER | DT_SINGLELINE);

	RECT r4 = {-(2*R3+FontSize), -R3, 0, R3};
	drawtext("9", &r4, DT_CENTER | DT_VCENTER | DT_SINGLELINE);

	setfont(FontSize*2, 0, "宋体");
	RECT r5 = {-R4*2, 0, R4*2, 2*R3+FontSize*5};
	drawtext("日月经天 江河行地", &r5, DT_CENTER | DT_VCENTER | DT_SINGLELINE);

	setfont(FontSize, 0, "宋体");
	RECT r6 = {0, 0, R4*3, 2*R3+FontSize*8};
	drawtext("BY:huazai", &r6, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
}

void ShowTime (void)
{
	while(1)
	{
		DrawCircleAndScale();
		ShowText();
		time_t t = time(0);
		tm ti = *localtime(&t);
		ShowSecondHand(ti);
		ShowMinuteHand(ti);
		ShowHourHand(ti);
		Sleep(1000);
		cleardevice();
		switch (count%16)
		{
			case 0 : colors = BLUE; break;
			case 1 : colors = GREEN; break;
			case 2 : colors = CYAN; break;
			case 3 : colors = RED; break;
			case 4 : colors = MAGENTA; break;
			case 5 : colors = BROWN; break;
			case 6 : colors = LIGHTGRAY; break;
			case 7 : colors = DARKGRAY; break;
			case 8 : colors = LIGHTBLUE; break;
			case 9 : colors = LIGHTGREEN; break;
			case 10 : colors = LIGHTCYAN; break;
			case 11 : colors = LIGHTRED; break;
			case 12 : colors = LIGHTMAGENTA; break;
			case 13 : colors = YELLOW; break;
			case 14 : colors = WHITE; break;
			case 15 : colors = BROWN; break;
			case 16 : colors = GREEN; break;
		}
		count++;
	}
}
void ShowSecondHand (tm tms)
{

	line (
			0, 
			0, 
			RS*cos(Radian*tms.tm_sec - Radian*15), 
			RS*sin(Radian*tms.tm_sec - Radian*15)
		);
}

void ShowMinuteHand (tm tms)
{
	setlinestyle (PS_SOLID, NULL, 4);
	setcolor(GREEN);
	line (
			0, 
			0, 
			RM*cos(Radian*tms.tm_min - Radian*15), 
			RM*sin(Radian*tms.tm_min - Radian*15)
		);
	setcolor(colors);
}

void ShowHourHand (tm tms)
{
	setlinestyle (PS_SOLID, NULL, 8);
	setcolor(BLUE);
	line (
			0, 
			0, 
			RH*cos(Radian*5*tms.tm_hour - Radian*15 + Radian*tms.tm_min/15), 
			RH*sin(Radian*5*tms.tm_hour - Radian*15 + Radian*tms.tm_min/15)
		);
	setcolor(colors);
}

转载自原文链接, 如需删除请联系管理员。

原文链接:带指针的【七彩时钟】(C语言),转载请注明来源!

0