Advertisement
Guest User

clock

a guest
Nov 17th, 2019
1,108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. #include <gl/glut.h>
  2. #include <math.h>
  3.  
  4. struct Point {
  5. GLint x;
  6. GLint y;
  7. };
  8.  
  9. void draw_needle(Point p1, Point p2) {
  10. GLfloat dx = p2.x - p1.x;
  11. GLfloat dy = p2.y - p1.y;
  12.  
  13. GLfloat x1 = p1.x;
  14. GLfloat y1 = p1.y;
  15.  
  16. GLfloat step = 0;
  17.  
  18. if(abs(dx) > abs(dy)) {
  19. step = abs(dx);
  20. } else {
  21. step = abs(dy);
  22. }
  23.  
  24. GLfloat xInc = dx/step;
  25. GLfloat yInc = dy/step;
  26.  
  27. for(float i = 1; i <= step; i++) {
  28. glVertex2f(x1, y1);
  29. x1 += xInc;
  30. y1 += yInc;
  31. }
  32. }
  33.  
  34. void draw_circle(Point circle, GLfloat radius) {
  35. GLfloat step = 1/radius;
  36. GLfloat x, y;
  37.  
  38. for(GLfloat theta = 0; theta <= 360; theta += step) {
  39. x = circle.x + (radius * cos(theta));
  40. y = circle.y + (radius * sin(theta));
  41. glVertex2f(x, y);
  42. }
  43. }
  44.  
  45. Point circle = {320, 250};
  46. GLint radius = 150;
  47.  
  48. GLint nRadius = 140;
  49. double nDegree = 0;
  50.  
  51. void display(void) {
  52. Point needle;
  53.  
  54. needle.y = circle.y + (nRadius * sin(nDegree));
  55. needle.x = circle.x + (nRadius * cos(nDegree));
  56.  
  57. glClear(GL_COLOR_BUFFER_BIT);
  58. glBegin(GL_POINTS);
  59. glColor3f(1.0, 1.0, 1.0);
  60. draw_circle(circle, radius);
  61.  
  62. glColor3f(1.0, 1.0, 1.0);
  63. draw_needle(circle, needle);
  64. glEnd();
  65. glFlush();
  66.  
  67. nDegree -= 0.004;
  68. }
  69.  
  70. void Timer(int value) {
  71. glutTimerFunc(33, Timer, 0);
  72. glutPostRedisplay();
  73. }
  74.  
  75. void glutInit() {
  76. glClearColor(0.0, 0.0, 0.0, 1.0);
  77. gluOrtho2D(0.0f, 640.0f, 0.0f, 480.0f);
  78. }
  79.  
  80. int main(int argc, char **argv) {
  81. glutInit(&argc, argv);
  82. glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  83. glutInitWindowPosition(150, 150);
  84. glutInitWindowSize(640, 480);
  85. glutCreateWindow("Clock");
  86. glutDisplayFunc(display);
  87. glutInit();
  88. Timer(0);
  89. glutMainLoop();
  90.  
  91. return 0;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement