0% found this document useful (0 votes)
55 views

Program For Displaying Several Shapes

This program displays a traffic signal by using filled circles of different colors (red, yellow, green) in a loop. It initializes the graphics mode and then has a while loop that iterates through the different signal colors by: 1. Drawing a filled red circle 2. Drawing a filled yellow circle 3. Drawing a filled green circle 4. Adding a delay before clearing the screen and repeating the loop. This allows the traffic signal to continuously cycle through the red, yellow and green colors to simulate an actual traffic signal.

Uploaded by

Raj Rudrapaa
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

Program For Displaying Several Shapes

This program displays a traffic signal by using filled circles of different colors (red, yellow, green) in a loop. It initializes the graphics mode and then has a while loop that iterates through the different signal colors by: 1. Drawing a filled red circle 2. Drawing a filled yellow circle 3. Drawing a filled green circle 4. Adding a delay before clearing the screen and repeating the loop. This allows the traffic signal to continuously cycle through the red, yellow and green colors to simulate an actual traffic signal.

Uploaded by

Raj Rudrapaa
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 24

PROGRAM:

/* Program for displaying several shapes */

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int g=DETECT,gm;
initgraph(&g,&gm,"");
setcolor(1);
outtextxy(15,15,"LINE");
outtextxy(350,15,"ELLIPSE");
outtextxy(15,180,"RECTANGLE");
outtextxy(15,325,"SQUARE");
outtextxy(350,200,"ARC");
outtextxy(350,300,"TRIANGLE");
outtextxy(15,50,"CIRCLE");
setcolor(1);
line(30,30,200,30);
setcolor(2);
ellipse(400,100,0,360,100,50);
setcolor(3);
rectangle(200,200,50,300);
setcolor(4);
rectangle(50,450,150,350);
setcolor(5);
line(300,450,450,450);
line(300,450,375,325);
line(450,450,375,325);
setcolor(6);
circle(100,100,50);
setcolor(8);
arc(400,300,20,150,70);
getch();
}
PROGRAM:

/* Program for displaying an object */

#include<graphics.h>
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
int main(void)
{
int g = DETECT, gm;
initgraph(&g,&gm," ");
setcolor(6);
line(100,200,300,200);
line(200,100,100,200);
line(200,100,300,200);
line(100,200,100,400);
line(300,200,300,400);
line(100,400,300,400);
line(300,200,500,200);
line(200,100,400,100);
line(400,100,500,200);
line(500,200,500,400);
line(500,400,300,400);
line(350,250,450,250);
line(350,350,450,350);
line(350,250,350,350);
line(350,250,450,350);
line(450,250,350,350);
line(450,250,450,350);
circle(100,70,20);
line(200,350,220,350);
line(220,350,220,400);
line(200,350,200,400);
line(150,420,250,420);
line(150,400,150,420);
line(250,400,250,420);
line(0,320,100,320);
line(0,390,100,390);
line(20,300,20,400);
line(40,300,40,400);
line(500,320,600,320);
line(500,390,600,390);
line(520,300,520,400);
line(540,300,540,400);
circle(200,160,5);
setbkcolor(GREEN);
closegraph();
return 0;}
PROGRAM:

/* Program for Displaying an ChessBoard */

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
void main()
{
int i,j;
int g = DETECT,gm;
initgraph(&g,&gm," ");
outtextxy(200,20,"CHESSBOARD");
setbkcolor(BLACK);
rectangle(50,50,450,450);
for(i=50;i<450;i+=100)
{
for(j=50;j<450;j+=100)
{
bar(i,j,i+50,j+50);
bar(i+50,j+50,i+100,j+100);
}
}
getch();
closegraph();
}
PROGRAM

/* Program for displaying an BarChart */

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
void main()
{
int y=0,a[100],b[100],d[100],i,n;
int j=380,k=150,r=390;
char c[100][100],v[30],t;
int g=DETECT,gm;
initgraph(&g,&gm," ");
printf(" Enter No. of batsman :");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter batsman %d name :",i);
scanf("%s",c[i]);
printf("Enter batsman %d Run :",i);
scanf("%d",&b[i]);
}
setcolor(1);
line(350,152,540,152);
outtextxy(540,152,"x");
setcolor(1);
line(350,152,350,10);
outtextxy(350,10,"y");
for(i=1;i<=n;i++)
{
setfillstyle(1,i+2);
setcolor(i+1);
bar(j,k-b[i],r,k);
j=j+40;
r=r+40;
}
for(i=1;i<=n;i++)
{
setfillstyle(1,i+2);
setcolor(i+2);
sprintf(v,"%s-%d",c[i],b[i]);
bar(50+y,400,65+y,420);
outtextxy(75+y,400,v);
y=y+100;
}
getch();}
PROGRAM:

/* Program for displaying an PieChart */

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int a[100],b[100],j,i,k,n,s=0,c=0;
char v[20];
int w=50,y=50;
int g=DETECT,gm;
initgraph(&g,&gm," ");
printf(" Enter the value of n:");
scanf("%d",&n);
printf("Enter the value of product percentage:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
s=s+a[i];
}
for(i=0;i<n;i++)
{
b[i]=(a[i]*360)/s;
}
printf("The Result is:");
for(i=1;i<=n;i++)
{
printf("\t%d",b[i]);
}
for(i=0;i<n;i++)
{
setfillstyle(2,i+2);
pieslice(150,200,c,c+b[i],50);
c=c+b[i];
}
for(i=1;i<=n;i++)
{
setfillstyle(2,i+2);
sprintf(v,"product = %d",i);
bar(400,y+50,375,y+75);
outtextxy(425,y+50,v);
w=w+50;
y=y+50;
}
printf("\n\n\n");
getch();}
PROGRAM:

/* Program for displaying an line using DDA Algorithm */

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
void dda(int,int,int,int);
int x1,x2,y1,y2,dx,dy,x,y,a,b,xin,yin,step,k,i,m,m1,m2,qx,qy;
char v[20],v1[20];
void main()
{
int g=DETECT,gm;
clrscr();
initgraph(&g,&gm,"");
printf("\n Enter the x1-value :");
scanf("%d",&x1);
printf("\n Enter the y1-value :");
scanf("%d",&y1);
printf("\n Enter the x2-value :");
scanf("%d",&x2);
printf("\n Enter the y2-value :");
scanf("%d",&y2);
sprintf(v,"%d,%d",x1,y1);
sprintf(v1,"%d,%d",x2,y2);
setcolor(5);
outtextxy(x1,y1+10,v);
outtextxy(x2,y2+10,v1);
outtextxy(x1-25,y1-25,"DDA Line Drawing ");
dda(x1,y1,x2,y2);
getch();
closegraph();
}
void dda(int x1,int y1,int x2,int y2)
{
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>abs(dy))
{
step=abs(dx);
}
else
{
step=abs(dy);
}
xin=dx/step;
yin=dy/step;
x=x1;
y=y1;
putpixel(x,y,2);
for(i=1;i<=step;i++)
{
x=x+xin;
y=y+yin;
putpixel(x,y,4);
}
getch();
}
PROGRAM:

/* Program for displaying an Line using Bresenhams Line Drawing


Algorithm */

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
void bres(int,int,int,int);
int tdy,tdydx,dx,dy,xa,xb,ya,yb,p,x,y,xend,yend,i;
char v[20],v1[20];
void main()
{
int g=DETECT,gm;
clrscr();
initgraph(&g,&gm,"");
printf("\n Enter the value of Xa:");
scanf("%d",&xa);
printf("\n Enter the value of Ya:");
scanf("%d",&ya);
printf("\n Enter the value of Xb:");
scanf("%d",&xb);
printf("\n Enter the value of Yb:");
scanf("%d",&yb);
bres(xa,ya,xb,yb);
setcolor(4);
sprintf(v,"%d,%d",xa,ya);
outtextxy(xa-25,ya-25,"Bresenhams Line Drawing ");
outtextxy(xa,ya+5,v);
setcolor(4);
sprintf(v1,"%d,%d",xb,yb);
outtextxy(xb,yb+5,v1);
getch();
closegraph();
}
void bres(int xa,int ya,int xb,int yb)
{
dx=abs(xb-xa);
dy=abs(yb-ya);
p=(2*dy)-dx;
tdy=2*dy;
tdydx=2*(dy-dx);
if(xa>=xb)
{
x=xb;
y=yb;
xend=xa;

putpixel(x,y,1);
}
else
{
x=xa;
y=ya;
xend=xb;
putpixel(x,y,1);
}
while(x<xend)
{
x++;
if(p<0)
{
p+=tdy;
}
else
{
y++;
p+=tdydx;
}
putpixel(x,y,4);
}
getch();
}
PROGRAM:

/* Program for displaying Circle using Midpoint Circle Generating


Algorithm */

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
void circlemidpt(int,int,int);
void cirpt(int,int,int,int);
int xcen,ycen,rad,p,x,y;
char v[20];
void main()
{
int g=DETECT,gm;
clrscr();
initgraph(&g,&gm,"");
printf("\n Enter the Xcen-value :");
scanf("%d",&xcen);
printf("\n Enter the Ycen-value :");
scanf("%d",&ycen);
printf("\n Enter the Radius :");
scanf("%d",&rad);
circlemidpt(xcen,ycen,rad);
setcolor(4);
sprintf(v,"Circle of Radius : %d",rad);
outtextxy(xcen-70,ycen-70,v);
getch();
closegraph();
}
void circlemidpt(int xcen,int ycen,int rad)
{
x=0;
y=rad;
p=1-rad;
cirpt(xcen,ycen,x,y);
while(x<y)
{
x++;
if(p<0)
{
p+=2*x+1;
}
else
{
y--;
p+=2*(x-y)+1;
}
cirpt(xcen,ycen,x,y);
}
}
void cirpt(int xcen,int ycen,int x,int y)
{
putpixel(xcen+x,ycen+y,1);
putpixel(xcen-x,ycen+y,1);
putpixel(xcen+x,ycen-y,1);
putpixel(xcen-x,ycen-y,1);
putpixel(xcen+y,ycen+x,1);
putpixel(xcen-y,ycen+x,1);
putpixel(xcen+y,ycen-x,1);
putpixel(xcen-y,ycen-x,1);
}
PROGRAM:

/* Program for displaying Smiley using DDA */

#include<stdio.h>
#include<graphics.h>
#include<conio.h>
void main()
{
int g=DETECT,gm,c,max;
initgraph(&g,&gm,"");
max=getmaxcolor();
for(c=1;c<max;c++)
{
cleardevice();
setbkcolor(c);
printf("\t\t Smile...!");
circle(110,220,100);
circle(320,220,100);
circle(530,220,100);
arc(110,210,220,-35,90);
arc(320,210,220,-35,90);
arc(530,210,220,-35,90);
fillellipse(60,200,10,10);
fillellipse(160,200,10,10);
fillellipse(270,200,10,10);
fillellipse(360,200,10,10);
fillellipse(480,200,10,10);
fillellipse(580,200,10,10);
getch();
}
}
PROGRAM:

/* Program for displaying Ellipse using Midpoint Algorithm */

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void disp();
float x,y;
int xc,yc;
void main()
{
int gd=DETECT,gm;
int a,b,s;
float p1,p2;
clrscr();
initgraph(&gd,&gm,"");
printf("\n enter the center points of xc and yc:");
scanf("%d%d",&xc,&yc);
printf("\n enter the rx,ry:");
scanf("%d%d",&a,&b);
x=0;
y=b;
disp();
p1=(b*b)-(a*a*b)+(a*a)/4;
while((2.0*b*b*x)<=(2.0*a*a*y))
{
x++;
if(p1<=0)
p1=p1+(2.0*b*b*x)+(b*b);
else
{
y--;
p1=p1+(2.0*b*b*x)+(b*b)-(2.0*a*a*y);
}
disp();
x=-x;
disp();
x=-x;
}
x=a;
y=0;
disp();
p2=(a*a)+2.0*(b*b*a)+(b*b)/4;
while((2.0*b*b*x)>(2.0*a*a*y))
{
y++;
if(p2>0)
p2=p2+(a*a)-(2.0*a*a*y);
else
{
x--;
p2=p2+(2.0*b*b*x)-(2.0*a*a*y)+(a*a);
}
disp();
y=-y;
disp();
y=-y;
}
outtextxy(xc,yc,"ELLIPSE");
getch();
closegraph();
}
void disp()
{
putpixel(xc+x,yc+y,10);
putpixel(xc-x,yc+y,10);
putpixel(xc+x,yc-y,10);
putpixel(xc-x,yc-y,10);
}
PROGRAM:

