C語(yǔ)言實(shí)現(xiàn)繪制可愛(ài)的橘子鐘表
簡(jiǎn)介
這個(gè)橘子鐘表程序主要分成三個(gè)部分:畫(huà)表盤(pán)、畫(huà)表針、顯示當(dāng)前時(shí)間。畫(huà)表盤(pán)部分運(yùn)用到了三次貝塞爾曲線(xiàn)、HSL 顏色模型以及字符串格式化命令,其中三次貝塞爾曲線(xiàn)確定點(diǎn)的坐標(biāo)比較復(fù)雜。
畫(huà)表針主要涉及到計(jì)算各表針運(yùn)動(dòng)的弧度。
顯示當(dāng)前時(shí)間所用字體為等寬字體,其作用在于居中后效果更均勻。
程序當(dāng)中計(jì)算三次貝塞爾曲線(xiàn)坐標(biāo)部分,我定義了 13 個(gè)點(diǎn),其中 0 點(diǎn)和 11 點(diǎn) 12 點(diǎn)重合,3 點(diǎn)和 4 點(diǎn)重合,5 點(diǎn)和 6 點(diǎn)重合,10 點(diǎn)和 9 點(diǎn)重合。這樣做的目的是便于確定起始點(diǎn)、控制點(diǎn)和終點(diǎn)。
程序中用到了 Broadway 字體,好像是裝 Office 的時(shí)候帶的字體。如果看不到文字效果,請(qǐng)先安裝這個(gè)字體。
程序截圖

