Search This Blog

Rotation of Wheel - C Program for Computer Graphics Lab

In computer graphics experiments, we need to rotate objects like wheels while doing animation. This is a simple c program that shows rotation of a wheel. A circle is drawn and four radial lines are drawn at 90 degree separation. These lines are drawn from center to a point on circle which depends on the value of theta, a variable. As the value of theta changes, the end points of the radial lines changes. Therefore, increasing the value of theta in a loop give counter clockwise rotaion and decreasing gives clockwise rotation.


The program is as follows:


#include<stdio.h>
#include<graphics.h>
#include<math.h>

void main()
{
int gd=DETECT,gm;
float theta=30,x0,y0,r=40,rad1,rad2;
initgraph(&gd,&gm,"..\\bgi");
x0=getmaxx()/2;
y0=getmaxy()/2;
while(!kbhit())
 {
 circle(x0,y0,r);
 rad1=theta*3.14f/180;
 rad2=(theta+90)*3.14f/180;
 line(x0,y0,x0+r*sin(rad1),y0+r*cos(rad1));
 line(x0,y0,x0+r*sin(rad2),y0+r*cos(rad2));
 line(x0,y0,x0-r*sin(rad1),y0-r*cos(rad1));
 line(x0,y0,x0-r*sin(rad2),y0-r*cos(rad2));
 theta-=8;
 delay(70);
 cleardevice();
 }
closegraph();
}

No comments: