Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <gl/glut.h>
- #include <math.h>
- struct Point {
- GLint x;
- GLint y;
- };
- void draw_needle(Point p1, Point p2) {
- GLfloat dx = p2.x - p1.x;
- GLfloat dy = p2.y - p1.y;
- GLfloat x1 = p1.x;
- GLfloat y1 = p1.y;
- GLfloat step = 0;
- if(abs(dx) > abs(dy)) {
- step = abs(dx);
- } else {
- step = abs(dy);
- }
- GLfloat xInc = dx/step;
- GLfloat yInc = dy/step;
- for(float i = 1; i <= step; i++) {
- glVertex2f(x1, y1);
- x1 += xInc;
- y1 += yInc;
- }
- }
- void draw_circle(Point circle, GLfloat radius) {
- GLfloat step = 1/radius;
- GLfloat x, y;
- for(GLfloat theta = 0; theta <= 360; theta += step) {
- x = circle.x + (radius * cos(theta));
- y = circle.y + (radius * sin(theta));
- glVertex2f(x, y);
- }
- }
- Point circle = {320, 250};
- GLint radius = 150;
- GLint nRadius = 140;
- double nDegree = 0;
- void display(void) {
- Point needle;
- needle.y = circle.y + (nRadius * sin(nDegree));
- needle.x = circle.x + (nRadius * cos(nDegree));
- glClear(GL_COLOR_BUFFER_BIT);
- glBegin(GL_POINTS);
- glColor3f(1.0, 1.0, 1.0);
- draw_circle(circle, radius);
- glColor3f(1.0, 1.0, 1.0);
- draw_needle(circle, needle);
- glEnd();
- glFlush();
- nDegree -= 0.004;
- }
- void Timer(int value) {
- glutTimerFunc(33, Timer, 0);
- glutPostRedisplay();
- }
- void glutInit() {
- glClearColor(0.0, 0.0, 0.0, 1.0);
- gluOrtho2D(0.0f, 640.0f, 0.0f, 480.0f);
- }
- int main(int argc, char **argv) {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
- glutInitWindowPosition(150, 150);
- glutInitWindowSize(640, 480);
- glutCreateWindow("Clock");
- glutDisplayFunc(display);
- glutInit();
- Timer(0);
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement