網站首頁 編程語言 正文
簡介
這個橘子鐘表程序主要分成三個部分:畫表盤、畫表針、顯示當前時間。畫表盤部分運用到了三次貝塞爾曲線、HSL 顏色模型以及字符串格式化命令,其中三次貝塞爾曲線確定點的坐標比較復雜。
畫表針主要涉及到計算各表針運動的弧度。
顯示當前時間所用字體為等寬字體,其作用在于居中后效果更均勻。
程序當中計算三次貝塞爾曲線坐標部分,我定義了 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()
{
// 畫藍色背景
setbkcolor(0xe6cdb4); // 設置背景色
cleardevice(); // 清屏
// 畫黃色外圓
setlinestyle(PS_SOLID, 10); // 設置線寬為十個像素
setlinecolor(YELLOW); // 設置畫線顏色為黃色
setfillcolor(WHITE); // 設置填充顏色為白色
fillcircle(0, 0, 170); // 畫圓
// 填充扇形顏色
int r = 150; // 定義半徑為 150
setlinestyle(PS_SOLID, 2); // 設置線寬為兩像素
for (int n = 0; n < 10; n++)
{
// 畫橘子瓣的三次貝塞爾曲線
POINT pts[13]; // 定義數組,起始點、控制點 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); // 設置彩虹色
setlinecolor(c); // 設置畫線顏色為彩虹色
polybezier(pts, 13); // 畫三次貝塞爾曲線
setfillcolor(c); // 設置填充色為彩虹色
floodfill(int(r / 2 * cos(a)), int(r / 2 * sin(a)), c); // 填充橘子瓣
}
// 顯示表盤細節
settextcolor(BLACK); // 設置字體顏色
setbkmode(TRANSPARENT); // 設置背景色為透明
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);
// 顯示數字
wchar_t s[10];
swprintf_s(s, 10, L"%d", 12 - n); // int 類型轉換成 char 類型
// 設置字體、大小及輸出
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);
}
}
// 顯示數字時鐘
void digital(int h, int m, int s)
{
// 畫顯示當前時間的三個小矩形
setlinecolor(LIGHTGRAY); // 設置邊框顏色為淺灰色
setfillcolor(WHITE); // 設置填充顏色為白色
fillrectangle(-40 - 13, 50, -40 + 13, 50 + 26);
fillrectangle( -13, 50, 13, 50 + 26);
fillrectangle( 40 - 13, 50, 40 + 13, 50 + 26);
// 顯示當前時間
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); // 時針所走弧度
double b = PI / 2 - (2 * PI * m / 60 + 2 * PI * 1 / 60 * s / 60); // 分針所走弧度
double c = PI / 2 - 2 * PI * s / 60; // 秒針所走弧度
setlinecolor(BLACK); // 設置畫線顏色為黑色
setlinestyle(PS_SOLID, 9); // 設置線寬為九像素
line(0, 0, int(50 * cos(a)), int(-50 * sin(a))); // 畫時針
setlinestyle(PS_SOLID, 6); // 設置線寬為六像素
line(0, 0, int(100 * cos(b)), int(-100 * sin(b))); // 畫分針
setlinecolor(RED); // 設置畫線顏色為紅色
setlinestyle(PS_SOLID, 3); // 設置線寬為三像素
line(int(20 * cos(c + PI)), -int(20 * sin(c + PI)), int(130 * cos(c)), -int(130 * sin(c))); // 畫秒針
}
// 主函數
int main()
{
// 創建繪圖窗口
initgraph(640, 480);
BeginBatchDraw(); // 開啟批量繪圖
setorigin(320, 240); // 設置原點
while (true)
{
// 計算
SYSTEMTIME ti; // 定義變量保存當前時間
GetLocalTime(&ti); // 獲取當前時間
// 繪畫
cleardevice();
dial(); // 畫表盤
digital(ti.wHour, ti.wMinute, ti.wSecond); // 畫數字時鐘
needles(ti.wHour, ti.wMinute, ti.wSecond); // 畫表針
// 延時
FlushBatchDraw();
Sleep(1000);
}
_getch();
EndBatchDraw();
closegraph();
return 0;
}
原文鏈接:https://blog.csdn.net/xiangxin1030/article/details/128419504
相關推薦
- 2023-03-17 一文掌握git?push命令_相關技巧
- 2022-12-28 C++數據結構之哈希算法詳解_C 語言
- 2022-10-15 C語言利用UDP實現群聊聊天室的示例代碼_C 語言
- 2022-12-21 PyGame實現初始化導入所有模塊方法詳解_python
- 2022-10-14 初識RPC中間件zeroC ICE工具之iceca
- 2022-05-20 ASP.NET?MVC模式中應用程序結構詳解_基礎應用
- 2022-06-18 Android自定義雙向滑動控件_Android
- 2023-08-30 Linux下查找和刪除7天以前的文件
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支