網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
簡(jiǎn)介
這個(gè)橘子鐘表程序主要分成三個(gè)部分:畫表盤、畫表針、顯示當(dāng)前時(shí)間。畫表盤部分運(yùn)用到了三次貝塞爾曲線、HSL 顏色模型以及字符串格式化命令,其中三次貝塞爾曲線確定點(diǎn)的坐標(biāo)比較復(fù)雜。
畫表針主要涉及到計(jì)算各表針運(yùn)動(dòng)的弧度。
顯示當(dāng)前時(shí)間所用字體為等寬字體,其作用在于居中后效果更均勻。
程序當(dāng)中計(jì)算三次貝塞爾曲線坐標(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; // 定義圓周率
// 畫表盤
void dial()
{
// 畫藍(lán)色背景
setbkcolor(0xe6cdb4); // 設(shè)置背景色
cleardevice(); // 清屏
// 畫黃色外圓
setlinestyle(PS_SOLID, 10); // 設(shè)置線寬為十個(gè)像素
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ù)組,起始點(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è)置畫線顏色為彩虹色
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++)
{
// 整點(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 類型轉(zhuǎn)換成 char 類型
// 設(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)
{
// 畫顯示當(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);
}
// 畫表針
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è)置畫線顏色為黑色
setlinestyle(PS_SOLID, 9); // 設(shè)置線寬為九像素
line(0, 0, int(50 * cos(a)), int(-50 * sin(a))); // 畫時(shí)針
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(); // 開(kāi)啟批量繪圖
setorigin(320, 240); // 設(shè)置原點(diǎn)
while (true)
{
// 計(jì)算
SYSTEMTIME ti; // 定義變量保存當(dāng)前時(shí)間
GetLocalTime(&ti); // 獲取當(dāng)前時(shí)間
// 繪畫
cleardevice();
dial(); // 畫表盤
digital(ti.wHour, ti.wMinute, ti.wSecond); // 畫數(shù)字時(shí)鐘
needles(ti.wHour, ti.wMinute, ti.wSecond); // 畫表針
// 延時(shí)
FlushBatchDraw();
Sleep(1000);
}
_getch();
EndBatchDraw();
closegraph();
return 0;
}
原文鏈接:https://blog.csdn.net/xiangxin1030/article/details/128419504
相關(guān)推薦
- 2024-03-10 【Redis】Redis哨兵模式
- 2022-06-01 ASP.Net?Core中的日志與分布式鏈路追蹤_實(shí)用技巧
- 2022-06-14 Go語(yǔ)言學(xué)習(xí)之條件語(yǔ)句使用詳解_Golang
- 2023-04-06 python中注釋用法簡(jiǎn)單示例_python
- 2022-07-20 C語(yǔ)言詳細(xì)實(shí)現(xiàn)猜拳游戲流程_C 語(yǔ)言
- 2022-12-07 Golang源碼分析之golang/sync之singleflight_Golang
- 2022-10-02 SpringBoot前端后端分離之Nginx服務(wù)器下載安裝過(guò)程_nginx
- 2022-10-04 C語(yǔ)言實(shí)現(xiàn)倒置字符串的兩種方法分享_C 語(yǔ)言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支