C語言實現(xiàn)繪制可愛的橘子鐘表
簡介
這個橘子鐘表程序主要分成三個部分:畫表盤、畫表針、顯示當(dāng)前時間。畫表盤部分運用到了三次貝塞爾曲線、HSL 顏色模型以及字符串格式化命令,其中三次貝塞爾曲線確定點的坐標(biāo)比較復(fù)雜。
畫表針主要涉及到計算各表針運動的弧度。
顯示當(dāng)前時間所用字體為等寬字體,其作用在于居中后效果更均勻。
程序當(dāng)中計算三次貝塞爾曲線坐標(biāo)部分,我定義了 13 個點,其中 0 點和 11 點 12 點重合,3 點和 4 點重合,5 點和 6 點重合,10 點和 9 點重合。這樣做的目的是便于確定起始點、控制點和終點。
程序中用到了 Broadway 字體,好像是裝 Office 的時候帶的字體。如果看不到文字效果,請先安裝這個字體。
程序截圖
源碼
#include <stdio.h> #include <conio.h> #include <graphics.h> #include <math.h> const double PI = 3.141592654; // 定義圓周率 // 畫表盤 void dial() { // 畫藍(lán)色背景 setbkcolor(0xe6cdb4); // 設(shè)置背景色 cleardevice(); // 清屏 // 畫黃色外圓 setlinestyle(PS_SOLID, 10); // 設(shè)置線寬為十個像素 setlinecolor(YELLOW); // 設(shè)置畫線顏色為黃色 setfillcolor(WHITE); // 設(shè)置填充顏色為白色 fillcircle(0, 0, 170); // 畫圓 // 填充扇形顏色 int r = 150; // 定義半徑為 150 setlinestyle(PS_SOLID, 2); // 設(shè)置線寬為兩像素 for (int n = 0; n < 10; n++) { // 畫橘子瓣的三次貝塞爾曲線 POINT pts[13]; // 定義數(shù)組,起始點、控制點 1、控制點 2、終點(起點)、控制點 1、控制點 2、終點(起點)…… double a = 2 * PI * n / 10; // 橘子瓣弧度 pts[0].x = int(r / 8 * cos(a + 2 * PI / 22)); pts[0].y = int(r / 8 * sin(a + 2 * PI / 22)); pts[12] = pts[11] = pts[0]; pts[1].x = int(r / 12 * cos(a + 2 * PI / 22)); pts[2].x = int(r / 12 * cos(a - 2 * PI / 22)); pts[1].y = int(r / 12 * sin(a + 2 * PI / 22)); pts[2].y = int(r / 12 * sin(a - 2 * PI / 22)); pts[3].x = int(r / 8 * cos(a - 2 * PI / 22)); pts[3].y = int(r / 8 * sin(a - 2 * PI / 22)); pts[4] = pts[3]; pts[5].x = int(r * cos(a - 2 * PI / 22)); pts[5].y = int(r * sin(a - 2 * PI / 22)); pts[6] = pts[5]; pts[9].x = int(r * cos(a + 2 * PI / 22)); pts[9].y = int(r * sin(a + 2 * PI / 22)); pts[10] = pts[9]; pts[7].x = int(r * 1.1 * cos(a - 2 * PI / 30)); pts[8].x = int(r * 1.1 * cos(a + 2 * PI / 30)); pts[7].y = int(r * 1.1 * sin(a - 2 * PI / 30)); pts[8].y = int(r * 1.1 * sin(a + 2 * PI / 30)); int c = HSLtoRGB(36.0f * n, 0.8f, 0.8f); // 設(shè)置彩虹色 setlinecolor(c); // 設(shè)置畫線顏色為彩虹色 polybezier(pts, 13); // 畫三次貝塞爾曲線 setfillcolor(c); // 設(shè)置填充色為彩虹色 floodfill(int(r / 2 * cos(a)), int(r / 2 * sin(a)), c); // 填充橘子瓣 } // 顯示表盤細(xì)節(jié) settextcolor(BLACK); // 設(shè)置字體顏色 setbkmode(TRANSPARENT); // 設(shè)置背景色為透明 for (int n = 0; n < 12; n++) { // 整點刻度 setfillcolor(BLACK); solidcircle(int(145 * cos(n * 2 * PI / 12)), -int(145 * sin(n * 2 * PI / 12)), 2); solidcircle(int(145 * cos(n * 2 * PI / 4)), -int(145 * sin(n * 2 * PI / 4)), 4); // 顯示數(shù)字 wchar_t s[10]; swprintf_s(s, 10, L"%d", 12 - n); // int 類型轉(zhuǎn)換成 char 類型 // 設(shè)置字體、大小及輸出 if ((12 - n) % 3 == 0) settextstyle(48, 0, L"Broadway"); else settextstyle(24, 0, L"Broadway"); // 定義字符串長和寬,居中 int w, h; w = textwidth(s); h = textheight(s); outtextxy(int(125 * cos(n * 2 * PI / 12 + PI / 2) - w / 2), -int(125 * sin(n * 2 * PI / 12 + PI / 2) - h / 2), s); } } // 顯示數(shù)字時鐘 void digital(int h, int m, int s) { // 畫顯示當(dāng)前時間的三個小矩形 setlinecolor(LIGHTGRAY); // 設(shè)置邊框顏色為淺灰色 setfillcolor(WHITE); // 設(shè)置填充顏色為白色 fillrectangle(-40 - 13, 50, -40 + 13, 50 + 26); fillrectangle( -13, 50, 13, 50 + 26); fillrectangle( 40 - 13, 50, 40 + 13, 50 + 26); // 顯示當(dāng)前時間 settextstyle(24, 0, L"Consolas"); wchar_t a[10]; int w; swprintf_s(a, 10, L"%d", h); w = textwidth(a); outtextxy(-40 - w / 2, 50, a); swprintf_s(a, 10, L"%d", m); w = textwidth(a); outtextxy( -w / 2, 50, a); swprintf_s(a, 10, L"%d", s); w = textwidth(a); outtextxy( 40 - w / 2, 50, a); } // 畫表針 void needles(int h, int m, int s) { double a = PI / 2 - (2 * PI * h / 12 + 2 * PI * 1 / 12 * m / 60); // 時針?biāo)呋《? double b = PI / 2 - (2 * PI * m / 60 + 2 * PI * 1 / 60 * s / 60); // 分針?biāo)呋《? double c = PI / 2 - 2 * PI * s / 60; // 秒針?biāo)呋《? setlinecolor(BLACK); // 設(shè)置畫線顏色為黑色 setlinestyle(PS_SOLID, 9); // 設(shè)置線寬為九像素 line(0, 0, int(50 * cos(a)), int(-50 * sin(a))); // 畫時針 setlinestyle(PS_SOLID, 6); // 設(shè)置線寬為六像素 line(0, 0, int(100 * cos(b)), int(-100 * sin(b))); // 畫分針 setlinecolor(RED); // 設(shè)置畫線顏色為紅色 setlinestyle(PS_SOLID, 3); // 設(shè)置線寬為三像素 line(int(20 * cos(c + PI)), -int(20 * sin(c + PI)), int(130 * cos(c)), -int(130 * sin(c))); // 畫秒針 } // 主函數(shù) int main() { // 創(chuàng)建繪圖窗口 initgraph(640, 480); BeginBatchDraw(); // 開啟批量繪圖 setorigin(320, 240); // 設(shè)置原點 while (true) { // 計算 SYSTEMTIME ti; // 定義變量保存當(dāng)前時間 GetLocalTime(&ti); // 獲取當(dāng)前時間 // 繪畫 cleardevice(); dial(); // 畫表盤 digital(ti.wHour, ti.wMinute, ti.wSecond); // 畫數(shù)字時鐘 needles(ti.wHour, ti.wMinute, ti.wSecond); // 畫表針 // 延時 FlushBatchDraw(); Sleep(1000); } _getch(); EndBatchDraw(); closegraph(); return 0; }
到此這篇關(guān)于C語言實現(xiàn)繪制可愛的橘子鐘表的文章就介紹到這了,更多相關(guān)C語言鐘表內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語言動態(tài)內(nèi)存管理的原理及實現(xiàn)方法
C語言動態(tài)內(nèi)存管理的原理是通過 malloc() 函數(shù)申請一塊連續(xù)的內(nèi)存空間,并返回其地址,通過 free() 函數(shù)釋放該內(nèi)存空間。實現(xiàn)方法是通過在程序運行時動態(tài)地管理內(nèi)存,即在需要內(nèi)存時申請,不需要時釋放,避免了靜態(tài)內(nèi)存分配的浪費和不足2023-04-04windows下安裝QT及visual studio 2017搭建開發(fā)環(huán)境
這篇文章主要介紹了windows下安裝QT及visual studio 2017搭建開發(fā)環(huán)境,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03判斷整數(shù)序列是否為二元查找樹的后序遍歷結(jié)果的解決方法
本篇文章是對判斷整數(shù)序列是否為二元查找樹的后序遍歷結(jié)果的解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05在Qt中使用QtWebApp搭建HTTP服務(wù)器的詳細(xì)步驟
QtWebApp是一個開源項目,它基于著名的Qt?Framework開發(fā),提供了一種在C++環(huán)境中構(gòu)建HTTP服務(wù)器的解決方案,這篇文章主要給大家介紹了關(guān)于在Qt中使用QtWebApp搭建HTTP服務(wù)器的詳細(xì)步驟,需要的朋友可以參考下2024-07-07