C语言绘制简单时钟小程序
本文实例为大家分享了C语言绘制时钟小程序的具体代码,供大家参考,具体内容如下
先贴效果图给大家先看看
基本机制是通过获取系统的时钟去绘制图线进行展示
贴出代码 ,大家可以直接使用的 .程序我进一步的讲解
#include <graphics.h> #include <conio.h> #include <math.h> #include <time.h> #define high 480 #define width 640 #define pi 3.141592 int main() { initgraph(high, width); int center_x, center_y; center_y = width / 2; // 320 center_x = high / 2; // 240 SYSTEMTIME ti; //获取系统的时间 // 秒针属性 int secondEnd_x, secondEnd_y; int secondLenth = 120; secondEnd_x = center_x + secondLenth; secondEnd_y = center_y; float secondAngle = 0; // 分钟属性 int minuteEnd_x, minuteEnd_y; float minuteAngle = 0; int minuteLenth = 90; minuteEnd_x = center_x + minuteLenth; minuteEnd_y = center_y + minuteLenth; // 时钟属性 int hoursEnd_x, hoursEnd_y; float hoursAngle = 0; int hoursLenth = 60; hoursEnd_x = center_x + hoursLenth; hoursEnd_y = center_y + hoursLenth; BeginBatchDraw(); while (1) { // 获取时间 GetLocalTime(&ti); //绘制中心坐标 //setlinecolor(WHITE); //fillcircle(center_x, center_y, 2); // 绘制一个表盘 setbkcolor(BLACK); setlinestyle(PS_SOLID, 1); setlinecolor(WHITE); circle(center_x, center_y, 130); //outtextxy(center_x - 25, center_y + width / 6, "我的时钟"); // 输出字符串 (VC6) TCHAR s[] = _T("我的时钟"); outtextxy(210, 400, s); // 绘制表盘刻度 int x, y, i; for (i = 0; i < 60; i++) { x = center_x + 125 * sin(i * 2 * pi / 60); y = center_y - 125 * cos(i * 2 * pi / 60); // 一刻钟 if (i % 15 == 0) { bar(x - 5, y - 5, x + 5, y + 5); } else if ( i % 5 == 0) //5分钟 { circle(x, y, 3); } else { putpixel(x, y, WHITE); // 小白点 } } //转动秒针 secondAngle = ti.wSecond * 2 * pi / 60; // 2 * pi / 60 =一秒钟走的角度 ti.wSecond =系统当前秒 secondEnd_x = center_x + secondLenth * sin(secondAngle); secondEnd_y = center_y - secondLenth * cos(secondAngle); //转动分钟 minuteAngle = ti.wMinute * 2 * pi / 60 + secondAngle / 60; minuteEnd_x = center_x + minuteLenth * sin(minuteAngle); minuteEnd_y = center_y - minuteLenth * cos(minuteAngle); //转动时钟 hoursAngle = ti.wHour * 2 * pi / 12 + minuteAngle / 60; hoursEnd_x = center_x + hoursLenth * sin(hoursAngle); hoursEnd_y = center_y + hoursLenth * cos(hoursAngle); // 绘制秒针 setlinecolor(YELLOW); setlinestyle(PS_SOLID, 1); line(center_x, center_y, secondEnd_x, secondEnd_y); // 绘制分钟 setlinecolor(BLUE); setlinestyle(PS_SOLID, 3); line(center_x, center_y, minuteEnd_x, minuteEnd_y); // 绘制时钟 setlinecolor(RED); setlinestyle(PS_SOLID, 5); line(center_x, center_y, hoursEnd_x, hoursEnd_y); FlushBatchDraw(); Sleep(50); //隐藏 秒针 setlinecolor(BLACK); line(center_x, center_y, secondEnd_x, secondEnd_y); //隐藏 分针 setlinecolor(BLACK); line(center_x, center_y, minuteEnd_x, minuteEnd_y); //隐藏 时针 setlinecolor(BLACK); line(center_x, center_y, hoursEnd_x, hoursEnd_y); } EndBatchDraw(); _getch(); closegraph(); return 0; }
第一:秒钟角度转动的讲解
1.首先数学基础不错的都知道 , 一个圆圈 ,一圈的角度是 2π ,所以 一秒就是2π/60 .
2. angle 角度 就是指 和 12点钟方向的夹角 ,比如 1点和12点的夹角就是30° , 也就是 2π/12 .
第二:讲解表盘刻度的绘制
1. 将表盘的刻度分为60份 , 并且都表为小白点
2. 5份刻度记为1小时 ,标记为小白圈
3. 15份刻度为一刻钟=15分钟.
好了 ,程序就是这么简单! 大家可以去试试
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
赞 (0)