Write A C-Program To Draw Basic Shapes Such As Line, Rectangle, Square, Rectangle, Elipse
Write A C-Program To Draw Basic Shapes Such As Line, Rectangle, Square, Rectangle, Elipse
Write A C-Program To Draw Basic Shapes Such As Line, Rectangle, Square, Rectangle, Elipse
OUTPUT:
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);
}
for(y=my;y>=0;y--)
{
putpixel(0,y,10);
delay(5);
}
getch();
}
OUTPUT:
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);
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();
}
OUTPUT:
#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();
}
OUTPUT:
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);
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();
}
OUTPUT:
#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();
}
OUTPUT:
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;
}
}
OUTPUT:
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++)
{
x+=xc;
y+=yc;
putpixel(x,y,10);
}
getch();
closegraph();
}
OUTPUT:
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;
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();
}
OUTPUT:
#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;
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);
}
OUTPUT:
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:
}
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 :
#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();
OUTPUT:
OUTPUT:
#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;
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];
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;
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)
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);
}
OUTPUT:
#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);
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();
OUTPUT:
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]);
for(i=0;i<4;i++)
line(x[i],y[i],x[i+4],y[i+4]);
getch();
}
OUTPUT:
}
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();
OUTPUT: