Cycle

Write a program to simulate the man riding a bicycle.



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

void main()
{
	int gd=DETECT,gm,i,j=-168;
	double x,y,x1,y1,x2,y2,x3,y3,
x4,y4,x5,y5,step=(12*M_PI)/360,a=0.0;
	initgraph(&gd,&gm,"c:\\tc\\bgi");
	while(!kbhit())
	{
	circle(50+j,270,28);
	setfillstyle(SOLID_FILL,BLACK);
	for(i=0;i<=360;i+=15)
		sector(50+j,270,i,i+15,25,25);

	circle(140+j,270,28);
	for(i=0;i<=360;i+=15)
	       sector(140+j,270,i,i+15,25,25);

	x=90+15*cos(a);
	y=275+15*sin(a);
	x1=90-15*cos(a);
	y1=275-15*sin(a);
	setcolor(WHITE);
	line(90+j,275,x+j,y);
	setcolor(WHITE);
	a+=step;
	if(a>=2*M_PI)
	a=0.00;
	x2=x+10; y2=y-35;
	x3=x1+10; y3=y1-35;
	x4=x-40; y4=y-5;
	x5=x+50; y5=y-5;
	circle(x4+j,y4,4);
	circle(x5+j,y5,4);
	setfillstyle(SOLID_FILL,WHITE);
	floodfill(x4+j,y4,WHITE);
	floodfill(x5+j,y5,WHITE);

	line(x+j,y,x2+j,y2);
	line(70+j,222,x2+j,y2);

	setcolor(GREEN);
	setfillstyle(EMPTY_FILL,WHITE);
	for(i=0;i<=360;i+=72)
		sector(90+j,275,i,i+72,8,8);
	setcolor(WHITE);

	line(50+j,268,90+j,273);
	line(50+j,271,90+j,276);
	line(50+j,268,50+j,272);
	line(90+j,273,90+j,277);

	line(49+j,268,71+j,232);
	line(51+j,268,73+j,232);

	line(71+j,232,89+j,275);
	line(73+j,232,91+j,275);

	line(139+j,270,124+j,232);
	line(141+j,270,126+j,232);

	line(72+j,231,125+j,231);
	line(72+j,233,125+j,233);

	line(90+j,274,125+j,233);
	line(90+j,276,125+j,235);

	line(72+j,232,71+j,227);
	line(68+j,227,80+j,226);
	line(63+j,222,68+j,227);
	line(63+j,222,77+j,223);
	line(77+j,223,80+j,226);
	setfillstyle(SOLID_FILL,WHITE);
	floodfill(70+j,225,WHITE);

	line(125+j,232,123+j,225);
	line(123+j,225,129+j,225);
	line(115+j,221,129+j,221);
	rectangle(115+j,220,125+j,222);
	arc(129+j,223,270,90,2);
	setcolor(WHITE);

	line(50+j,269,89+j,267);
	line(50+j,271,89+j,283);

	setcolor(WHITE);
	line(90+j,275,x1+j,y1);

	setcolor(WHITE);
	line(x1+j,y1,x3+j,y3);
	line(70+j,222,x3+j,y3);

	line(70+j,222,75+j,180);
	circle(75+j,168,12);

	line(75+j,190,95+j,210);
	line(95+j,210,120+j,220);
	line(75+j,190,105+j,205);
	line(105+j,205,120+j,219);
	delay(10);
	j++;
	if(j==getmaxx())
	j=-168;
	clearviewport();
	}
	closegraph();
}



Output
1

Pulley

Write a program to animate the movement of the pulley.


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
	int gd=DETECT,gm,x=320,y=100,d=0,dy=40,dyr=429-y;
	initgraph(&gd,&gm,"c:\\tc\\bgi");
	while(!kbhit())
	{
		cleardevice();
		circle(x,y,40);
		pieslice(x,y,0+d,20+d,40);
		pieslice(x,y,120+d,140+d,40);
		pieslice(x,y,240+d,260+d,40);
		d=(d+10)%100;
		line(x-40,y,x-40,y+dy);
		rectangle(x-70,y+dy,x-10,y+dy+50);
		dy+=10;

		line(x+40,y,x+40,y+dyr);
		rectangle(x+10,y+dyr,x+70,y+dyr+20);
		dyr-=10;
		line(10,451,620,451);
		if(y+dy+50>=451)
			break;
		if(y+dyr+20<=y+60)
			break;
		delay(100);
	}
	getch();
}


Output
1

Man Walking in Rain

Write a program to simulate a man walking in rain with an umbrella.


#include<conio.h>
#include<stdio.h>
#include<graphics.h>
void displayMan(int x,int y)
{
	circle(x,y,10);			//face
	line(x,y+10,x,y+30); 		//neck
	line(x,y+30,x-20,y+40);    //left hand
	line(x,y+30,x+20,y+40);    //right hand
	line(x+20,y+40,x+30,y+30);
	line(x,y+30,x,y+70);      	//body
	line(x+30,y+30,x+30,y-90);	//umbrella
	pieslice(x+30,y-30,0,180,55);       

}


void main()
{
	int gd=DETECT, gm,i,d=0,x=50,y=340,shouldMove=1;
	int rx,ry;
	initgraph(&gd,&gm,"C:\\tc\\bgi");
	while(!kbhit())
	{
		cleardevice();
		displayMan(x,340);
		line(0,430,639,430);
		
for(i=0;i<500;i++)
		{
			rx=rand()%639;
			ry=rand()%439;
			if(rx>=(x-40)&&rx<=(x+110))
				if(ry>=(y-50)&&ry<=479)
					continue;
			line(rx-10,ry+10,rx,ry);
		}

		//legs
		if(shouldMove)
		{
			if(d<20)
				d+=4;
			else
				shouldMove=0;
			line(x,y+70,x-d,y+90);
			line(x,y+70,x+d,y+90);
		}
		else
		{
			if(d>0)
				d-=4;
			else
				shouldMove=1;
			line(x,y+70,x-d,y+90);
			line(x,y+70,x+d,y+90);
		}
		delay(200);
		x=(x+10)%639;
	}
	getch();
}

Output
1

Wall Clock

Write a program to simulate the wall clock.


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void main()
{
	int gdriver = DETECT, gmode, errorcode;


	int theta=270,x=0,sec=0,min=1,y=66;
	initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
	while(!kbhit())
	{
		cleardevice();

		circle(350,250,100);
		circle(350,250,101);
		pieslice(350,250,301-y,300-y,60);
		pieslice(350,250,91-x,90-x,90);
		pieslice(350,250,360-theta+1,360-theta,100);
		theta=theta+6;
		setcolor(WHITE);
		if(sec%60==0)
		{
			x=x+6;
			min=min+1;
		}
		sec=sec+1;
		if(min%60==0)
		{
			y=y+6;
		}
		delay(1000);
	}

}


Output
1

Rotation of a Wheel

Write a program to implement rotation of a wheel.


#include<conio.h>
#include<stdio.h>
#include<graphics.h>
#include<math.h>
void wheel1(int x,int y)
{
	int r=20;
	circle(x,y,r);
	line(x,y-r,x,y+r);
	line(x-r,y,x+r,y);
}
void wheel2(int x,int y)
{
	int r=20,fac;
	fac=r*(sin((45*M_PI)/180));
	circle(x,y,r);
	line(x+fac,y+fac,x-fac,y-fac);
	line(x-fac,y+fac,x+fac,y-fac);

}
void main()
{
	int gd=DETECT,gm,x=100,y=380;
	int c=0;
	initgraph(&gd,&gm,"c:\\tc\\bgi");
	while(!kbhit())
	{
		cleardevice();
		c++;
		line(0,400,639,400);
		if(c%2==0)
			wheel1(x,y);
		else
			wheel2(x,y);
		x=(x+10)%639;
		delay(200);
	}
	getch();
}


Output
1

Polygon Clipping

Write a program to implement Polygon Clipping.


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int cx1,cx2,cy1,cy2,i;
float x[50],y[50],xt[50],yt[50];
void check(float x1,float y1,float x2,float y2,int side);
int gd=DETECT,gm;
int nt=0,k=0,g,h,n;
void display()
{
	int i;
	for(i=0;i<n-1;i++)
		line(x[i],y[i],x[i+1],y[i+1]);
	line(x[i],y[i],x[0],y[0]);
	getch();
}
void main()
{
	int p,j;
	initgraph(&gd,&gm,"c:\\tc\\bgi");
	printf("\nEnter values for cx1,cy1,cx2,cy2\t:");
	scanf("%d%d%d%d",&cx1,&cy1,&cx2,&cy2);
	printf("\nEnter value of n\t:");
	scanf("%d",&n);

	putpixel(0,0,WHITE);
	putpixel(639,479,WHITE);
	for(p=0;p<n;p++)
	{
		printf("enter x[%d],y[%d]\t:",p+1,p+1);
		scanf("%f%f",&x[p],&y[p]);
	}
	g=x[n-1];h=y[n-1];
	setcolor(5);
	rectangle(cx1,cy1,cx2,cy2);
	getch();
	display();
	for(j=1;j<=4;j++)
	{
		k=0;
		i=0;
		for(i=0;i<n-1;)
		{
			check(x[i],y[i],x[i+1],y[i+1],j);
			i++;
		}
		check(x[i],y[i],x[0],y[0],j);
		n=k;
		for(p=0;p<nt;p++)
		{
			x[p]=xt[p];
			y[p]=yt[p];

		}
	}
	setcolor(WHITE);
	rectangle(cx1,cy1,cx2,cy2);
	setcolor(WHITE);
	display();
}
void check(float x1,float y1,float x2,float y2,int side)
{
	float m;
	  //int i=0;
	if(x1!=x2)
	m=(y2-y1)/(x2-x1);
	switch(side)
	{
		case 1:if(x1<cx1&&x2>cx1)
		{
			xt[k]=cx1;
			yt[k]=m*(cx1-x1)+y1;
			k++;
			xt[k]=x2;
			yt[k]=y2;
			k++;

		}
		else if(x1>=cx1&&x2>=cx1)
		{
			xt[k]=x2;
			yt[k]=y2;
			k++;
		}
		else if(x1>cx1&&x2<cx1)
		{
			xt[k]=cx1;
			yt[k]=m*(cx1-x1)+y1;
			k++;
		}
		break;
		case 2:if(x1>cx2&&x2<cx2)
		{
			xt[k]=cx2;
			yt[k]=m*(cx2-x1)+y1;
			k++;
			xt[k]=x2;
			yt[k]=y2;
			k++;
		}
		else if(x1<cx2&&x2>cx2)
		{
			xt[k]=cx2;
			yt[k]=m*(cx2-x1)+y1;
			k++;
		}
		else if(x1<=cx2&&x2<=cx2)
		{
			xt[k]=x2;
			yt[k]=y2;k++;
		}
		break;
		case 3:if(y1>cy2&&y2<cy2)
		{
			yt[k]=cy2;
			if(x1==x2)
				xt[k]=x1;
			else
				xt[k]=(cy2-y2)/m+x2;
			k++;
			xt[k]=x2;
			yt[k]=y2;
			k++;
		}
		else if(y1<=cy2&&y2<=cy2)
		{
			xt[k]=x2;
			yt[k]=y2;
			k++;
		}
		else if(y1<cy2&&y2>cy2)
		{
			yt[k]=cy2;
			if(x1==x2)
				xt[k]=x1;
			else
			xt[k]=(cy2-y2)/m+x2;
			k++;
		}
		break;
		case 4:if(y1<cy1&&y2>cy1)
		{
			yt[k]=cy1;
			if(x1==x2)
				xt[k]=x1;
			else
			xt[k]=(cy1-y1)/m+x1;
			k++;
			xt[k]=x2;
			yt[k]=y2;
			k++;
		}
		else if(y1>cy1&&y2<cy1)
		{
			yt[k]=cy1;
			if(x1==x2)
				xt[k]=x1;
			else
			xt[k]=(cy1-y2)/m+x2;
			k++;
		}
		else if(y1>=cy1&&y2>=cy1)
		{
			yt[k]=y2;
			xt[k]=x2;
			k++;
		}
		break;
	}
	nt=k;
}


Output
1

Line Clipping

Write a program to implement Line Clipping.


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

