网页就可以实现数字时钟?简洁美观科技感~本期编程学习网就教大家如何实现C语言编程版时钟,大家可以一起试试
先看看效果图:
上代码:
*/ #include "SDL2/SDL.h" #include "SDL2/SDL_thread.h" #include<stdio.h> #include<math.h> #include<time.h> #define PI 3.1415926536 //画可旋转矩形,围绕矩形中心旋转/顺时针旋转 //相对于数学坐标象限4/3/2/1 //参数分别是渲染器,矩形中心点坐标x0/y0坐标,矩形w宽/h高,A旋转角度/弧度值 int myRect(SDL_Renderer *render,int x0,int y0,int w,int h,double A) { //源矩形四个顶点坐标 double Ax,Ay,Bx,By,Cx,Cy,Dx,Dy; //以矩形中心作为暂时坐标原点 Ax=-w/2.0;Ay=+h/2.0; Bx=Ax;By=-h/2.0; Cx=+w/2.0;Cy=By; Dx=Cx;Dy=Ay; SDL_Point p0[4]= { {Ax,Ay},//A {Bx,By},//B {Cx,Cy},//C {Dx,Dy} //D }; /* 逆时针旋转: x'=x*cos(a)-y*sin(a); y'=x*sin(a)+y*cos(a); */ //旋转A弧度后矩形ABCD四点新坐标 double A1x,A1y,B1x,B1y,C1x,C1y,D1x,D1y; D1x=Dx*cos(A)-Dy*sin(A)+x0; D1y=Dx*sin(A)+Dy*cos(A)+y0; C1x=Cx*cos(A)-Cy*sin(A)+x0; C1y=Cx*sin(A)+Cy*cos(A)+y0; A1x=Ax*cos(A)-Ay*sin(A)+x0; A1y=Ax*sin(A)+Ay*cos(A)+y0; B1x=Bx*cos(A)-By*sin(A)+x0; B1y=Bx*sin(A)+By*cos(A)+y0; //旋转后的矩形四个点坐标 SDL_Point p1[5]= { {A1x,A1y},//A {B1x,B1y},//B {C1x,C1y},//C {D1x,D1y}, //D {A1x,A1y}//A }; //画出旋转后矩形 SDL_RenderDrawLines(render,p1,5); return 0; } //画圆函数 int myCircle(SDL_Renderer *render,int x0,int y0,int r) { double x1=0.0,y1=0.0; static double TPI=6.2831853072; double temp=0.0; double step=0.0031415926536; for(int i=0;temp<TPI;i++) { temp=i*step; x1=x0-r*cos(temp); y1=y0-r*sin(temp); SDL_RenderDrawPoint(render, x1, y1); } return 0; } //画刻度 int myDraw(SDL_Renderer *render,int x0,int y0,int r) { double x1=0.0,y1=0.0; static double TPI=6.2831853072; double a1=0.1047197551; double st=0.0,x2=0.0,y2=00; //画刻度线60个 长度15像素 //夹角弧度 SDL_SetRenderDrawColor(render, 255, 255, 255, SDL_ALPHA_OPAQUE); for(int j=0;j<60;j++) { st=j*a1; x1=x0-r*cos(st); y1=y0-r*sin(st); x2=x1-15.0*cos(st); y2=y1-15.0*sin(st); if(j%5==0 && j!=0 && j!=15 && j!=30 && j!=45) { myRect(render,x1,y1,30,8,j*PI/30); } if(j==0) //9 { SDL_Rect rect = {x1-15,y1-4,30 , 8 }; SDL_RenderFillRect(render, &rect); } else if(j==15) //12 { SDL_Rect rect = {x1-4,y1-15,8 ,30 }; SDL_RenderFillRect(render, &rect); } else if(j==30) //3 { SDL_Rect rect = {x1-10,y1-4,30, 8}; SDL_RenderFillRect(render, &rect); } else if(j==45) //6 { SDL_Rect rect = {x1-4,y1-10,8 ,30}; SDL_RenderFillRect(render, &rect); } else { if(j!=5 && j!=10 && j!=20 && j!=25 && j!=35 && j!=40 &&j!=50 &&j!=55) { SDL_Point points[2] { {x1, y1}, {x2, y2} }; //画线 SDL_RenderDrawLines(render, points, 2); } } } return 0; } //实时获取东8区时间 //获取时/分/秒 int getTime(int *hour,int *min,int *sec) { time_t now; time(&now); struct tm *info; //获取GMT时间 info=gmtime(&now); *hour=(info->tm_hour+8)%24; *min=info->tm_min; *sec=info->tm_sec; return 0; } //动态画表针 int drawHand(SDL_Renderer *ren) { //时/分/秒 int h=0,m=0,s=0; //对应弧度 float Ah,Am,As; //获取时间 getTime(&h,&m,&s); //当前时针偏转弧度 Ah=h*PI/6.0+m*PI/360.0; //当前分针偏转弧度 Am=m*PI/30.0+s*PI/1800.0; //当前秒针偏转弧度 As=s*PI/30.0; //初始化时针在12时位置 时针长200宽12 //画时针 myRect(ren,360+100*sin(Ah),640-100*cos(Ah),12,200,Ah); //画分针 myRect(ren,360+120*sin(Am),640-120*cos(Am),8,240,Am); //画秒针 myRect(ren,360+140*sin(As),640-140*cos(As),4,280,As); return 0; } int main(int agrs,char *argv[]) { //定义窗口 SDL_Window *wnd=NULL; //定义渲染器 SDL_Renderer *render=NULL; //初始化所有子系统 SDL_Init(SDL_INIT_EVERYTHING); // 创建窗口 wnd = SDL_CreateWindow( // 标题 "我的SDL时钟", // 居中 SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, // 宽高 350, 350, //状态 SDL_WINDOW_SHOWN); //检查窗口是否创建成功 if (wnd == NULL) { printf("Could not create window: %s\n", SDL_GetError()); return 1; } //渲染器创建 render=SDL_CreateRenderer(wnd,1,1); //背景色黑色 SDL_SetRenderDrawColor(render,0,0,0,255); //刷新屏幕 SDL_RenderClear(render); //选择绘制颜色 SDL_SetRenderDrawColor(render, 255, 0, 0, SDL_ALPHA_OPAQUE); while(1){ //绘制钟盘 myDraw(render,360,640,300); SDL_SetRenderDrawColor(render, 255, 0, 0, SDL_ALPHA_OPAQUE); myCircle(render,360,640,3); myCircle(render,360,640,4); SDL_SetRenderDrawColor(render, 0, 0, 255, SDL_ALPHA_OPAQUE); myCircle(render,360,640,11); myCircle(render,360,640,12); drawHand(render); SDL_RenderPresent(render); } //清理 SDL_DestroyRenderer(render); SDL_DestroyWindow(wnd); SDL_Quit(); return 0; }安卓系统下测试可行,以上就是“用c语言编程实现一个科技感的时钟(用c语言编写一个时钟程序)”的详细内容,想要了解更多C语言教程欢迎持续关注编程学习网
扫码二维码 获取免费视频学习资料
- 本文固定链接: http://phpxs.com/post/9763/
- 转载请注明:转载必须在正文中标注并保留原文链接
- 扫码: 扫上方二维码获取免费视频资料
查 看2022高级编程视频教程免费获取