Advertisement
Guest User

kg

a guest
Nov 17th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 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_dda(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. glVertex2i(x1, y1);
  29. x1 += xInc;
  30. y1 += yInc;
  31. }
  32. }
  33.  
  34. void init() {
  35. glClearColor(1.0, 1.0, 1.0, 0.0);
  36. glColor3f(0.0f, 0.0f, 0.0f);
  37. glPointSize(1.0f);
  38. gluOrtho2D(0.0f, 640.0f, 0.0f, 480.0f);
  39. }
  40.  
  41. void draw_circle(Point pC, GLfloat radius) {
  42. GLfloat step = 1/radius;
  43. GLfloat x, y;
  44.  
  45. for(GLfloat theta = 0; theta <= 360; theta += step) {
  46. x = pC.x + (radius * cos(theta));
  47. y = pC.y + (radius * sin(theta));
  48. glVertex2i(x, y);
  49. }
  50. }
  51.  
  52. Point pC = {200, 200};
  53. GLint radius = 150;
  54.  
  55. // radius of the needles from the center
  56. GLint hRadius = 120;
  57. GLint mRadius = 130;
  58. GLint sRadius = 140;
  59.  
  60. // angles of the three needles
  61. double hDegree = 0;
  62. double mDegree = 0;
  63. double sDegree = 0;
  64.  
  65. void display(void) {
  66. // Terminal Points for Needle
  67. Point pHour, pMinute, pSecond;
  68.  
  69. pHour.y = pC.y + (hRadius * sin(hDegree));
  70. pHour.x = pC.x + (hRadius * cos(hDegree));
  71.  
  72. pMinute.y = pC.y + (mRadius * sin(mDegree));
  73. pMinute.x = pC.x + (mRadius * cos(mDegree));
  74.  
  75. pSecond.y = pC.y + (sRadius * sin(sDegree));
  76. pSecond.x = pC.x + (sRadius * cos(sDegree));
  77.  
  78. glClear(GL_COLOR_BUFFER_BIT);
  79. glBegin(GL_POINTS);
  80. glColor3f(1.0, 0.0, 1.0);
  81. draw_circle(pC, radius);
  82. glColor3f(1.0, 0.0, 0.0);
  83. draw_dda(pC, pHour);
  84.  
  85. glColor3f(0.0, 1.0, 0.0);
  86. draw_dda(pC, pMinute);
  87.  
  88. glColor3f(0.0, 0.0, 1.0);
  89. draw_dda(pC, pSecond);
  90. glEnd();
  91. glFlush();
  92.  
  93. mDegree -= 0.001333333;
  94. sDegree -= 0.08;
  95. hDegree -= 0.0002733333;
  96.  
  97. }
  98.  
  99. void Timer(int value) {
  100. glutTimerFunc(33, Timer, 0);
  101. glutPostRedisplay();
  102. }
  103.  
  104. int main(int argc, char **argv) {
  105. glutInit(&argc, argv);
  106. glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
  107. glutInitWindowPosition(200, 200);
  108. glutInitWindowSize(640, 480);
  109. glutCreateWindow("Square");
  110. glutDisplayFunc(display);
  111. init();
  112. Timer(0);
  113. glutMainLoop();
  114.  
  115. return 0;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement