C語言繪製時鐘 小程序

先貼效果圖給大家先看看

基本機制是通過獲取系統的時鐘去繪製圖線進行展示

貼出代碼 ,大家可以直接使用的 .程序我進一步的講解

#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;
}

 第一:秒鐘角度轉動的講解

1.首先數學基礎不錯的都知道 , 一個圓圈 ,一圈的角度是 2π ,所以 一秒就是2π/60 .

2. angle 角度 就是指 和 12點鐘方向的夾角 ,比如 1點和12點的夾角就是30° , 也就是 2π/12 .

第二: 講解錶盤刻度的繪製 

1. 將錶盤的刻度分爲60份 , 並且都表爲小白點

2. 5份刻度記爲1小時 ,標記爲小白圈

3. 15份刻度爲一刻鐘=15分鐘.

 

好了 ,程序就是這麼簡單! 大家可以去試試

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章