日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

C語(yǔ)言繪制簡(jiǎn)單時(shí)鐘小程序_C 語(yǔ)言

作者:逼哥很瘋狂 ? 更新時(shí)間: 2022-08-06 編程語(yǔ)言

本文實(shí)例為大家分享了C語(yǔ)言繪制時(shí)鐘小程序的具體代碼,供大家參考,具體內(nèi)容如下

先貼效果圖給大家先看看

基本機(jī)制是通過(guò)獲取系統(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)單! 大家可以去試試

原文鏈接:https://blog.csdn.net/feipo_zhm/article/details/106791580

欄目分類
最近更新