Write A C-Program To Draw Basic Shapes Such As Line, Rectangle, Square, Rectangle, Elipse

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 48

Computer Graphics

1. Write a C-program to draw basic shapes such as line,rectangle,square,rectangle,elipse.


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
line(300,200,400,100);
rectangle(50,50,100,100);
circle(150,350,50);
ellipse(300,350,0,360,50,30);
rectangle(400,300,550,400);
getch();
}

Department of Computer science (MCA V) Page 1


Computer Graphics

OUTPUT:

Department of Computer science (MCA V) Page 2


Computer Graphics

2. Write a C program to draw a rectangular border around the screen in VGA high mode
using different colors.
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,mx,my,gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
mx=getmaxx();
my=getmaxy();
for(x=0;x<=mx;x++)
{
putpixel(x,0,7);
delay(5);
}
for(y=0;y<=my-1;y++)
{
putpixel(mx-1,y,13);
delay(5);
}
for(x=mx;x>=0;x--)
{
putpixel(x,my,15);
delay(5);
}

Department of Computer science (MCA V) Page 3


Computer Graphics

for(y=my;y>=0;y--)
{
putpixel(0,y,10);
delay(5);
}
getch();
}

OUTPUT:

Department of Computer science (MCA V) Page 4


Computer Graphics

3. Write a C program to draw a line graph of monthly sales data for the following data
values.
Month Sales(Rs)
Jan 2000
Feb 4000
Mar 7000
Apr 6000
May 4000
June 8000
July 8000
Aug 7000
Sep 5000
Oct 6000
Nov 8000
Dec 9000

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int y[15]={0,2000,4000,7000,6000,4000,8000,8000,7000,5000,6000,8000,9000};
char month[15][15]={" ","j","f","m","a","m","j","j","a","s","o","n","d"};
charp[15][15]={"","1000","2000","3000","4000","5000","6000","7000","8000",
"9000","10000","0"};
int i,xa,ya,xb,yb,gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
line(50,400,630,400);

Department of Computer science (MCA V) Page 5


Computer Graphics

line(50,5,50,400);
for(i=1;i<10;i++)
{
outtextxy(10,400-40*i,p[i]);
}
xa=100;
ya=400-y[1]/25;
outtextxy(xa,405,month[1]);
for(i=2;i<=12;i++)
{
xb=30+50*i;
yb=400-y[i]/25;

line(xa,ya,xb,yb);
outtextxy(xb,405,month[i]);
xa=xb;
ya=yb;
}
getch();
}

Department of Computer science (MCA V) Page 6


Computer Graphics

OUTPUT:

Department of Computer science (MCA V) Page 7


Computer Graphics

4.Write a C program for a sinusoidal function.

#include<conio.h>
#include<stdio.h>
#include<math.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm,a,x,sy;
float PI=3.142,ay;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
line(10,200,450,200);
line(10,5,10,400);
for(a=0;a<=360;a++)
{
ay=sin(a*PI/180);
sy=200-ay*100;
x=10+a;
putpixel(x,sy,7);
}
outtextxy(350,20,"sinewave");
outtextxy(10,200,"0");
outtextxy(450,200,"360");
getch();
}

Department of Computer science (MCA V) Page 8


Computer Graphics

OUTPUT:

Department of Computer science (MCA V) Page 9


Computer Graphics

5. Write a C program to draw a pie chart to show the sales figure of different car
companies given the following data.
Company Sales
Harley Davidson 9
Kawasaki 24
Honda 58
Royal Enfield 12
Others 18

#include<graphics.h>
#include<conio.h>
#include<math.h>
#define pi 3.142
void main()
{
char comp[20][20] ={"harley devidson","kawasaki","honda","royal enfield","others"};
int sales[] ={9,24,58,12,18};
int i,px,py,x,y,xc,yc,r,tot=0;
int gd = DETECT,gm;
float ang = 0;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
xc = 320;
yc = 240;
r = 175;
circle(xc,yc,r);
px = xc + r;
py = yc;
line(xc,yc,px,py);

Department of Computer science (MCA V) Page 10


Computer Graphics

for(i=0;i<=5;i++)
tot+=sales[i];
for(i=0;i<5;i++)
{
ang += (float)sales[i]/tot*360;
x = xc+r*cos(ang*pi/180);

y = yc-r*sin(ang*pi/180);
line(xc,yc,x,y);
outtextxy(px,py,comp[i]);
px=x;
py=y;
}
outtextxy(px,py,comp[5]);
getch();
}

Department of Computer science (MCA V) Page 11


Computer Graphics

OUTPUT:

Department of Computer science (MCA V) Page 12


Computer Graphics

6.Write a C program for tickertape display.


#include<graphics.h>

#include<conio.h>
void main()
{
char msg[]={"MCA V SEM"};
int x,y,p,gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
settextstyle(1,0,6);
p=textwidth(msg);
while(!kbhit())
{
x=getmaxx();
y=getmaxy()/2;
while(x>-p&&!kbhit())
{
outtextxy(x,y,msg);
delay(5);
cleardevice();
x--;
}
}
getch();
}

Department of Computer science (MCA V) Page 13


Computer Graphics

OUTPUT:

Department of Computer science (MCA V) Page 14


Computer Graphics

7. Write a C program to generate a capcha.


#include<stdlib.h>
#include<dos.h>
#include<graphics.h>
main()
{
int i = 0, key, num, midx, gd = DETECT, gm;
char a[10];
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
midx = getmaxx()/2;
settextstyle(SCRIPT_FONT,HORIZ_DIR,5);
settextjustify(CENTER_TEXT,CENTER_TEXT);
setcolor(GREEN);
outtextxy(midx,20,"CAPTCHA");
settextstyle(SCRIPT_FONT,HORIZ_DIR,2);
outtextxy(midx,125,"Press any key to change the generated random code\"captcha\"");
outtextxy(midx,150,"Press escape key to exit...");
setcolor(WHITE);
setviewport(100,200,600,400,1);
setcolor(RED);
randomize();
while(1)
{
while(i<6)
{
num = random(3);
if ( num == 0 )

Department of Computer science (MCA V) Page 15


Computer Graphics

a[i] = 65 + random(26);
else if ( num == 1)
a[i] = 97 + random(26);
else
a[i] = 48 + random(10);
i++;
}
a[i] = '\0';
outtextxy(210,100,a);
key = getch();
if( key == 27 ) /* escape key*/
exit(0);
clearviewport();
i = 0;
}
}

Department of Computer science (MCA V) Page 16


Computer Graphics

OUTPUT:

Department of Computer science (MCA V) Page 17


Computer Graphics

8. Write a C program to draw line segment using DDA line drawing algorithm for the
given two points(x1,y1)(x2,y2).
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>
void main()
{
float x1,y1,x2,y2,dx,dy,xc,yc,x,y,s;
int gd = DETECT,gm,k;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
printf("Enter co-ordinates of starting points:");
scanf("%f%f",&x1,&y1);
printf("Enter co-ordinates of ending points:");
scanf("%f%f",&x2,&y2);
dx =abs(x2 - x1);
dy = abs(y2 - y1);
if(dy>dx)
s=dx;
else
s=dy;
xc = dx/s;
yc = dy/s;
x = x1;
y = y1;
putpixel(x,y,10);
for(k=1;k<=s;k++)

Department of Computer science (MCA V) Page 18


Computer Graphics

{
x+=xc;
y+=yc;
putpixel(x,y,10);
}
getch();
closegraph();
}

OUTPUT:

Department of Computer science (MCA V) Page 19


Computer Graphics

9. Write a program to draw a line segment using Bresenham’s line drawing algorithm for
the given two points(x1,y1)(x2,y2).
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
void main()
{
int x,y,x1,x2,y1,y2,dx,dy,xend,p,gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
printf("enter start point:");
scanf("%d%d",&x1,&y1);
printf("enter end point:");
scanf("%d%d",&x2,&y2);
dx = abs(x1-x2);
dy = abs(y1-y2);
p = 2 * dy - dx;
if(x1<x2)
{
x = x1;
y = y1;
xend = x2;
}
else
{
x = x2;
y = y2;

Department of Computer science (MCA V) Page 20


Computer Graphics

xend = x1;
}
putpixel(x,y,5);
while(x<xend)
{
++x;
if(p<0)
{
p+= 2*dy;
}
else
{
++y;
p+=2*(dy-dx);
}
putpixel(x,y,15);
}
getch();
closegraph();
}

Department of Computer science (MCA V) Page 21


Computer Graphics

OUTPUT:

Department of Computer science (MCA V) Page 22


Computer Graphics

10. Write a C program to plot a circle using Midpoint algorithm.


#include<graphics.h>

