Program To Rotate A Multi-Link Mechanism (Graphically) Along Any Joint

#include<stdio.h>
#include<GRAPHICS.h>
#include<math.h>
double radian(int val)
{
  return (M_PI*val)/180;
}

void rotate(int x_c,int y_c,int a,int b,double *x_p,double *y_p,int degree)
{
   double rad=radian(degree);
   *x_p=((a-x_c)*cos(rad))-((b-y_c)*sin(rad))+x_c;
   *y_p=((a-x_c)*sin(rad))+((b-y_c)*cos(rad))+y_c;
}

int main()
{
  double x_c,x1,x2,x3,x4,a,b;
  double y_c,y1,y2,y3,y4;
  int degree;
  /* request auto detection */
int gdriver = DETECT, gmode, errorcode;

/* initialize graphics mode */
initgraph(&gdriver, &gmode, "C:\\TurboC3\\BGI");

  x_c=getmaxx()/2;
  y_c=getmaxy()/2;

  x1=x_c+50;
  y1=y_c-30;

  x2=x1+50;
  y2=y1;

  x3=x2;
  y3=y2-50;

  x4=x3-30;
  y4=y3-10;

  setcolor(YELLOW);
  line(x_c,y_c,x1,y1);

  setcolor(RED);
  line(x1,y1,x2,y2);

  setcolor(GREEN);
  line(x2,y2,x3,y3);

  setcolor(BLUE);
  line(x3,y3,x4,y4);

  degree=0;
  while(!kbhit())
  {
    double c,d,e,f,g,h;
      /* Rotate Link-4 */
       /* setcolor(BLUE);
 rotate(x3,y3,x4,y4,&a,&b,degree);
 degree+=150;
 if(degree>150)
 degree=0;
 line(x3,y3,a,b);
 delay(1000);
 setcolor(BLACK);
 line(x3,y3,a,b); */


     /* Rotate Link-3 & Link-4 */
    /* setcolor(GREEN);
       rotate(x2,y2,x3,y3,&a,&b,degree);
       line(x2,y2,a,b);
       setcolor(BLUE);
       rotate(x2,y2,x4,y4,&c,&d,degree);
       line(a,b,c,d);
       degree+=90;
       if(degree>90)
       degree=0;
       delay(1000);
       setcolor(BLACK);
       line(x2,y2,a,b);
       line(a,b,c,d); */

    /* Rotate Link-2 & Link-3 & Link-4 */
      setcolor(RED);
       rotate(x1,y1,x2,y2,&a,&b,degree);
       line(x1,y1,a,b);
       setcolor(GREEN);
       rotate(x1,y1,x3,y3,&c,&d,degree);
       line(a,b,c,d);
       setcolor(BLUE);
       rotate(x1,y1,x4,y4,&e,&f,degree);
       line(c,d,e,f);
       delay(1000);
       degree-=90;
       if(degree<-90)
       degree=0;
       setcolor(BLACK);
       line(x1,y1,a,b);
       line(a,b,c,d);
       line(c,d,e,f);

      /* Rotate Link-1,Link-2,Link-3 & Link-4 */
     /*   setcolor(YELLOW);
       rotate(x_c,y_c,x1,y1,&a,&b,degree);
       line(x_c,y_c,a,b);
       setcolor(RED);
       rotate(x_c,y_c,x2,y2,&c,&d,degree);
       line(a,b,c,d);
       setcolor(GREEN);
       rotate(x_c,y_c,x3,y3,&e,&f,degree);
       line(c,d,e,f);
       setcolor(BLUE);
       rotate(x_c,y_c,x4,y4,&g,&h,degree);
       line(e,f,g,h);
       delay(1000);
       degree-=90;
       if(degree<-90)
       degree=0;
       setcolor(BLACK);
       line(x_c,y_c,a,b);
       line(a,b,c,d);
       line(c,d,e,f);
       line(e,f,g,h);
       */

  }

  getch();
 return 0;
}

Rotation of Link-2 , Link-3 & Link-4





No comments: