今天给大家分享一个C语言图形库编程的源文件,你们如果有这方面兴趣的同学或者朋友可以尝试编译一下,当然,可能过程中会有报错和警告,如果有,你们可以加群:558502932,在群内发截图或者找管理问这方面问题,大家一起学习交流。 对这方面感兴趣或者想学习C/C++可以加群:558502932 好了,废话不多说,直接把代码送上: //图形编程:电子时钟 //数学知识 #include "time.h" #include "stdio.h" #include "graphics.h" #define PI 3.1415926536 //画表盘 void DrawDial() { setlinecolor(RGB(rand() % 255, rand() % 255, rand() % 255)); circle(320, 240, 2); setlinecolor(RGB(rand() % 255, rand() % 255, rand() % 255)); circle(320, 240, 60); setlinecolor(RGB(rand() % 255, rand() % 255, rand() % 255)); circle(405, 210, 30); outtextxy(320, 240, L"MoYg"); //绘制刻度 int x, y; for (int i = 0; i < 60; i++) { x = 320 + int(145 * sin(PI * 2 * i / 60)); y = 240 + int(145 * cos(PI * 2 * i / 60)); if (i % 15 == 0)//0,3,6,9 { bar(x - 5, y - 5, x + 5, y + 5);//实心的rectangle } else if (i % 5 == 0) //五分钟一格 { setfillcolor(RGB(rand() % 255, rand() % 255, rand() % 255)); fillcircle(x, y, 3); } else putpixel(x, y, RGB(rand() % 255, rand() % 255, rand() % 255)); } } //画表针 void DrawHand(int hour,int minute,int second) { //弧度值 double a_hour, a_min, a_sec; //各表针末端的坐标 int x_hour, y_hour, x_min, y_min, x_sec, y_sec; //计算弧度值:混合进制运算 a_sec = second * 2 * PI / 60; a_min = minute * 2 * PI/60 + a_sec / 60; a_hour = hour * 2 * PI/12 + a_min / 60; //末端位置 x_sec = int(120 * sin(a_sec)); y_sec = int(120 * cos(a_sec)); x_min = int(100 * sin(a_min)); y_min = int(100 * cos(a_min)); x_hour = int(70 * sin(a_hour)); y_hour = int(70 * cos(a_hour)); //绘制表针 setlinestyle(PS_SOLID, 2); setcolor(RGB(rand() % 255, rand() % 255, rand() % 255)); line(320 + x_sec, 240 - y_sec, 320 - x_sec / 2, 240 + y_sec / 2); setlinestyle(PS_SOLID, 5); setcolor(RGB(rand() % 255, rand() % 255, rand() % 255)); line(320 + x_min, 240 - y_min, 320 - x_min / 4, 240 + y_min / 4); setlinestyle(PS_SOLID, 7); setcolor(RGB(rand() % 255, rand() % 255, rand() % 255)); line(320 + x_hour, 240 - y_hour, 320 - x_hour / 5, 240 + y_hour / 5); } int _tmain(int argc,_TCHAR *argv[]) { //初始化640*480窗口 initgraph(640, 480); SYSTEMTIME NowTime; //绘图模式 GetLocalTime(&NowTime); //获取系统当前时间 while (true) { DrawDial(); DrawHand(NowTime.wHour, NowTime.wMinute, NowTime.wSecond);//结构成员当作实参 Sleep(1000); cleardevice(); GetLocalTime(&NowTime); //获取系统当前时间 } closegraph(); return 0; } 上述就是一个电子时钟所需代码了,如果你们在复制粘贴过程中发现有什么问题,可以加群:558502932,在群内或者找群内管理发出,管理或者群员会把帮忙解答。 最后希望大家能学好C/C++,大家一起学习交流。 |
|
来自: thchen0103 > 《c语言学习资料》