源碼
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <math.h>
const double PI = 3.141592654; // 定義圓周率
// 畫(huà)表盤(pán)
void dial()
{
// 畫(huà)藍(lán)色背景
setbkcolor(0xe6cdb4); // 設(shè)置背景色
cleardevice(); // 清屏
// 畫(huà)黃色外圓
setlinestyle(PS_SOLID, 10); // 設(shè)置線(xiàn)寬為十個(gè)像素
setlinecolor(YELLOW); // 設(shè)置畫(huà)線(xiàn)顏色為黃色
setfillcolor(WHITE); // 設(shè)置填充顏色為白色
fillcircle(0, 0, 170); // 畫(huà)圓
// 填充扇形顏色
int r = 150; // 定義半徑為 150
setlinestyle(PS_SOLID, 2); // 設(shè)置線(xiàn)寬為兩像素
for (int n = 0; n < 10; n++)
{
// 畫(huà)橘子瓣的三次貝塞爾曲線(xiàn)
POINT pts[13]; // 定義數(shù)組,起始點(diǎn)、控制點(diǎn) 1、控制點(diǎn) 2、終點(diǎn)(起點(diǎn))、控制點(diǎn) 1、控制點(diǎn) 2、終點(diǎn)(起點(diǎn))……
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è)置畫(huà)線(xiàn)顏色為彩虹色
polybezier(pts, 13); // 畫(huà)三次貝塞爾曲線(xiàn)
setfillcolor(c); // 設(shè)置填充色為彩虹色
floodfill(int(r / 2 * cos(a)), int(r / 2 * sin(a)), c); // 填充橘子瓣
}
// 顯示表盤(pán)細(xì)節(jié)
settextcolor(BLACK); // 設(shè)置字體顏色
setbkmode(TRANSPARENT); // 設(shè)置背景色為透明
for (int n = 0; n < 12; n++)
{
// 整點(diǎ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 類(lèi)型轉(zhuǎn)換成 char 類(lèi)型
// 設(shè)置字體、大小及輸出
if ((12 - n) % 3 == 0) settextstyle(48, 0, L"Broadway");
else settextstyle(24, 0, L"Broadway");
// 定義字符串長(zhǎng)和寬,居中
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ù)字時(shí)鐘
void digital(int h, int m, int s)
{
// 畫(huà)顯示當(dāng)前時(shí)間的三個(gè)小矩形
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)前時(shí)間
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);
}
// 畫(huà)表針
void needles(int h, int m, int s)
{
double a = PI / 2 - (2 * PI * h / 12 + 2 * PI * 1 / 12 * m / 60); // 時(shí)針?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è)置畫(huà)線(xiàn)顏色為黑色
setlinestyle(PS_SOLID, 9); // 設(shè)置線(xiàn)寬為九像素
line(0, 0, int(50 * cos(a)), int(-50 * sin(a))); // 畫(huà)時(shí)針
setlinestyle(PS_SOLID, 6); // 設(shè)置線(xiàn)寬為六像素
line(0, 0, int(100 * cos(b)), int(-100 * sin(b))); // 畫(huà)分針
setlinecolor(RED); // 設(shè)置畫(huà)線(xiàn)顏色為紅色
setlinestyle(PS_SOLID, 3); // 設(shè)置線(xiàn)寬為三像素
line(int(20 * cos(c + PI)), -int(20 * sin(c + PI)), int(130 * cos(c)), -int(130 * sin(c))); // 畫(huà)秒針
}
// 主函數(shù)
int main()
{
// 創(chuàng)建繪圖窗口
initgraph(640, 480);
BeginBatchDraw(); // 開(kāi)啟批量繪圖
setorigin(320, 240); // 設(shè)置原點(diǎn)
while (true)
{
// 計(jì)算
SYSTEMTIME ti; // 定義變量保存當(dāng)前時(shí)間
GetLocalTime(&ti); // 獲取當(dāng)前時(shí)間
// 繪畫(huà)
cleardevice();
dial(); // 畫(huà)表盤(pán)
digital(ti.wHour, ti.wMinute, ti.wSecond); // 畫(huà)數(shù)字時(shí)鐘
needles(ti.wHour, ti.wMinute, ti.wSecond); // 畫(huà)表針
// 延時(shí)
FlushBatchDraw();
Sleep(1000);
}
_getch();
EndBatchDraw();
closegraph();
return 0;
}到此這篇關(guān)于C語(yǔ)言實(shí)現(xiàn)繪制可愛(ài)的橘子鐘表的文章就介紹到這了,更多相關(guān)C語(yǔ)言鐘表內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
減少C++代碼編譯時(shí)間的簡(jiǎn)單方法(必看篇)
下面小編就為大家?guī)?lái)一篇減少C++代碼編譯時(shí)間的簡(jiǎn)單方法(必看篇)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-01-01
C++類(lèi)中的static和const用法實(shí)例教程
這篇文章主要介紹了C++類(lèi)中的static和const用法,是C++面向?qū)ο蟪绦蛟O(shè)計(jì)中非常重要的概念,需要的朋友可以參考下2014-08-08
C/C++函數(shù)調(diào)用棧的實(shí)現(xiàn)方法
這篇文章主要介紹了C/C++函數(shù)調(diào)用棧的實(shí)現(xiàn)方法,可實(shí)現(xiàn)一個(gè)簡(jiǎn)單的腳本解釋器,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2014-10-10
C++使用grpc實(shí)現(xiàn)回射服務(wù)器
gRPC是由Google開(kāi)發(fā)的一個(gè)開(kāi)源的高性能遠(yuǎn)程過(guò)程調(diào)用(RPC)框架,用于在分布式系統(tǒng)中實(shí)現(xiàn)跨語(yǔ)言的服務(wù)通信,本文我們就來(lái)看看C++如何使用grpc實(shí)現(xiàn)回射服務(wù)器2024-10-10
C++示例講解friend static const關(guān)鍵字的用法
靜態(tài)成員static是解決同一個(gè)類(lèi)的不同對(duì)象之間數(shù)據(jù)和函數(shù)共享問(wèn)題。區(qū)分全局變量,全局變量也能實(shí)現(xiàn)數(shù)據(jù)共享,但安全性和封裝性被破壞了,友元提供了不同類(lèi)或?qū)ο蟮某蓡T函數(shù)之間、類(lèi)的成員函數(shù)與一般函數(shù)之間進(jìn)行數(shù)據(jù)共享的機(jī)制,const常引用-被引用的對(duì)象不能被更新2022-06-06

