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

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

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

C語(yǔ)言實(shí)現(xiàn)繪制可愛(ài)的橘子鐘表_C 語(yǔ)言

作者:C語(yǔ)言小火車 ? 更新時(shí)間: 2023-02-04 編程語(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

欄目分類
最近更新