Robot Arm Opengl Program in C

#include <GL/glut.h>
#include<stdio.h>
#include<math.h>

float xd[4],yd[4];
void Draw(){
 glClearColor(0.0, 0.0, 0.0, 0.0);
     glClear(GL_COLOR_BUFFER_BIT);
     glColor3f(0.0, 1.0, 0.0);
     glLineWidth(12);
 glBegin(GL_LINES);
        glVertex2f(xd[0],yd[0]);
        glVertex2f(xd[1],yd[1]);
 glEnd();

 glColor3f(1.0, 0.0, 0.0);
 glLineWidth(10);
 glBegin(GL_LINES);
        glVertex2f(xd[1],yd[1]);
        glVertex2f(xd[2],yd[2]);
 glEnd();

 glColor3f(1.0, 1.0, 1.0);
 glLineWidth(8);
 glBegin(GL_LINES);
        glVertex2f(xd[2],yd[2]);
        glVertex2f(xd[3],yd[3]);
 glEnd();

 glFlush();
}
void rotation(){
 float t,xh[10],yh[10];
 int i;
 float e,r;
 float theta = 15;
 t= (theta * 3.14/180);
 double coss = cos(t);
 double sinn = sin(t);

 for(i=2;i<=3;i++){
  xh[i]=xd[i]-xd[2];
  yh[i]=yd[i]-yd[2];
  e= (xh[i]*coss) - (yh[i]*sinn);
  r= (xh[i]*sinn) + (yh[i]*coss);
  xh[i]=e;
  yh[i]=r;
  xd[i]=xh[i]+xd[2];
  yd[i]=yh[i]+yd[2];
     }
}

void rotation1()
{
 float t,xh[10],yh[10];
 int i;
 float e,r;
 float theta = 15;
 t= -(theta * 3.14/180);
 double coss = cos(t);
 double sinn = sin(t);

 for(i=2;i<=3;i++){
  xh[i]=xd[i]-xd[2];
  yh[i]=yd[i]-yd[2];
  e= (xh[i]*coss) - (yh[i]*sinn);
  r= (xh[i]*sinn) + (yh[i]*coss);
  xh[i]=e;
  yh[i]=r;
  xd[i]=xh[i]+xd[2];
  yd[i]=yh[i]+yd[2];
     }
}

void rotation2()
{
 float t,xh[10],yh[10];
 int i;
 float e,r;
 float theta = 15;
 t= (theta * 3.14/180);
 double coss = cos(t);
 double sinn = sin(t);

 for(i=1;i<=3;i++){
       xh[i]=xd[i]-xd[1];
       yh[i]=yd[i]-yd[1];
e= (xh[i]*coss) - (yh[i]*sinn);
 r= (xh[i]*sinn) + (yh[i]*coss);
 xh[i]=e;
 yh[i]=r;
xd[i]=xh[i]+xd[1];
yd[i]=yh[i]+yd[1];
     }
}

void rotation3()
{
 float t,xh[10],yh[10];
 int i;
 float e,r;
 float theta = 15;
 t= -(theta * 3.14/180);
 double coss = cos(t);
 double sinn = sin(t);

 for(i=1;i<=3;i++){
       xh[i]=xd[i]-xd[1];
       yh[i]=yd[i]-yd[1];
       e= (xh[i]*coss) - (yh[i]*sinn);
   r= (xh[i]*sinn) + (yh[i]*coss);
   xh[i]=e;
   yh[i]=r;
   xd[i]=xh[i]+xd[1];
       yd[i]=yh[i]+yd[1];
     }
}

void rotation4()
{
 float t,xh[10],yh[10];
 int i;
 float e,r;
 float theta = 15;
 t= (theta * 3.14/180);
 double coss = cos(t);
 double sinn = sin(t);

 for(i=0;i<=3;i++){
       xh[i]=xd[i]-xd[0];
       yh[i]=yd[i]-yd[0];
       e= (xh[i]*coss) - (yh[i]*sinn);
   r= (xh[i]*sinn) + (yh[i]*coss);
   xh[i]=e;
   yh[i]=r;
   xd[i]=xh[i]+xd[0];
       yd[i]=yh[i]+yd[0];
     }
}

void rotation5()
{
 float t,xh[10],yh[10];
 int i;
 float e,r;
 float theta = 15;
 t= -(theta * 3.14/180);
 double coss = cos(t);
 double sinn = sin(t);

 for(i=0;i<=3;i++){
       xh[i]=xd[i]-xd[0];
       yh[i]=yd[i]-yd[0];
       e= (xh[i]*coss) - (yh[i]*sinn);
   r= (xh[i]*sinn) + (yh[i]*coss);
   xh[i]=e;
   yh[i]=r;
   xd[i]=xh[i]+xd[0];
       yd[i]=yh[i]+yd[0];
     }
}

void operation(GLubyte key, GLint x, GLint y)
{
 float currentX,currentY;
 int p=0;
 switch(key)
 {
  case 'q':
    rotation();
    Draw();
    break;

  case 'w':
    rotation2();
    Draw();
    break;

  case 'e':
    rotation4();
    Draw();
    break;

  case 'r':
    rotation1();
    Draw();
    break;

  case 't':
    rotation3();
    Draw();
    break;

  case 'y':
    rotation5();
    Draw();
    break;

  case 'u':
    exit(0);
 }
}

int main( int argc, char **argv ){
 float vxmax,vxmin,vymax,vymin;
 float wxmax,wymax,wxmin,wymin;
 wxmax = 600;
 wymax = 600;
 wxmin = wymin = 200;
 xd[0]=yd[0]=-0.2;

 xd[1]=0.0;

 yd[1]=0.0;

 xd[2]=0.2;

 yd[2]=0.2;

 xd[3]=0.3;

 yd[3]=0.3;

 glutInit( &argc, argv );
 glutInitDisplayMode( GLUT_RGB| GLUT_SINGLE);
 glutInitWindowSize( wxmax-wxmin,wymax-wymin);
 glutInitWindowPosition(wxmin,wymin);
 glutCreateWindow( "mamraj's Robotic Arm" );
 glutDisplayFunc(Draw);
 glutKeyboardFunc(operation);
 glutMainLoop();

 return 0;
}

Related Posts