/* Program for displaying Water pipe */

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<graphics.h>
void main()
{
int gdriver=DETECT,gmode,errorcode;
int i;
initgraph(&gdriver,&gmode,"");
for(i=1;i<=10;i++)
{
setcolor(4);
bar(0,0,650,600);
line(400,160,400,400);
line(420,140,420,400);
line(420,140,320,140);
line(400,160,280,160);
line(320,140,320,120);
line(320,120,330,120);
line(330,120,330,110);
line(330,110,300,110);
line(300,110,300,120);
line(300,120,310,120);
line(310,120,310,140);
line(310,140,270,140);
line(270,140,270,180);
line(280,160,280,180);
circle(275,335,30);
line(245,330,255,400);
line(305,330,295,400);
circle(275,400,19);
ellipse(275,180,0,360,5,3);
if(i%2!=0)
{
ellipse(275,185,0,360,3,5);
ellipse(275,220,0,360,3,5);
ellipse(275,255,0,360,3,5);
ellipse(275,290,0,360,3,5);
ellipse(275,335,0,360,3,5);
}
else if(i%2==0)
{
ellipse(275,180,0,360,3,5);
ellipse(275,187,0,360,3,5);
ellipse(275,237,0,360,3,5);
ellipse(275,275,0,360,3,5);
ellipse(275,305,0,360,3,5);
ellipse(275,330,0,360,3,5);
}
delay(500);
clrscr();
}
getch();
}
PROGRAM:

/* Program for displaying Bouncing Ball */

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gdriver=DETECT,gmode;
int i,j,K=0;
initgraph(&gdriver,&gmode,"\\BG1");
while(K<5)
{
for(i=1;i<=400;i++)
{
fillellipse(100,i,40,40);
setfillstyle(2,RED);
delay(1);
cleardevice();
}
if(i=400)
{
for(j=400;j>=1;j--)
{
fillellipse(100,j,40,40);
setfillstyle(K,GREEN);
delay(1);
cleardevice();
}
}
K++;
}
getch();
}
PROGRAM:

/* Program for displaying Traffic Signal */


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void main()
{
int gd=DETECT,gm,i;
initgraph(&gd,&gm,"");
outtextxy(25,25,"TRAFFIC sIGNAL");
rectangle(45,50,150,300);
rectangle(100,450,90,300);
for(i=0;i<=2;i++)
{
setfillstyle(1,RED);
fillellipse(100,100,30,30);
ellipse(100,100,0,360,30,30);
setfillstyle(1,BLACK);
fillellipse(100,170,30,30);
ellipse(100,170,0,360,30,30);
setfillstyle(1,BLACK);
fillellipse(100,240,30,30);
ellipse(100,240,0,360,30,30);
delay(2000);
setfillstyle(1,BLACK);
fillellipse(100,100,30,30);
ellipse(100,100,0,360,30,30);
setfillstyle(1,YELLOW);
fillellipse(100,170,30,30);
ellipse(100,170,0,360,30,30);
setfillstyle(1,BLACK);
fillellipse(100,240,30,30);
ellipse(100,240,0,360,30,30);
delay(2000);
setfillstyle(1,BLACK);
fillellipse(100,100,30,30);
ellipse(100,100,0,360,30,30);
setfillstyle(1,BLACK);
fillellipse(100,170,30,30);
ellipse(100,170,0,360,30,30);
setfillstyle(1,GREEN);
fillellipse(100,240,30,30);
ellipse(100,240,0,360,30,30);
delay(2500);
}
getch();}
Program:

