create an analog clock in C using graphics. This tutorial is written in a way that a beginner C graphics programmer can also understand.Before getting into the main let me explain the functions I have used in the program.创建模拟时钟 。 本教程是以C语言初学者也可以理解的方式编写的。在进入主要内容之前,让我解释一下我在程序中使用的功能。

Also Read: C/C++ Program to Create a Digital Stopwatch Also Read: Simple program to create a circular loading bar using graphics

另请参阅: 用于创建数字秒表的C / C ++程序 另请参见: 用于使用图形创建圆形加载条的简单程序

clockLayout() I’ve used this function to print the clock layout i.e. clock dial and the markings on the clock. If we observe clearly, the clock has hours marking each separated by 30 degrees and each hour is divided into 5 markings each making an angle of 6 degrees. So, iterating the markings for every 30 degrees gives hours and iterating markings with 6 degrees give minutes markings on the clock. The clock would look like this after executing this function.

clockLayout()我已经使用此函数来打印时钟布局,即时钟刻度盘和时钟上的标记。 如果我们清楚地观察到,时钟上的小时标记彼此分开30度,每个小时分为5个标记,每个标记成6度角。 因此,每30度迭代一次标记会产生数小时,而每6度迭代一次标记会产生数分钟的时钟标记。 执行此功能后,时钟将如下所示。

secHand() It is clear from the name that this gonna do something with the seconds hand. This function is going to get the present second from the system clock and incline the line according to a particular angle. Eg: if the present seconds is 5 then the angle of the seconds hand with respect to the vertical must be 30 degrees, i.e. 5*6=30.

secHand()从名称中可以明显看出,这将使秒针起作用 。 此功能将从系统时钟中获取当前秒数,并根据特定角度倾斜线路。 例如:如果当前秒数是5,则秒针相对于垂直线的角度必须为30度,即5 * 6 = 30。

minHand() This function fulfills the task of moving the minutes hand based on the system clock. The minutes hand must be inclined 6 degrees for every minute passing. Eg: if the elapsed minutes are 30 then the minutes hand angle must be making 180 degrees with the vertical.

minHand()此函数完成根据系统时钟移动分针的任务。 分针每经过一分钟必须倾斜6度。 例如:如果经过的分钟数为30,则分钟指针的角度必须与垂直方向成180度。

hrHand() This function is going to print an inclined hours line. The function is designed to get the present hour and also the no. of elapsed minutes from the system clock and incline the line according to a particular angle. For every hour elapsed the hour hand moves 30 degrees and every 12 minutes it moves 6 degrees.

hrHand()此函数将打印倾斜的小时线。 该功能旨在获取当前时间以及编号。 从系统时钟开始经过的分钟数,并根据特定角度倾斜线路。 时针每小时移动30度,每12分钟移动6度。

main() The first lines in main are graphic initialization, you must change the path “c:\turboc3\bgi\” to your compiler’s BGI file path otherwise program will not work. Coming to the while loop, the while loop iterates for every 100 milliseconds reprinting all the functions. This program is like getting the static picture of clock every second and combining all the pictures to make a moving analog clock.

main()main的第一行是图形初始化,必须将路径“ c:\ turboc3 \ bgi \”更改为编译器的BGI文件路径,否则程序将无法工作。 进入while循环时,while循环每隔100毫秒迭代一次,以重新打印所有功能。 该程序就像每秒获取时钟的静态图片,然后将所有图片合成一个移动的模拟时钟一样。

Also Read: Simple program to create a moving car in graphics

另请阅读: 用于在图形中创建移动汽车的简单程序

C语言模拟时钟程序 (Program for Analog Clock in C)

