C語言繪制簡單時鐘小程序
本文實例為大家分享了C語言繪制時鐘小程序的具體代碼,供大家參考,具體內(nèi)容如下
先貼效果圖給大家先看看

基本機制是通過獲取系統(tǒng)的時鐘去繪制圖線進行展示
貼出代碼 ,大家可以直接使用的 .程序我進一步的講解
#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; //獲取系統(tǒng)的時間
?
?? ?// 秒針屬性
?? ?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); // 小白點
?? ??? ??? ?}
?? ??? ?}
?
?? ??? ?
?? ??? ?//轉(zhuǎn)動秒針
?? ??? ?secondAngle = ti.wSecond * 2 * pi / 60; ? ?// ? ?2 * pi / 60 =一秒鐘走的角度 ? ti.wSecond ?=系統(tǒng)當前秒
?? ??? ?secondEnd_x = center_x + secondLenth * sin(secondAngle);
?? ??? ?secondEnd_y = center_y - secondLenth * cos(secondAngle);
?
?? ??? ?//轉(zhuǎn)動分鐘
?? ??? ?minuteAngle = ti.wMinute * 2 * pi / 60 ?+ secondAngle / 60;
?? ??? ?minuteEnd_x = center_x + minuteLenth * sin(minuteAngle);
?? ??? ?minuteEnd_y = center_y - minuteLenth * cos(minuteAngle);
?
?? ??? ?//轉(zhuǎn)動時鐘
?? ??? ?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;
}第一:秒鐘角度轉(zhuǎn)動的講解

1.首先數(shù)學(xué)基礎(chǔ)不錯的都知道 , 一個圓圈 ,一圈的角度是 2π ,所以 一秒就是2π/60 .
2. angle 角度 就是指 和 12點鐘方向的夾角 ,比如 1點和12點的夾角就是30° , 也就是 2π/12 .
第二:講解表盤刻度的繪制

1. 將表盤的刻度分為60份 , 并且都表為小白點
2. 5份刻度記為1小時 ,標記為小白圈
3. 15份刻度為一刻鐘=15分鐘.
好了 ,程序就是這么簡單! 大家可以去試試
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++高級數(shù)據(jù)結(jié)構(gòu)之優(yōu)先隊列
這篇文章主要介紹了C++高級數(shù)據(jù)結(jié)構(gòu)之優(yōu)先隊列,文章圍繞主題的相關(guān)資料展開詳細介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-05-05
Visual Studio 2019下配置 CUDA 10.1 + TensorFlow-GPU 1.14.0
這篇文章主要介紹了Visual Studio 2019下配置 CUDA 10.1 + TensorFlow-GPU 1.14.0,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
QT Creator+OpenCV實現(xiàn)圖像灰度化的示例代碼
這篇文章主要為大家詳細介紹了QT如何利用Creator和OpenCV實現(xiàn)圖像灰度化效果,文中的示例代碼講解詳細,感興趣的小伙伴可以嘗試一下2022-12-12