/* Program for performing 3D-Transformations */

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int p[10][10],c;
void get()
{
int i,j,left,top,right,bottom,width;
printf("\n Enter the four points");
for(i=0;i<4;i++)
{
printf("\n Enter x,y,z value",i+1);
for(j=0;j<3;j++)
{
scanf("%d",&p[i][j]);
}
}
}
void draw()
{
int left,top,right,bottom,width;
left=p[0][0];
top=p[0][0];
right=p[2][0];
bottom=p[2][1];
width=(right-left)/4;
setfillstyle(1,2);
setcolor(2);
bar3d(left,top,right,bottom,width,1);
getch();
}
void scl()
{
int i,sx,sy,sz;
printf("\n Enter the scaling factor:");
scanf("%d%d%d",&sx,&sy,&sz);
for(i=0;i<4;i++)
{
p[i][0]=p[i][0]*sx;
p[i][1]=p[i][1]*sy;
p[i][2]=p[i][2]*sz;
}
}
void tsl()
{
int i,tx,ty,tz;
printf("\n Enter the translating factor:");
scanf("%d%d%d",&tx,&ty,&tz);
for(i=0;i<4;i++)
{
p[i][0]=p[i][0]+tx;
p[i][1]=p[i][1]+ty;
p[i][2]=p[i][2]+tz;
}
}
void rot()
{
int i,ch,x,y,z,a;
printf("\n Enter the angle");
scanf("%d",&a);
printf("\n 1.x-axis\n2.y-axis\n3.z-axis");
scanf("%d",&ch);
switch(ch)
{
case 1:
for(i=0;i<4;i++)
{
x=p[i][0];
y=p[i][1];
z=p[i][2];
p[i][1]=y*cos(a)-z*sin(a);
p[i][2]=y*sin(a)+z*cos(a);
p[i][0]=x;
}
break;
case 2:
for(i=0;i<4;i++)
{
x=p[i][0];
y=p[i][1];
z=p[i][2];
p[i][2]=z*cos(a)-x*sin(a);
p[i][2]=z*sin(a)+x*cos(a);
p[i][1]=y;
}
break;
case 3:
for(i=0;i<4;i++)
{
x=p[i][0];
y=p[i][1];
z=p[i][2];
p[i][0]=x*cos(a)-y*sin(a);
p[i][1]=x*sin(a)+y*cos(a);
p[i][1]=z;
}
break;
case 4:
break;
}
}
void trans()
{
char g;
do
{
printf("\n 1.translating\n2.scaling\n3.rotation\n4.exit");
printf("\n Enter your choice :");
scanf("%d",&c);
switch(c)
{
case 1:
tsl();
draw();
break;
case 2:
scl();
draw();
break;
case 3:
rot();
draw();
break;
}
printf("\n do you want to continue (y/n) :");
scanf("%s",&g);
}
while((g=='y')||(g=='y'));
}
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
get();
draw();
trans();
getch();
}
Program:

/* Program for performing 2D-Transformations */

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gm=DETECT,gd;
int
tx,ty,sx,sy,c,x=100,y=100,r,x1,y1,x2,y2,x3,y3,theta,x11,x22,x33,y11,y22,
y33;
char g;
initgraph(&gm,&gd,"");
do
{
printf("\n\t Menus \n\t 1.translation \n\t 2.scaling \n\t 3.rotation \n\t
4.exit");
printf("\n Enter the choice:");
scanf("%d",&c);
switch(c)
{
case 1:
printf("\n Translation:");
printf("\n Enter the original coordinate position(x,y):");
scanf("%d%d",&x,&y);
printf("\n Enter the translation vectors(tx,ty):");
scanf("%d%d",&tx,&ty);
clrscr();
setcolor(1);
outtextxy(x,y-20,"Before translation:");
rectangle(x,y,x1,y1);
outtextxy(x+tx,(y+ty)-20,"After translation:");
setcolor(1);
rectangle(x+tx,y+ty,x1+tx,y1+ty);
getch();
break;
case 2:
printf("scaling:");
printf("\n Enter the original coordinates position(x,y):");
scanf("%d%d",&x1,&y1);
printf("\n Enter the scaling vector(sx,sy):");
scanf("%d%d",&sx,&sy);
clrscr();
setcolor(1);
if(sx==sy)
{
printf("\n\t uniform scaling");
}
else
{
printf("\n\t differential scaling");
}
setcolor(1);
outtextxy(x1-20,y1-20,"before scaling");
rectangle(x,y,x1,y1);
outtextxy(x1+30,y1+30,"After scaling");
rectangle(x*sx,y*sy,x1*sx,y1*sy);
getch();
break;
case 3:
printf("\n\t Rotation:");
printf("\n Enter the value of x1,y1:");
scanf("%d%d",&x1,&y1);
printf("\n Enter the value of x2,y2:");
scanf("%d%d",&x2,&y2);
printf("\n Enter the value of x3,y3:");
scanf("%d%d",&x3,&y3);
theta=90;
clrscr();
setcolor(1);
outtextxy(x3,y1,"before rotation");
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
getch();
x11=abs(x1*cos(theta)-y1*sin(theta));
x22=abs(x2*cos(theta)-y2*sin(theta));
x33=abs(x3*cos(theta)-y3*sin(theta));
y11=abs(x1*sin(theta)+y1*cos(theta));
y22=abs(x2*sin(theta)+y2*cos(theta));
y33=abs(x3*sin(theta)+y3*cos(theta));
outtextxy(x33,y11,"After rotation");
line(x11,y11,x22,y22);
line(x22,y22,x33,y33);
line(x33,y33,x11,y11);
getch();
break;
case 4:
exit(0);
default:
printf("\n Enter the valid choice between 1 to 3:");
break;
}
cleardevice();
printf("\n do you want to continue (y/n)\n");
scanf("%s",&g);
}
while((g=='y')||(g=='y'));
}

You might also like