C语言绘制西瓜钟表

教育   2024-11-04 07:00   宁夏  

效果图

源码

///////////////////////////////////////////////////// 程序名称:西瓜钟表// 编译环境:Mictosoft Visual Studio 2022, EasyX_20200315(beta)// 作  者:luoyh <2864292458@qq.com>// 公 众 号:C语言研究// 最后修改:2024-11-4//
#include<graphics.h>#include<conio.h>#include<math.h>
SYSTEMTIME t; //定义变量保存当前时间 #define PI acos(-1.0)void DralDial(int X, int Y);void DrawHand(int hour, int minute, int second, int X, int Y);void XGR(int X, int Y);// 时钟int main(){ initgraph(1080,600); setbkcolor(RGB(252,228,226)); cleardevice(); BeginBatchDraw(); while (true) { setfillcolor(RGB(175,216,182)); solidcircle(540,300,200); XGR(540,300);
GetLocalTime(&t); //获取当前时间 DralDial(540, 300); //画表盘 DrawHand(t.wHour, t.wMinute, t.wSecond, 540, 300); //画表针 FlushBatchDraw(); } EndBatchDraw();}
/*画表盘*/void DralDial(int X, int Y){ setbkmode(TRANSPARENT); int x1, y1, x2, y2; setlinecolor(RGB(40, 52, 60)); setlinestyle(PS_SOLID | PS_ENDCAP_ROUND, 15); for (int i = 0; i <= 59; i++) { // 画60条短线 x1 = (int)(X + (sin(i * PI / 30) * 138)); y1 = (int)(Y - (cos(i * PI / 30) * 138)); x2 = (int)(X + (sin(i * PI / 30) * 145)); y2 = (int)(Y - (cos(i * PI / 30) * 145)); if (!(i % 5)) { line(x1, y1, x2, y2); } }}
//画指针 void DrawHand(int hour, int minute, int second, int X, int Y){ int xhour, yhour, xminute, yminute, xsecond, ysecond; //表心坐标系指针坐标 xhour = (int)(90 * sin(hour * PI / 6 + minute * PI / 360 + second * PI / 1800)); yhour = (int)(90 * cos(hour * PI / 6 + minute * PI / 360 + second * PI / 1800)); xminute = (int)(110 * sin(minute * PI / 30 + second * PI / 1800)); yminute = (int)(110 * cos(minute * PI / 30 + second * PI / 1800)); xsecond = (int)(125 * sin(second * PI / 30)); ysecond = (int)(125 * cos(second * PI / 30)); //画时针 setlinecolor(RGB(38,43,62)); setlinestyle(PS_SOLID, 5); line(X + xhour, Y - yhour, X, Y); //画分针 setlinestyle(PS_SOLID, 5); line(X + xminute, Y - yminute, X, Y); setlinecolor(RGB(130, 229, 178)); setlinestyle(PS_SOLID, 3); line(X + xsecond, Y - ysecond, X - xsecond / 5, Y + ysecond / 5); setfillcolor(BLACK); solidcircle(X, Y, 8);}
void XGR(int X,int Y){ setlinestyle(PS_SOLID, 2); float H = 1.f; float V = 0.95f; float S; double a = -0.00167597765362346; // 或者使用更精确的浮点数表示 double b = 0.801675977653623; // 或者使用更精确的浮点数表示 for (int i = 0; i < 180; i++) { S = a * i + b; setlinecolor(HSVtoRGB(H, S, V)); circle(X, Y, i); }}

动态效果

C语言研究
写给自己的笔记,时常写写,时常看看,仅此而已。