【EasyX】实时时钟
目录
- 实时时钟
- 1. 绘制静态秒针
- 2. 秒针的转动
- 3. 根据实际时间转动
- 4. 添加时针和分针
- 5. 添加表盘刻度
实时时钟
本博客介绍利用EasyX实现一个实时钟表的小程序,同时学习时间函数的使用。
本文源码可从github获取
1. 绘制静态秒针
第一步定义钟表的中心坐标center,它也是秒针的起点;定义秒针的长度secondLength、秒针的终点坐标secondEnd;利用setlinestyle函数设定线的型号和宽度,调用line函数绘制秒针。
#include <graphics.h>
#include <conio.h>
#include <cmath>using namespace std;struct Point
{int x;int y;
};#define High 480
#define Width 640int main(void)
{initgraph(Width, High);Point center, secondEnd;center.x = Width / 2;center.y = High / 2;int sencondLenth = Width / 5;secondEnd.x = center.x + sencondLenth;secondEnd.y = center.y;// 画秒针setlinestyle(PS_SOLID, 2); // 画实线,宽度为2个像素setcolor(WHITE);line(center.x, center.y, secondEnd.x, secondEnd.y);_getch();closegraph();return 0;
}
2. 秒针的转动
第二步实现秒针的转动,定义secondAngle为秒针对应的角度,利用三角几何知识求出秒针的终点坐标:
secondEnd.x = center.x + secondLenth * sin(secondAngle);
secondEnd.y = center.y - secondLenth * cos(secondAngle);
让角度循环变化,则实现了秒针转动的动画效果。
#include <graphics.h>
#include <conio.h>
#include <cmath>using namespace std;struct Point
{int x;int y;
};#define High 480
#define Width 640
#define PI 3.1415926int main(void)
{initgraph(Width, High);Point center, secondEnd;center.x = Width / 2;center.y = High / 2;int secondLenth = Width / 5;secondEnd.x = center.x + secondLenth;secondEnd.y = center.y;double secondAngle = 1.0; while (true){// 由角度决定终点坐标secondEnd.x = center.x + secondLenth * sin(secondAngle);secondEnd.y = center.y - secondLenth * cos(secondAngle);// 画秒针setlinestyle(PS_SOLID, 2); // 画实线,宽度为2个像素setcolor(WHITE);line(center.x, center.y, secondEnd.x, secondEnd.y);Sleep(100);setcolor(BLACK);line(center.x, center.y, secondEnd.x, secondEnd.y);// 秒针角度变化secondAngle = secondAngle * 2 * PI / 60 + 1;}_getch();closegraph();return 0;
}
3. 根据实际时间转动
第三步定义系统变量(SYSTEMTIME ti),通过GetLocalTime(&ti)获取当前时间,秒针的角度由实际时间决定,即secondAngle = ti.wSecond * 2 * PI/60。
#include <graphics.h>
#include <conio.h>
#include <cmath>using namespace std;struct Point
{int x;int y;
};#define High 480
#define Width 640
#define PI 3.1415926int main(void)
{initgraph(Width, High);Point center, secondEnd;center.x = Width / 2;center.y = High / 2;int secondLenth = Width / 5;secondEnd.x = center.x + secondLenth;secondEnd.y = center.y;double secondAngle;SYSTEMTIME ti;while (true){GetLocalTime(&ti);secondAngle = ti.wSecond * 2 * PI / 60;// 由角度决定终点坐标secondEnd.x = center.x + secondLenth * sin(secondAngle);secondEnd.y = center.y - secondLenth * cos(secondAngle);// 画秒针setlinestyle(PS_SOLID, 2); // 画实线,宽度为2个像素setcolor(WHITE);line(center.x, center.y, secondEnd.x, secondEnd.y);Sleep(100);setcolor(BLACK);line(center.x, center.y, secondEnd.x, secondEnd.y);}_getch();closegraph();return 0;
}
4. 添加时针和分针
第四步添加时针、分针,和秒针变化相比,他们的长度、宽度、颜色、旋转速度有一定的不同。
#include <graphics.h>
#include <conio.h>
#include <cmath>using namespace std;struct Point
{int x;int y;
};#define High 480
#define Width 640
#define PI 3.1415926int main(void)
{initgraph(Width, High);Point center, secondEnd, minuteEnd, hourEnd;center.x = Width / 2;center.y = High / 2;int secondLenth = Width / 5;int minuteLenth = Width / 6;int hourLenth = Width / 8;double secondAngle;double minuteAngle;double hourAngle;SYSTEMTIME ti;while (true){GetLocalTime(&ti);secondAngle = ti.wSecond * 2 * PI / 60;minuteAngle = ti.wMinute * 2 * PI / 60;hourAngle = (ti.wHour % 12) * 2 * PI / 12;// 由角度决定秒针终点坐标secondEnd.x = center.x + secondLenth * sin(secondAngle);secondEnd.y = center.y - secondLenth * cos(secondAngle);// 由角度决定分针终点坐标minuteEnd.x = center.x + minuteLenth * sin(minuteAngle);minuteEnd.y = center.y - minuteLenth * cos(minuteAngle);// 由角度决定时针终点坐标hourEnd.x = center.x + hourLenth * sin(hourAngle);hourEnd.y = center.y - hourLenth * cos(hourAngle);// 画秒针setlinestyle(PS_SOLID, 2); // 画实线,宽度为2个像素setcolor(WHITE);line(center.x, center.y, secondEnd.x, secondEnd.y);// 画分针setlinestyle(PS_SOLID, 4); // 画实线,宽度为4个像素setcolor(BLUE);line(center.x, center.y, minuteEnd.x, minuteEnd.y);// 画时针setlinestyle(PS_SOLID, 6); // 画实线,宽度为6个像素setcolor(RED);line(center.x, center.y, hourEnd.x, hourEnd.y);Sleep(10); // 延时10毫秒setcolor(BLACK);setlinestyle(PS_SOLID, 2); // 画实线,宽度为2个像素line(center.x, center.y, secondEnd.x, secondEnd.y);setlinestyle(PS_SOLID, 4); // 画实线,宽度为4个像素line(center.x, center.y, minuteEnd.x, minuteEnd.y);setlinestyle(PS_SOLID, 6); // 画实线,宽度为6个像素line(center.x, center.y, hourEnd.x, hourEnd.y);}int c = _getch();closegraph();return 0;
}
5. 添加表盘刻度
第五步绘制表盘,并可以利用outtextxy()函数在画面中输出文字,为了让时针、分针的转动更自然,对求解时针、分针的角度进行了改进。
#include <graphics.h>
#include <conio.h>
#include <cmath>using namespace std;struct Point
{int x;int y;
};#define High 480
#define Width 640
#define PI 3.1415926int main(void)
{initgraph(Width, High);Point center, secondEnd, minuteEnd, hourEnd;center.x = Width / 2;center.y = High / 2;int secondLenth = Width / 5;int minuteLenth = Width / 6;int hourLenth = Width / 8;double secondAngle;double minuteAngle;double hourAngle;SYSTEMTIME ti;BeginBatchDraw();while (true){// 绘制一个简单的表盘setlinestyle(PS_SOLID, 1); // 画实线,宽度为1个像素setcolor(WHITE);circle(center.x, center.y, Width / 4);// 画刻度int x, y, i;for (i = 0; i < 60; i++){x = center.x + int(Width/4.3*sin(PI*2*i/60));y = center.y - int(Width/4.3*cos(PI*2*i/60));if (i % 15 == 0) {bar(x - 5, y -5, x + 5, y + 5);}else if (i % 5 == 0) {circle(x, y, 3);}else {putpixel(x, y, WHITE);}}GetLocalTime(&ti);secondAngle = ti.wSecond * 2 * PI / 60;minuteAngle = ti.wMinute * 2 * PI / 60 + secondAngle / 60;hourAngle = ti.wHour*2*PI/12 + minuteAngle / 12;// 由角度决定秒针终点坐标secondEnd.x = center.x + secondLenth * sin(secondAngle);secondEnd.y = center.y - secondLenth * cos(secondAngle);// 由角度决定分针终点坐标minuteEnd.x = center.x + minuteLenth * sin(minuteAngle);minuteEnd.y = center.y - minuteLenth * cos(minuteAngle);// 由角度决定时针终点坐标hourEnd.x = center.x + hourLenth * sin(hourAngle);hourEnd.y = center.y - hourLenth * cos(hourAngle);// 画秒针setlinestyle(PS_SOLID, 2); // 画实线,宽度为2个像素setcolor(WHITE);line(center.x, center.y, secondEnd.x, secondEnd.y);// 画分针setlinestyle(PS_SOLID, 4); // 画实线,宽度为4个像素setcolor(BLUE);line(center.x, center.y, minuteEnd.x, minuteEnd.y);// 画时针setlinestyle(PS_SOLID, 6); // 画实线,宽度为6个像素setcolor(RED);line(center.x, center.y, hourEnd.x, hourEnd.y);FlushBatchDraw();Sleep(10); // 延时10毫秒setcolor(BLACK);setlinestyle(PS_SOLID, 2); // 画实线,宽度为2个像素line(center.x, center.y, secondEnd.x, secondEnd.y);setlinestyle(PS_SOLID, 4); // 画实线,宽度为4个像素line(center.x, center.y, minuteEnd.x, minuteEnd.y);setlinestyle(PS_SOLID, 6); // 画实线,宽度为6个像素line(center.x, center.y, hourEnd.x, hourEnd.y);}EndBatchDraw();int c = _getch();closegraph();return 0;
}
至此完成。
【EasyX】实时时钟相关推荐
- 【C语言】运用easyX——实现实时时钟,给自己的桌面加点料
额外知识点: SYSTEMTIME ti / GetLocalTime(&ti),获取系统时间同步代码,C语言实现时.分.秒针转动 目录 前言 设计思路 绘制画布 头函数 宏定义 绘制时钟 确 ...
- 关于STM32驱动DS1302实时时钟的一点思考
关于STM32驱动DS1302实时时钟的一点思考 之前用51驱动过DS1302,没用多久就输出了正确的时间.当时以为这块芯片其实没啥,很简单.但是现在用STM32做项目,用到同样的芯片,以为这有何难, ...
- 25 linux ndk 头文件_正点原子Linux第二十五章RTC实时时钟实验
1)资料下载:点击资料即可下载 2)对正点原子Linux感兴趣的同学可以加群讨论:935446741 3)关注正点原子公众号,获取最新资料更新 第二十五章RTC实时时钟实验 实时时钟是很常用的一个外设 ...
- STM32 RTC实时时钟
我用的是STM32库函数:两个知识点: 一.RTC时钟框图分析(重要) 二.时间是怎样显示出来的(简析) 一.RTC时钟框图分析(重要) 先熟悉一下几个知识点: 1 ...
- 8086实时时钟实验(一)——《x86汇编语言:从实模式到保护模式》05
1.代码清单 ;代码清单9-1;文件名:c09_1.asm;文件说明:用户程序 ;创建日期:2011-4-16 22:03;====================================== ...
- stm32之实时时钟RTC(掉电计时保持、秒中断、闹钟中断、溢出中断)
前言:stm32系列产品普遍都有实时时钟RTC模块,它提供一个掉电保持计时功能,掉电后由后备供电区域供电.除了提供时间和日期之外,还可以设置闹钟提醒,且可以在待机模式下设置闹钟唤醒系统.在一些小容量. ...
- esp32 rtc 时钟设置不对_「STM32」实时时钟(RTC)实验
实时时钟的特征和原理 RTCCLK:时钟来源 RTC的时钟有哪些来源呢? 如图,有3个渠道 来自于外部的LSE也就是外部的晶振 来自于HSE的128分频 来自于LSI 一般情况下我们都是采用外部晶振来 ...
- WS2812自动生成花样c语言,WS2812实时时钟程序 STM32F103C8T6+WS2812串行总线控制60个全彩LED - 下载 - 搜珍网...
压缩包 : abf533fe1eccb6c5afbb056f4619b12.rar 列表 WS2812实时时钟程序/APP/24Cxx/24cxx.c WS2812实时时钟程序/APP/24Cxx/2 ...
- arm linux应用调用rtc接口,ARMLinux驱动RTC(实时时钟)驱动分析
硬件平台:FL2440(S3C2440) 内核版本:Linux 2.6.28本文引用地址:http://www.eepw.com.cn/article/201611/317629.htm 主机平台:U ...
最新文章
- node 压缩模块速成
- teechart属性和方法
- Ace-editor 输入内容时光标闪动,定位错乱的解决方案
- Android退出程序(二)——利用广播机制
- Java 8中的新BigInteger方法
- 阿里云2020上云采购季,你适合买什么云产品?
- 银行业务队列简单模拟(队列queue)
- php html 停止工作,换行符php和html无法正常工作
- 【java笔记】类的抽象
- T-SQL行合并成列与列拆分成行
- MLOps- 吴恩达Andrew Ng Data Definition and Baseline Week3 论文等资料汇总
- ODB for mysql
- Learning from class-imbalanced data: Review of methods and applications 论文阅读
- 天翼云服务器怎么重装系统,天翼云操作系统介绍
- FIAA固定资产【05资产购置】
- docker portainer agent 安装
- JAVAWEB之JSTL标签
- Python培训:python中写文件的操作方法
- ffmpeg中合并音频文件
- 完美世界《钢的琴》《失恋33天》台湾引关注_0