C語言繪制簡(jiǎn)單時(shí)鐘小程序
本文實(shí)例為大家分享了C語言繪制時(shí)鐘小程序的具體代碼,供大家參考,具體內(nèi)容如下
先貼效果圖給大家先看看
基本機(jī)制是通過獲取系統(tǒng)的時(shí)鐘去繪制圖線進(jìn)行展示
貼出代碼 ,大家可以直接使用的 .程序我進(jìn)一步的講解
#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)的時(shí)間 ? ?? ?// 秒針屬性 ?? ?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; ? ?? ?// ?時(shí)鐘屬性 ?? ?int hoursEnd_x, hoursEnd_y; ?? ?float hoursAngle = 0; ?? ?int hoursLenth = 60; ?? ?hoursEnd_x = center_x + hoursLenth; ?? ?hoursEnd_y = center_y + hoursLenth; ? ?? ?BeginBatchDraw(); ? ?? ?while (1) ?? ?{ ?? ??? ?// 獲取時(shí)間 ?? ??? ?GetLocalTime(&ti); ? ?? ??? ?//繪制中心坐標(biāo) ?? ??? ?//setlinecolor(WHITE); ?? ??? ?//fillcircle(center_x, center_y, 2); ? ?? ??? ?// 繪制一個(gè)表盤 ?? ??? ?setbkcolor(BLACK); ?? ??? ?setlinestyle(PS_SOLID, 1); ?? ??? ?setlinecolor(WHITE); ?? ??? ?circle(center_x, center_y, 130); ?? ??? ?//outtextxy(center_x - 25, center_y + width / 6, "我的時(shí)鐘"); ?? ??? ?// 輸出字符串 (VC6) ?? ??? ?TCHAR s[] = _T("我的時(shí)鐘"); ?? ??? ?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); // 小白點(diǎn) ?? ??? ??? ?} ?? ??? ?} ? ?? ??? ? ?? ??? ?//轉(zhuǎn)動(dòng)秒針 ?? ??? ?secondAngle = ti.wSecond * 2 * pi / 60; ? ?// ? ?2 * pi / 60 =一秒鐘走的角度 ? ti.wSecond ?=系統(tǒng)當(dāng)前秒 ?? ??? ?secondEnd_x = center_x + secondLenth * sin(secondAngle); ?? ??? ?secondEnd_y = center_y - secondLenth * cos(secondAngle); ? ?? ??? ?//轉(zhuǎn)動(dòng)分鐘 ?? ??? ?minuteAngle = ti.wMinute * 2 * pi / 60 ?+ secondAngle / 60; ?? ??? ?minuteEnd_x = center_x + minuteLenth * sin(minuteAngle); ?? ??? ?minuteEnd_y = center_y - minuteLenth * cos(minuteAngle); ? ?? ??? ?//轉(zhuǎn)動(dòng)時(shí)鐘 ?? ??? ?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); ? ?? ??? ?// 繪制時(shí)鐘 ?? ??? ?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); ? ?? ??? ?//隱藏 時(shí)針 ?? ??? ?setlinecolor(BLACK); ?? ??? ?line(center_x, center_y, hoursEnd_x, hoursEnd_y); ?? ?} ?? ?EndBatchDraw(); ?? ?_getch(); ?? ?closegraph(); ? ?? ?return 0; }
第一:秒鐘角度轉(zhuǎn)動(dòng)的講解
1.首先數(shù)學(xué)基礎(chǔ)不錯(cuò)的都知道 , 一個(gè)圓圈 ,一圈的角度是 2π ,所以 一秒就是2π/60 .
2. angle 角度 就是指 和 12點(diǎn)鐘方向的夾角 ,比如 1點(diǎn)和12點(diǎn)的夾角就是30° , 也就是 2π/12 .
第二:講解表盤刻度的繪制
1. 將表盤的刻度分為60份 , 并且都表為小白點(diǎn)
2. 5份刻度記為1小時(shí) ,標(biāo)記為小白圈
3. 15份刻度為一刻鐘=15分鐘.
好了 ,程序就是這么簡(jiǎn)單! 大家可以去試試
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++高級(jí)數(shù)據(jù)結(jié)構(gòu)之優(yōu)先隊(duì)列
這篇文章主要介紹了C++高級(jí)數(shù)據(jù)結(jié)構(gòu)之優(yōu)先隊(duì)列,文章圍繞主題的相關(guān)資料展開詳細(xì)介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-05-05Visual Studio 2019下配置 CUDA 10.1 + TensorFlow-GPU 1.14.0
這篇文章主要介紹了Visual Studio 2019下配置 CUDA 10.1 + TensorFlow-GPU 1.14.0,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03QT Creator+OpenCV實(shí)現(xiàn)圖像灰度化的示例代碼
這篇文章主要為大家詳細(xì)介紹了QT如何利用Creator和OpenCV實(shí)現(xiàn)圖像灰度化效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以嘗試一下2022-12-12C++使用鏈表存儲(chǔ)實(shí)現(xiàn)通訊錄功能管理
這篇文章主要為大家詳細(xì)介紹了C++使用鏈表存儲(chǔ)實(shí)現(xiàn)通訊錄功能管理,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06