首页 > 其他分享 >C语言—用EaxyX绘制实时钟表

C语言—用EaxyX绘制实时钟表

时间:2024-04-04 22:59:22浏览次数:12  
标签:secondend center hourend int C语言 EaxyX width minuteend 钟表

 代码效果如图

#undef UNICODE
#undef _UNICODE
#include<graphics.h>
#include<conio.h>
#include<math.h>

#define width 640
#define high 480
#define PI 3.14159

int main()
{
	initgraph(width, high);
	int center_x, center_y;
	center_x = width / 2;
	center_y = high / 2;
	int secondlen = width / 5;
	int minutelen = width / 6;
	int hourlen = width / 7;

	int secondend_x, secondend_y;
	int minuteend_x, minuteend_y;
	int hourend_x, hourend_y;
	float secondangle;
	float minuteangle;
	float hourangle;

	SYSTEMTIME ti;

	BeginBatchDraw();
	while (1)
	{
		setbkcolor(RGB(0,200,200));//只是设置背景色,还未填充背景
		cleardevice();//用背景色来清空背景

		setlinestyle(PS_SOLID, 2);//画表盘
		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)
				solidrectangle(x - 5, y - 5, x + 5, y + 5);
			else if (i % 5 == 0)
				circle(x, y, 3);
			else
				putpixel(x, y, WHITE);
		}

		outtextxy(center_x - 25, center_y + width / 6, "我的时钟");

		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 + secondlen * sin(secondangle);
		secondend_y = center_y - secondlen * cos(secondangle);

		minuteend_x = center_x + minutelen * sin(minuteangle);
		minuteend_y = center_y - minutelen * cos(minuteangle);

		hourend_x = center_x + hourlen * sin(hourangle);
		hourend_y = center_y - hourlen * cos(hourangle);

		setlinestyle(PS_SOLID, 2);//画时针分针和秒针
		setcolor(YELLOW);
		line(center_x, center_y, secondend_x, secondend_y);
		setlinestyle(PS_SOLID, 4);
		setcolor(BLUE);
		line(center_x, center_y, minuteend_x, minuteend_y);
		setlinestyle(PS_SOLID, 6);
		setcolor(RED);
		line(center_x, center_y, hourend_x, hourend_y);

		FlushBatchDraw();
		Sleep(10);

		setcolor(BLACK);//隐藏前一秒的时针分针和秒针
		setlinestyle(PS_SOLID, 2);
		line(center_x, center_y, secondend_x, secondend_y);
		setlinestyle(PS_SOLID, 4);
		line(center_x, center_y, minuteend_x, minuteend_y);
		setlinestyle(PS_SOLID, 6);
		line(center_x, center_y, hourend_x, hourend_y);
	}
	EndBatchDraw();
	_getch();
	closegraph();
	return 0;
}

标签:secondend,center,hourend,int,C语言,EaxyX,width,minuteend,钟表
From: https://blog.csdn.net/2301_80163571/article/details/137379221

相关文章

  • C语言经典例题(17) --- 最小公倍数、单词倒置、你是天才吗?、完美成绩、判断整数的奇偶
    1.最小公倍数正整数A和正整数B的最小公倍数是指能被A和B整除的最小的正整数,设计一个算法,求输入A和B的最小公倍数。输入描述:输入两个正整数A和B。输出描述:输出A和B的最小公倍数。输入:57输出:35#include<stdio.h>intmain(){inta=0;intb=0;int......
  • C语言经典例题(18) --- 判断字母、三角形判断、衡量人体胖瘦程度、翻转金字塔图案、平
    1.判断是不是字母题目描述:KK想判断输入的字符是不是字母,请帮他编程实现。输入描述:多组输入,每一行输入一个字符。输出描述:针对每组输入,输出单独占一行,判断输入字符是否为字母,输出内容详见输出样例。输入:A6输出:Aisanalphabet.6isnotanalphabet......
  • 16.C语言错题整理
    一些C语言错题//求n的阶乘intsum=1;intn;printf("请输入n的值:");scanf("%d",&n);for(intj=1;j<n+1;++j){sum*=j;}printf("%d\n",sum);inthee=0;intb=1;for(......
  • DFS 全排列问题 C语言代码
    深度优先搜索(DFS)是一种遍历算法,尽可能深地向子树中的结点搜索,直到达到一定的深度,再回溯到上层的结点,继续搜索未被访问的结点。全排列问题给定4个数1234,求他们所有可能的排列结果。代码#include<stdio.h>voiddfs(intx);inti;inta[4];intresult[4];/......
  • C语言 | Leetcode C语言题解之第8题字符串转换整数atoi
    题目:题解:intmyAtoi(char*s){inti=0;intout=0;intpol=1;intlen=strlen(s);if(len==0)return0;while(s[i]=='')i++;//删除空格if(s[i]=='-'){//判断正负pol=-1;i++;}else......
  • c语言中关于字符数组赋值问题
    一维数组代码#include<iostream>#include<cstring>#include<algorithm>usingnamespacestd;constintN=1010;charstr[N];charst[N];chars1[N];chars2[N];/*abcdeabcdeabcdeabcde*/intmain(){ scanf("%s",&str+1); ......
  • c语言:模拟字符串拷贝功能(strcpy),面试题
    面试题:优化中的优化(10分满分)字符串拷贝:是将一个字符串的内容复制到另一个字符串中的操作。运用函数模拟字符串拷贝:(5分)模拟字符串拷贝#include<stdio.h>voidmy_strcpy(char*dest,char*str){ while(*str!='\0') { *dest=*str; str++; dest++; } *dest......
  • C语言入门:预处理器指令详解
    目录一宏定义A.不带参数的宏定义:B.带参数的宏定义(宏函数):C.宏定义的注意事项:二条件编译A.#ifdef/#ifndef:B.#if/#elif/#else/#endif:C.#pragmaonce:三头文件包含A.#include:B.头文件包含顺序:C.编译警告:四C语言的优缺点A.C语言的优点:B.C语言的缺点:五C......
  • C语言实现牛顿迭代法(Newton-Raphson Method)
    目录前言A.建议B.简介一代码实现二时空复杂度A.时间复杂度B.空间复杂度C.总结三优缺点A.优点:B.缺点:C.总结:四现实中的应用前言A.建议1.学习算法最重要的是理解算法的每一步,而不是记住算法。2.建议读者学习算法的时候,自己手动一步一步地运行算法。B.......
  • C语言实现龙格-库塔方法(Runge-Kutta Methods)
    前言A.建议1.学习算法最重要的是理解算法的每一步,而不是记住算法。2.建议读者学习算法的时候,自己手动一步一步地运行算法。B.简介龙格-库塔方法(Runge-KuttaMethods)是一种用于求解常微分方程(ODEs)的数值积分方法,尤其适用于一阶非线性微分方程组。一代码实现在C语言......