#include<conio.h>
#include<graphics.h>
void midpt(int,int,int);
void plotpts(int,int,int,int);
void main()
{
int xc,yc,r,gd = DETECT,gm;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
printf("Enter circle points:");
scanf("%d%d",&xc,&yc);
printf("Enter radius");
scanf("%d",&r);
midpt(xc,yc,r);
getch();
}
void midpt(int xc,int yc,int r)
{
int x,y,p;
x = 0;
y = r;
p = 1-r;
plotpts(xc,yc,x,y);
while(x<y)
{
++x;

Department of Computer science (MCA V) Page 23


Computer Graphics

if(p<0)
p+=2*x+1;
else
{
--y;
p+=2*(x-y)+1;
}
plotpts(xc,yc,x,y);
}
getch();
closegraph();
}
void plotpts(int xc,int yc,int x,int y)
{
putpixel(xc+x,yc+y,1);
putpixel(xc-x,yc+y,2);
putpixel(xc+x,yc-y,3);
putpixel(xc-x,yc-y,4);
putpixel(xc+y,yc+x,5);
putpixel(xc-y,yc+x,6);
putpixel(xc+y,yc-x,7);
putpixel(xc-y,yc-x,8);
}

Department of Computer science (MCA V) Page 24


Computer Graphics

OUTPUT:

Department of Computer science (MCA V) Page 25


Computer Graphics

11.Write a C program to demonstrate general fixed point scaling.


#include<graphics.h>
#include<conio.h>
#include<math.h>
void main()
{
int gd=DETECT,gm,xd[5],yd[5],i,xf,yf;
float sx,sy;
int x[]={50,150,150,100,50};
int y[]={350,350,250,200,250};
void draw(int [],int []);
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
draw(x,y);
printf("enter scaling factors");
scanf("%f%f",&sx,&sy);
xf=x[0];
yf=y[0];
for(i=0;i<5;i++)
{
xd[i]=x[i]*sx+xf*(1-sx);
yd[i]=y[i]*sy+yf*(1-sy);
}
draw(xd,yd);
getch();
}

void draw(int x[],int y[])

Department of Computer science (MCA V) Page 26


Computer Graphics

int i;
for(i=0;i<4;i++)
line(x[i],y[i],x[i+1],y[i+1]);
line(x[4],y[4],x[0],y[0]);
}

OUTPUT:

Department of Computer science (MCA V) Page 27


Computer Graphics

12. Write a C program to demonstrate general pivot point rotation.


#include<graphics.h>
#include<conio.h>
#include<math.h>
#include<stdio.h>
void draw(int x[],int y[]);
void main()
{
int gd=DETECT,gm,i,xp,yp,xd[5],yd[5],t,k;
float rt,c,s;
int x[5]={320,380,380,350,320};
int y[5]={240,240,180,120,180};
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
printf("INPUT ROTATION ANGLE:");
scanf("%d",&k);
draw(x,y);
xp=x[0];
yp=y[0];
for(t=k;t<360;t+=k)
{
rt=t*3.142/180;
c=cos(rt);
s=sin(rt);
for(i=0;i<5;i++)
{
xd[i]=x[i]*c-y[i]*s-xp*c+yp*s+xp;
yd[i]=x[i]*s+y[i]*c-xp*s-yp*c+yp;

Department of Computer science (MCA V) Page 28


Computer Graphics

}
draw(xd,yd);
delay(100);
}
getch();
}
void draw(int x[],int y[])
{
int i;
for(i=0;i<5;i++)
{
line(x[i],y[i],x[(i+1)%5],y[(i+1)%5]);
}
}
OUTPUT :

Department of Computer science (MCA V) Page 29


Computer Graphics

13. Write a C program to illusrate planetary motion.


#include<math.h>