#define LEFT 1
#define RIGHT 2
#define BOTTOM 4
#define TOP 8

#define INSIDE(a) (!a)
#define REJECT(a,b) (a&b)
#define ACCEPT(a,b) (!(a|b))

int gd=DETECT,gm;
struct wc
{
	float x,y;
}pt1,pt2;

struct dc
{
	float x,y;
}winmin,winmax;

int encode(struct wc pt,struct dc winmin,struct dc winmax)
{
	int code=0x00;
	if(pt.x<winmin.x)
		code=code|LEFT;
	if(pt.x>winmax.x)
		code=code|RIGHT;
	if(pt.y<winmin.y)
		code=code|BOTTOM;
	if(pt.y>winmax.y)
		code=code|TOP;
	return(code);
}
void swapPts(struct wc *p1,struct wc *p2)
{
	struct wc temp;
	temp=*p1;
	*p1=*p2;
	*p2=temp;
}
void swapCode(int *c1,int *c2)
{
	int temp;
	temp=*c1;
	*c1=*c2;
	*c2=temp;
}
int main()
{
	int x,y,draw=0,code1,code2,c,done=0;
	float m;
	int poly[10]={150,200,350,200,350,400,150,400,150,200};
	initgraph(&gd,&gm,"c:\\tc\\bgi");
	printf("\nEnter the coordinates of the first point: ");
	scanf("%d%d",&x,&y);
	pt1.x=x;
	pt1.y=y;
	printf("\nEnter the coordinates of he second point: ");
	scanf("%d%d",&x,&y);
	pt2.x=x;
	pt2.y=y;

	setcolor(3);
	line(pt1.x,pt1.y,pt2.x,pt2.y);
	drawpoly(5,poly);
	winmin.x=150;
	winmax.x=350;
	winmin.y=200;
	winmax.y=400;
	printf("\n1.CLIP\n2.EXIT\nENTER UR CHOICE:\t");
	scanf("%d",&c);
	switch(c)
	{
		case 1:
			while(!done)
			{
				code1=encode(pt1,winmin,winmax);
				code2=encode(pt2,winmin,winmax);
				if(ACCEPT(code1,code2))
				{
					done=1;
					draw=1;
				}
				else if(REJECT(code1,code2))
					done=1;
				else
				{
					if(INSIDE(code1))
					{
						swapPts(&pt1,&pt2);
						swapCode(&code1,&code2);
					}
					if(pt2.x!=pt1.x)
						m=(pt2.y-pt1.y)/(pt2.x-pt1.x);
					if(code1 & LEFT)
					{
						pt1.y+=(winmin.x-pt1.x)*m;
						pt1.x=winmin.x;
					}
					else if(code1 & RIGHT)
					{
						pt1.y+=(winmax.x-pt1.x)*m;
						pt1.x=winmax.x;
					}
					else if(code1 & BOTTOM)
					{
					   if(pt2.x!=pt1.x)
						pt1.x+=(winmin.y-pt1.y)*m;
						pt1.y=winmin.y;
					}
					else if(code1 & TOP)
					{
					    if(pt2.x!=pt1.x)
						pt1.x+=(winmax.y-pt1.y)*m;
						pt1.x=winmax.y;
					}
				}
			}

			if(draw)
			{
				//clrscr();
				//initgraph(&gd,&gm,"c:\\tc\\bgi");
				setcolor(WHITE);
				drawpoly(5,poly);
				line(pt1.x,pt1.y,pt2.x,pt2.y);
			}
			break;
		case 2:
			break;
		default:
			printf("\nINVALID OPTION");
	}
	getch();
	return(0);
}


Output
1

Generate shape of letter D

Write a program to generate the shape of letter D


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
	int gd=DETECT,gm,x,y,r=100;
	initgraph(&gd,&gm,"c:\\tc\\bgi");
	printf("\nEnter the coordinates to draw the 'D' : ");
	scanf("%d %d",&x,&y);
	arc(x,(y+r),270,90,r);
	arc(x,(y+r),260,100,r+20);
	line(x,y,x,y+(2*r));
	line(x-20,y-20,x-20,y+(2*r)+20);
	setfillstyle(1,YELLOW);
	floodfill(x-1,y-1,WHITE);
	getch();
}