/* Graphical Analog Clock designed in C*/
/*Note press ctrl+pause break to stop the clock while executing in TC*/#include<stdio.h>
#include<graphics.h>
#include<stdlib.h>
#include<math.h>
#include<dos.h>
#include<time.h>#define PI 3.147void clockLayout();
void secHand();
void hrHand();
void minHand();
int maxx,maxy;void main()
{int gdriver=DETECT,gmode,error;initgraph(&gdriver,&gmode,"c:\turboc3\bgi\");error=graphresult();if(error!=grOk){printf("Error in graphics, code= %d",grapherrormsg(error));exit(0);}while(1){clockLayout();secHand();minHand();hrHand();sleep(1);                /* pausing the outputscreen for 1 sec */cleardevice();           /* clearing the previous picture of clock */}
}void clockLayout()
{int i,x,y,r;float j;maxx=getmaxx();maxy=getmaxy();for(i=1;i<5;i++){                   /* printing a round ring with outer radius of 5 pixel */setcolor(YELLOW);circle(maxx/2,maxy/2,120-i);}pieslice(maxx/2,maxy/2,0,360,5);    /* displaying a circle in the middle of clock */x=maxx/2+100;y=maxy/2;r=100;setcolor(BLUE);for(j=PI/6;j<=(2*PI);j+=(PI/6)){    /* marking the hours for every 30 degrees */pieslice(x,y,0,360,4);x=(maxx/2)+r*cos(j);y=(maxy/2)+r*sin(j);}x=maxx/2+100;y=maxy/2;r=100;setcolor(RED);for(j=PI/30;j<=(2*PI);j+=(PI/30)){  /* marking the minutes for every 6 degrees */pieslice(x,y,0,360,2);x=(maxx/2)+r*cos(j);y=(maxy/2)+r*sin(j);}
}void secHand()
{struct time t;int r=80,x=maxx/2,y=maxy/2,sec;float O;maxx=getmaxx();maxy=getmaxy();gettime(&t);                     /*getting the seconds in system clock */sec=t.ti_sec;O=sec*(PI/30)-(PI/2);           /* determining the angle of the line with respect to vertical */setcolor(YELLOW);line(maxx/2,maxy/2,x+r*cos(O),y+r*sin(O));
}void hrHand()
{int r=50,hr,min;int x,y;struct time t;float O;maxx=getmaxx();maxy=getmaxy();x=maxx/2,y=maxy/2;gettime(&t);                     /*getting the seconds in system clock */hr=t.ti_hour;min=t.ti_min;/* determining the angle of the line with respect to vertical */if(hr<=12)O=(hr*(PI/6)-(PI/2))+((min/12)*(PI/30));if(hr>12) O=((hr-12)*(PI/6)-(PI/2))+((min/12)*(PI/30));setcolor(BLUE);line(maxx/2,maxy/2,x+r*cos(O),y+r*sin(O));
}void minHand()
{int r=60,min;int x,y;float O;struct time t;maxx=getmaxx();maxy=getmaxy();x=maxx/2;y=maxy/2;gettime(&t);  /*getting the seconds in system clock */min=t.ti_min;O=(min*(PI/30)-(PI/2)); /* determining the angle of the line with respect to vertical */setcolor(RED);line(maxx/2,maxy/2,x+r*cos(O),y+r*sin(O));
}

Coment down below if you have any queries related to above program.

如果您对以上程序有任何疑问,请在下面注释。

翻译自: https://www.thecrazyprogrammer.com/2014/10/make-analog-clock-in-c-using-graphics.html

使用图形制作C语言的模拟时钟相关推荐

  1. Android模拟时钟和数字时钟示例

    在Android中, AnalogClock是两只手的时钟,一个代表小时,另一个代表分钟. DigitalClock看起来就像您手中的普通数字手表,以数字格式显示小时,分钟和秒. AnalogCloc ...

  2. 使用 HTML、CSS 和 JavaScript 制作模拟时钟(初学者教程)

    在线演示地址:https://haiyong.site/demo/clock1.html 文章目录 步骤1:创建制作此时钟的基本结构 步骤2:使用 CSS 代码设计背景 步骤3:使用 CSS 代码设计 ...

  3. c语言画图 钟表模拟程序,图形模拟时钟C语言课程设计

    图形模拟时钟C语言课程设计 更新时间:2017/2/2 9:03:00  浏览量:640  手机版 学号13082101182014-2015学年第二学期 <高级语言程序设计> 课程设计报 ...

  4. c语言图形时钟课程设计,图形模拟时钟C语言课程设计详解.doc

    学号<>课程设计报告图形模拟时钟专业:计算机科学与技术班级:13计科2班姓名:指导教师:陈广宏成绩: 计算机学院 2015 年 4月 30 日 目 录 1 设计内容及要求1 1.1 设计内 ...

  5. java模拟时钟课程设计_java课程设计-时钟图形模拟

    java课程设计-时钟图形模拟 计算机与信息工程系 <高级语言程序设计>课程设计报告课 程 设 计 任 务 书专 业 通信工程 班 级 13 级四班 姓 名 张凯铭设 计 起 止 日 期设 ...

  6. c语言编程模拟机械钟表行走,C语言课程设计报告-模拟时钟转动程序

    1. 课程设计报告题 目 课 程 名 称 结构化程序设计课程设计 院 部 名 称 专 业 班 级 学 生 姓 名 王蕾 学 号 课程设计地点 课程设计学时 指 导 教 师 金陵科技学院教务处制 程序设 ...

  7. c语言课设报告时钟vc环境,C语言课程设计报告模拟时钟转动程序

    C语言课程设计报告模拟时钟转动程序 课程设计报告题 目 课 程 名 称 结构化程序设计课程设计 院 部 名 称 专 业 班 级 学 生 姓 名 王蕾 学 号 课程设计地点 课程设计学时 指 导 教 师 ...

  8. c语言课设报告时钟vc环境,C语言课程设计报告模拟时钟转动程序.doc

    C语言课程设计报告模拟时钟转动程序 PAGE 课程设计报告 题 目 课 程 名 称 结构化程序设计课程设计 院 部 名 称 专 业 班 级 学 生 姓 名 王蕾 学 号 课程设计地点 课程设计学时 指 ...

  9. [C语言] 制作一个控制台的电子时钟

    使用c语言制作一个控制台的电子时钟 学习了c语言基本语法后,在学习了time.h的库文件,让我产生了想制作一款电子时钟的念头,那好就开始动手操作吧. 使用到下面这些技术: 首先必须先导入库 /**** ...

最新文章

  1. 使用.NET FileSystemWatcher对象监控磁盘文件目录的改变
  2. Java之——java.lang.NoSuchMethodException: [org.springframework.web.multipart.MultipartFile;.()
  3. HDU 2216(简单BFS)
  4. iOS Base64转码(使用ios7系统自带编码库 和 GMTBase64 两种方式)
  5. 中国通用电阻器市场趋势报告、技术动态创新及市场预测
  6. [转载]网站分析的最基本度量(3)——网站分析工具如何辨别UV
  7. 六星经典CSAPP-笔记(7)加载与链接(上)
  8. 房子怎么拆除_新规,可能拆除农村这4类房子,每户家庭可能获得40万
  9. wps vba6.3 宏插件下载
  10. SAXReader解析xml文件
  11. 【论文阅读-表情捕捉】High-quality Real Time Facial Capture Based on Single Camera
  12. Oracle19c 出现 ora-12514
  13. Ctrl + Alt + Left/Right键失效以及Ctrl + Space键被占用解决
  14. 北邮 计算机网络实验二
  15. 只有一重循环的排序——侏儒排序(Gnome Sort)
  16. lcd与led的区别
  17. Tecplot进阶——如何用Tecplot制作一张满足论文投稿要求的图片
  18. [转]JavaScript和html5 canvas生成圆形印章
  19. 自动生成 changelog.md,做一名有追求的工程师
  20. 新上线的“闪电”算法

热门文章

  1. 95后程序员卧底诈骗群只为反诈
  2. Guice——轻量级注解Guice简单之美
  3. 列奥纳多 • 全才 • 达 • 芬奇
  4. mac下配置VSCode的C语言开发环境
  5. sql-索引的作用(超详细),索引用法教程
  6. JAVA程序员成长之路的总结
  7. java毕业生设计运动会管理系统计算机源码+系统+mysql+调试部署+lw
  8. vue + echarts + ( bmap) 百度地图 实现公交、骑行、车辆 轨迹图
  9. A股滚动净利润增速最高排名
  10. 登陆页面的获取验证码