#include<graphics.h>
#define PI 3.142
void planet()
{
int x,y;
float ang,i=90;
while(!kbhit())
{
ang = ++i*PI/180;
x=320-300*cos(ang);
y=240-150*sin(ang);
setcolor(i);
circle(x,y,20);
delay(200);
setcolor(BLACK);
circle(x,y,20);
}
}
void main()
{
int gd = DETECT,gm;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
setcolor(MAGENTA);
circle(320,240,100);
planet();

Department of Computer science (MCA V) Page 30


Computer Graphics

OUTPUT:

Department of Computer science (MCA V) Page 31


Computer Graphics

14. Write a C program to draw flower by translation of petals.


#include<math.h>
#include<graphics.h>
#define PI 3.142
void main()
{
int gd=DETECT,gm,i,x,y;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
setcolor(YELLOW);
circle(320,240,30);
setfillstyle(INTERLEAVE_FILL,YELLOW);
floodfill(320,240,YELLOW);
delay(500);
for(i=0;i<6;i++)
{
x=320+80*sin(i*60*PI/180);
y=240+80*cos(i*60*PI/180);
setcolor(i+1);
circle(x,y,40);
setfillstyle(SOLID_FILL,i+1);
floodfill(x,y,i+1);
delay(200);
}
getch();
}

Department of Computer science (MCA V) Page 32


Computer Graphics

OUTPUT:

Department of Computer science (MCA V) Page 33


Computer Graphics

15.Write a C program for polygon clipping.

#include<graphics.h>
#include<conio.h>
#include<math.h>
typedef enum{false,true}boolean;
typedef enum{L,R,B,T} boundary;
typedef struct
{
int x,y;
}point;
point winmin={225,125},winmax={375,275};
int clip(int ,point[],point[],boundary);
boolean inside(point ,boundary);
point intersect(point,point,boundary);
void earse(point[],int);
void main()
{
int i,gd=DETECT,gm,ni,nl,nr,nb,no;
point ptsin[10],poutl[10],poutb[10],poutr[10],ptsout[10];
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
ptsin[0].x=200;
ptsin[0].y=200;
ptsin[1].x=300;
ptsin[1].y=100;
ptsin[2].x=400 ;
ptsin[2].y=200;

Department of Computer science (MCA V) Page 34


Computer Graphics

ptsin[3].x=300;
ptsin[3].y=300;
rectangle(winmin.x,winmin.y,winmax.x,winmax.y);
getch();
ni=4;
for(i=0;i<ni;i++)

line(ptsin[i].x,ptsin[i].y,ptsin[(i+1)%ni].x,ptsin[(i+1)%ni].y);
getch();
nl=clip(ni,ptsin,poutl,L);
nr=clip(nl,poutl,poutr,R);
nb=clip(nr,poutr,poutb,B);
no=clip(nb,poutb,ptsout,T);
earse(ptsin,ni);
getch();
for(i=0;i<no;i++)
line(ptsout[i].x,ptsout[i].y,ptsout[(i+1)%no].x,ptsout[(i+1)%no].y);
getch();
}
int clip(int n,point pin[],point pout[],boundary b)
{
int i,cont=0;
boolean inp1,inp2;
point p1,p2,pint;
for(i=0;i<n;i++)
{
p1=pin[i];

Department of Computer science (MCA V) Page 35


Computer Graphics

p2=pin[(i+1)%n];
inp1=inside(p1,b);
inp2=inside(p2,b);
if(!inp1 && inp2)
{
pint=intersect(p1,p2,b);
pout[cont++]=pint;
pout[cont++]=p2;
}
if(inp1&&inp2)
pout[cont++]=p2;
if(inp1 && !inp2)
{
pint=intersect(p1,p2,b);
pout[cont++]=pint;
}
}
return cont;
}
boolean inside(point p,boundary b)
{
boolean ans=true;
switch(b)
{
case L :if(p.x<winmin.x)
ans=false;
break;

Department of Computer science (MCA V) Page 36


Computer Graphics

case R :if(p.x>winmax.x)
ans=false;
break;
case B :if(p.y>winmax.y)
ans=false;
break;
case T :if(p.y<winmin.y)
ans=false;
}
return ans;
}
point intersect(point p1,point p2,boundary b)
{
point pint;
float m;
if(p1.x!=p2.x)
m=(float)(p2.y-p1.y)/(p2.x-p1.x);
switch(b)
{
case L :pint.x=winmin.x;
pint.y=p2.y+m*(winmin.x-p2.x);
break;
case R :pint.x=winmax.x;
pint.y=p2.y+m*(winmax.x-p2.x);
break;
case B :pint.y=winmax.y;
if(p1.x==p2.x)

Department of Computer science (MCA V) Page 37


Computer Graphics

pint.x=p1.x;
else
pint.x=p2.x+(winmax.y-p2.y)/m;
break;
case T :pint.y=winmin.y;
if(p1.x==p2.x)
pint.x=p1.x;
else
pint.x=p2.x+(winmin.y-p2.y)/m;
}
return pint;
}
void earse(point p[],int n)
{
int i,f,b;
f=getcolor();
b=getbkcolor();
setcolor(b);
for(i=0;i<n;i++)
line(p[i].x,p[i].y,p[(i+1)%n].x,p[(i+1)%n].y);
setcolor(f);
}

Department of Computer science (MCA V) Page 38


Computer Graphics

OUTPUT:

Department of Computer science (MCA V) Page 39


Computer Graphics

Department of Computer science (MCA V) Page 40


Computer Graphics

16. Write a C program for window view port transformation.


#include<stdio.h>

#include<conio.h>
#include<graphics.h>
#include<alloc.h>
void draw(int x[],int y[])
{
moveto(x[0],y[0]);
lineto(x[1],y[1]);
lineto(x[2],y[2]);
lineto(x[0],y[0]);
}
void main()
{
int gd=DETECT,gm,xwmin,ywmin,xwmax,ywmax;
int xvmin,yvmin,xvmax,yvmax,i,sx,sy,area;
int xw[ ]={50,100,75},yw[ ]={50,50,100};
int xv[3],yv[3];
char *buff;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
xwmin=20,ywmin=20;
xwmax=120,ywmax=120;
rectangle(xwmin,ywmin,xwmax,ywmax);
draw(xw,yw);
getch();
area=imagesize(xwmin,ywmin,xwmax,ywmax);
buff=(char *) malloc(area);

Department of Computer science (MCA V) Page 41


Computer Graphics

if(buff==NULL)
{
printf("Not enough memory to allocate\n");
exit(0);
}
getimage(xwmin,ywmin,xwmax,ywmax,buff);
cleardevice();
xvmin=0;
yvmin=0;
xvmax=getmaxx();
yvmax=getmaxy();
rectangle(xvmin,yvmin,xvmax,yvmax);
sx=(xvmax-xvmin)/(xwmax-xwmin);
sy=(yvmax-yvmin)/(ywmax-ywmin);
for(i=0;i<3;i++)
{
xv[i]=xvmin+(xw[i]-xwmin)*sx;
yv[i]=yvmin+(yw[i]-ywmin)*sy;
}
draw(xv,yv);
getch();
cleardevice();
putimage(xwmin,ywmin,buff,COPY_PUT);
getch();

Department of Computer science (MCA V) Page 42


Computer Graphics

OUTPUT:

Department of Computer science (MCA V) Page 43


Computer Graphics

17. Write a C program to draw 3 dimensional cube using parallel projection technique.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
typedef struct
{
int x,y,z;
}point;
void main()
{
int gd=DETECT,gm,i,n=8,x[10],y[10];
float dx,dy,dz;
point p[]={{200,100,-50},{250,100,-50},
{250,100,-100},{200,100,-100},
{200,150,-50},{250,150,-50},{250,150,-100},{200,150,-100}};
initgraph(&gd,&gm,"c://turboc3//bgi");
printf("Enter the direction vector\n");
scanf("%f%f%f",&dx,&dy,&dz);
for(i=0;i<n;i++)
{
x[i]=p[i].x-p[i].z*dx/dz;
y[i]=p[i].y-p[i].z*dy/dz;
}
for(i=0;i<4;i++)
line(x[i],y[i],x[(i+1)%4],y[(i+1)%4]);
for(i=4;i<=7;i++)
line(x[i],y[i],x[((i+1)%4)+4],y[((i+1)%4)+4]);

Department of Computer science (MCA V) Page 44


Computer Graphics

for(i=0;i<4;i++)
line(x[i],y[i],x[i+4],y[i+4]);

getch();
}

OUTPUT:

Department of Computer science (MCA V) Page 45


Computer Graphics

18. Write a C program to draw a 3 dimensional bar chart.


#include<stdio.h>
#include<graphics.h>
#include<conio.h>
void main()
{
int gd=DETECT,gm,i;
char p[18][18]={"0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15"};
char q[10][10]={"0","1","2","3","4","5","6","7","8","9"};
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
setcolor(YELLOW);
rectangle(10,20,629,460);
setcolor(WHITE);
outtextxy(300,60,"BARCHART");
line(100,380,100,60);
line(100,380,600,380);
line(90,70,100,60);
line(110,70,100,60);
line(590,370,600,380);
line(590,390,600,380);
for(i=0;i<15;i++)
{
outtextxy(84,370-20*i,p[i]);
}
for(i=4;i>=1;i--)
{
outtextxy(90+90*i,385,q[i]);

Department of Computer science (MCA V) Page 46


Computer Graphics

}
outtextxy(95,45,"y");
outtextxy(610,380,"x");
outtextxy(330,405,"Overs");
settextstyle(DEFAULT_FONT,VERT_DIR,1);
outtextxy(70,200,"Runs");

setfillstyle(LINE_FILL,BLUE);
bar3d(150,100,200,379,30,20);
setfillstyle(XHATCH_FILL,RED);
bar(245,150,295,379,30,20);
setfillstyle(WIDE_DOT_FILL,GREEN);
bar3d(340,200,390,379,30,20);
setfillstyle(INTERLEAVE_FILL,MAGENTA);
bar3d(435,125,485,379,30,20);
getch();

Department of Computer science (MCA V) Page 47


Computer Graphics

OUTPUT:

Department of Computer science (MCA V) Page 48

You might also like