Output
1

2-D Transformations

Write a program to implement the following 2-D Transformations on a triangle:
• Translation
• Rotation
• Scaling



#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<stdlib.h>
int points[8],pointsCopy[8];
void triangle();
void reinit();
void main()
{
	int gd=DETECT,gm,choice=0,tx,ty,i,x,y;
	float sx,sy,theta;
	initgraph(&gd,&gm,"c:\\tc\\bgi");
	printf("\nEnter the coordinates of the triangle: ");
scanf("%d %d %d %d %d %d",&points[0],&points[1],&points[2],  
 				&points[3],&points[4],&points[5]);
	points[6]=points[0];
	points[7]=points[1];


	for(i=0;i<8;i++)
	{
		pointsCopy[i]=points[i];
	}
	triangle();

	do
	{
		printf("\n________MENU________");
		printf("\n1.Translate");
		printf("\n2.Rotate");
		printf("\n3.Scale");
		printf("\n4.Redraw the triangle");
		printf("\n5.Exit");
		printf("\nEnter your choice: ");
		scanf("%d",&choice);
		switch(choice)
		{
			case 1:{
					printf("\nEnter the tx and ty: ");
					scanf("%d %d",&tx,&ty);
					for(i=0;i<6;i+=2)
					{
						points[i]+=tx;
						points[i+1]+=ty;
					}
					points[6]=points[0];
					points[7]=points[1];
					triangle();
					break;
				   }
			case 2:{
					printf("\nEnter the angle to be 
rotated: ");
					scanf("%f",&theta);
					for(i=0;i<6;i+=2)
					{   x=points[i];
						y=points[i+1];
						points[i]=x*cos(theta*0.01744)-
							y*sin(theta*0.01744);
						points[i+1]=x*sin(theta*0.01744)+
y*cos(theta*0.01744);
					}
					points[6]=points[0];
					points[7]=points[1];
					triangle();
					break;
				   }
			case 3:{
					printf("\nEnter the scaling factors-
 sx and sy: ");
					scanf("%f %f",&sx,&sy);
					for(i=0;i<6;i+=2)
					{
						points[i]*=sx;
						points[i+1]*=sy;
					}
					points[6]=points[0];
					points[7]=points[1];
					triangle();
					break;
				   }
			case 4:{
					reinit();
					cleardevice();
					clrscr();
					triangle();
					break;
				   }
			default: break;
		}
	}while(choice!=5);
	getch();
}
void triangle()
{
	setviewport(0,0,640,480,0);
	drawpoly(4,points);
}
void reinit()
{
	int i;
	for(i=0;i<8;i++)
	{
		points[i]=pointsCopy[i];
	}
}


Output
1
1
1

Midpoint Circle Drawing Algorithm

Write a program to implement midpoint circle drawing algorithm.


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void midpoint(int, int, int);
void plot(int, int, int, int);
int col=0;
void main()
{
	int x,y,r,gd=DETECT,gm;
	initgraph(&gd,&gm,"c:\\tc\\bgi");
	printf("\nEnter the center and radius of the circle: ");
	scanf("%d %d %d",&x,&y,&r);
	while(!kbhit())
	{
		midpoint(x,y,r);
		delay(500);
	}

	getch();
}

void midpoint(int xc, int yc, int r)
{
	int x=0,y=r,p=1-r;
	col=WHITE;
	plot(xc,yc,x,y);
	while(x<y)
	{
		if(p<0)
		{
			x++;
			plot(xc,yc,x,y);
			p=p+2*x+1;
		}
		else
		{
			x++;
			y--;
			plot(xc,yc,x,y);
			p=p+2*x+1-2*y;
		}
	}
}
void plot(int xc, int yc,int x,int y)
{

	putpixel(xc+x,yc+y,col);
	putpixel(xc+x,yc-y,col);
	putpixel(xc-x,yc+y,col);
	putpixel(xc-x,yc-y,col);
	putpixel(xc+y,yc+x,col);
	putpixel(xc+y,yc-x,col);
	putpixel(xc-y,yc+x,col);
	putpixel(xc-y,yc-x,col);
}



Output
1