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

#Include #Include Void Main (Int GD DETECT, GM Initgraph (&gd,&gm,"d://tc//bgi") Setcolor (15) Line (100,200,100,300) Getch Closegraph )

This document contains 16 code snippets demonstrating various computer graphics concepts in C programming, including: 1. Drawing basic shapes like lines, circles, and colored bar graphs. 2. Translating and scaling objects using 2D transformations. 3. Drawing curves and graphs using algorithms like DDA line, Bresenham's circle, and sinusoidal graphs. 4. Applying clipping to texts, lines, and points to restrict drawing within a viewport.

Uploaded by

Vishu Aeri
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
106 views

#Include #Include Void Main (Int GD DETECT, GM Initgraph (&gd,&gm,"d://tc//bgi") Setcolor (15) Line (100,200,100,300) Getch Closegraph )

This document contains 16 code snippets demonstrating various computer graphics concepts in C programming, including: 1. Drawing basic shapes like lines, circles, and colored bar graphs. 2. Translating and scaling objects using 2D transformations. 3. Drawing curves and graphs using algorithms like DDA line, Bresenham's circle, and sinusoidal graphs. 4. Applying clipping to texts, lines, and points to restrict drawing within a viewport.

Uploaded by

Vishu Aeri
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 23

/ * 1.

Write a Simple program to draw a line * /


#include<graphics.h>
#include<conio.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"d:\\tc\\bgi");
setcolor(15);
line(100,200,100,300);
getch();
closegraph();
}

/ * 2 Write a simple program to draw a circle */


#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"d:\\tc\\bgi");
circle(270,215,50);
printf("*********OUTPUT***********");
getch();
closegraph();
}

/ * 3. Write a program to draw a colored bar diagram */


#include<graphics.h>
#include<conio.h>
#include<stdio.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"d:\\tc\\bgi");
line(50,50,50,300);
line(50,300,300,300);
setfillstyle(1,4);
bar(75,75,95,300);
setfillstyle(1,3);
bar(125,85,145,300);
setfillstyle(1,6);
bar(175,125,195,300);
setfillstyle(1,3);
bar(225,195,245,300);
getch();
closegraph();
}

/ * 4.Write a program to translate a circle */


#include<stdio.h>#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd=DETECT,gm,r,xc,yc,tx,ty;
initgraph(&gd,&gm,"d:\\tc\\bgi");
printf("Enter the center x:");
scanf("%d",&xc);
printf("Enter the center y:");
scanf("%d",&yc);
printf("Enter the radius:");
scanf("%d",&r);
circle(xc,yc,r);
getch();
printf("Enter tx,ty");
scanf("%d%d",&tx,&ty);
circle(xc+tx,yc+ty,r);
getch();
}

/ * 5. WAP to draw a Pie chart using function */


#include<graphics.h>
#include<stdio.h>
void main()
{
int gd=DETECT;
int gm;
initgraph(&gd, &gm, "c:\\tc\\bin\\bgi");
setfillstyle(1, RED);
pieslice(80,80,0,45,40);
setfillstyle(2, GREEN);
pieslice(80,80,45,90,40);
setfillstyle(3, CYAN);
pieslice(80,80,90,135,40);
setfillstyle(4, MAGENTA);
pieslice(80,80,135,180,40);
setfillstyle(5, YELLOW);
pieslice(80,80,180,225,40);
setfillstyle(7,GREEN);
pieslice(80,80,225,270,40);
setfillstyle(5,RED);
pieslice(80,80,270,315,40);
setfillstyle(2,YELLOW);
pieslice(80,80,315,360,40);
setfillstyle(1,CYAN);
setcolor(MAGENTA);
rectangle(200,200,300,300);
floodfill(250,250,MAGENTA );
getch();
closegraph();
}

/ 6. * WAP to draw a Pie Chart * /


#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT;
int gm;
int x[10],n,i;
float a,b,sum;
initgraph( &gd, &gm, "c:\\tc\\bin\\bgi");
printf("Enter limit");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Marks of %d position",i+1);
scanf("%d",&x[i]);
}
sum=0;
a=0;
for(i=0;i<n;i++)
{
sum=sum+x[i];
}
for(i=0;i<n;i++)
{
b=360/sum*x[i];
setfillstyle(i,i);
b=a+b;
pieslice(300,200,a,b,50);
a=b;
}
getch();
// closegraph();
}

/ * 7. WAP to draw a DDA Line */


#include<graphics.h>
#include<conio.h>
#include<math.h>
void ddaline(int ,int ,int ,int );
int round(float );
void main()
{
int gd= DETECT;
int gm,x1,y1,x2,y2;
initgraph(&gd,&gm,"c:\\tc\\bgi");
printf("enter the value of coordinates of first end point:");
scanf("%d%d",&x1,&y1);
printf("enter the value of coordinates of second end point:");
scanf("%d%d",&x2,&y2);
ddaline(x1,y1,x2,y2);
getch();
}
void ddaline(int x1,int y1,int x2,int y2)
{
int i,dy,dx,steps,x_round,y_round;
float y_incr,x_incr,x_init,y_init;
dy=y2-y1;
dx=x2-x1;
if(abs(dy)>abs(dx))
steps=dy;
else
steps=dx;
y_incr=dy/steps;
x_incr= dx/steps;
x_init=x1;
y_init=y1;
putpixel(x_init,y_init,1);
for(i=1;i<steps;i++)
{
x_init+=x_incr;
y_init+=y_incr;
x_round = round(x_init);
y_round= round(y_init);
putpixel(round(x_round),round(y_round),1);
}
}
int round(float x_ini)
{

int x_round;
x_ini=x_ini+0.5;
x_round=x_ini;
return(x_round);
}

/ * 8. WAP to draw a Circle * /


#include<graphics.h>
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int x1,y1,r,p,a;
int gd=DETECT,gm,x,y;
printf("Enter the Coordinate x1-->") ;
scanf("%d",&x);
printf("Enter the Coordinate x2-->");
scanf("%d",&y);
printf("Enter the Radius r-->");
scanf("%d",&r);
initgraph(&gd,&gm,"c:\\tc\\bin\\bgi");
x1=0;
y1=r;
p=3-2*r;
while(x1<y1)
{
plotpoint(x,y,x1,y1);
if(p<0)
p=p+4*x1+6;
else
{
p=p+4*(x1-y1)+10;
y1=y1-1;
}
x1=x1+1;
}
if(x1==y1)
plotpoint(x,y,x1,y1);
getch();
closegraph();
}
plotpoint(int x,int y,int x1,int y1)
{
putpixel(x+x1,y+y1,RED);
putpixel(x-x1,y+y1,RED);
putpixel(x+x1,y-y1,GREEN);
putpixel(x-x1,y-y1,GREEN);

putpixel(x+y1,y+x1,CYAN);
putpixel(x-y1,y+x1,CYAN);
putpixel(x+y1,y-x1,MAGENTA);
putpixel(x-y1,y-x1,MAGENTA);
}

/ * 9. WAP to draw SinCos Graph */


#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
#define angle 3.14/180
#define radian 180/3.14
void main()
{
int gd=DETECT;
int gm,a;
int x,y,ang;
float y1;

initgraph(&gd,&gm,"c:\\tc\\bgi");
printf("enter 1 for cos");
printf("\nenter 2 for sin");
scanf("%d",&a);
printf("enter the angle:");
scanf("%d",&ang);
line(200,200,200,400);
line(100,300,300,300);
switch(a)
{
case 1:
for(x=0;x<ang;x++)
{
y1=cos(x*angle);
y=y1*radian;
putpixel(x+200,300-y,3);
}
break;
case 2:
for(x=0;x<ang;x++)
{
y1=sin(x*angle);
y=y1*radian;
putpixel(x+200,300-y,3);
}
break;
}
getch() }

/ * 10. WAP to draw Ellipse */


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd=DETECT,gm,i,x,y,xc,yc;
long d1,d2,rx,ry,rx2,ry2;
clrscr();
printf("/nenter the X radius of ellipse:");
scanf("%d",&rx);
printf("/n enter the Y radius of ellipse:");

scanf("%d",&ry);
printf("/n enter the center of ellipse:");
printf("/enter X-coordinate:");
scanf("%d",&xc);
printf("/enter Y-coordinate:");
scanf("%d",&yc);
initgraph(&gd,&gm,"c:\\tc\\bgi");
line(320,0,320,479);
line(0,240,639,240);
rx2=rx*rx;
ry2=ry*ry;
x=0;
y=ry;
d1=ry2-rx2*ry+0.25*rx2;
do
{
putpixel(320+xc+x,240-yc+y,15);
putpixel(320+xc-x,240-yc+y,15);
putpixel(320+xc+x,240-yc-y,15);
putpixel(320+xc-x,240-yc+y,15);
if(d1<0)
d1+=2*ry2*x+3*ry2;
else
{
y--;
d1+=2*ry2*x+3*ry2-2*rx2*y+2*rx2;
}
x++;
}while(2*ry2*x<2*rx2*y);
d2=ry2*(x+0.5)*(x+0.5)+rx2*(y-1)*(y-1)-rx2*ry2;
do
{
putpixel(320+xc+x,240-yc+y,15);
putpixel(320+xc-x,240-yc-y,15);
putpixel(320+xc+x,240-yc-y,15);
putpixel(320+xc-x,240-yc+y,15);
if(d2>0)
d2+=3*rx2-2*rx2*y;
else
{
x++;
d2+=2*ry2*x+3*ry2-2*rx2*y+2*rx2;
}
y--;
}while(y>0);
getch();

closegraph();
}

/* 11 Write a program of translate an object in 2d transformation * /


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm,x1,x2,x3,y1,y2,y3,tx,ty;
initgraph(&gd,&gm,"d:\\tc\\bgi");
printf("enter the value of the coordinates of three points of triangle");
scanf("\n%d\n%d\n%d\n%d\n%d\n%d",&x1,&y1,&x2,&y2,&x3,&y3);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x1,y1,x3,y3);

printf("\n Enter the value of translation vector");


scanf("\n%d\n%d",&tx,&ty);
line(x1+tx,y1+ty,x2+tx,y2+ty);
line(x2+tx,y2+ty,x3+tx,y3+ty);
line(x1+tx,y1+ty,x3+tx,y3+ty);
getch();
}

/ * 12 Write a program of scaling an object in 2D transformation */


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm,x1,x2,x3,y1,y2,y3,sx,sy;
initgraph(&gd,&gm,"d:\\tc\\bgi");
printf("enter the value of the coordinates of three points of triangle");
scanf("\n%d\n%d\n%d\n%d\n%d\n%d",&x1,&y1,&x2,&y2,&x3,&y3);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x1,y1,x3,y3);

printf("\n Enter the value of scaling vector");


scanf("\n%d\n%d",&sx,&sy);
line(x1*sx,y1*sy,x2*sx,y2*sy);
line(x2*sx,y2*sy,x3*sx,y3*sy);
line(x1*sx,y1*sy,x3*sx,y3*sy);
getch();
}

/*13 Write a program to draw a line using bresenham algo


*/
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd= DETECT,gm,x,y,x1,y1,x2,y2,dx,dy,d,ds,dt;
initgraph(&gd,&gm,"d:\\tc\\bgi");
printf("\n enter the value of starting coordinates");
scanf("\n%d\n%d",&x1,&y1);
printf("\n enter the value of ending coordinates");
scanf("\n%d\n%d",&x2,&y2);

dx=x2-x1;
dy=y2-y1;
dt=2*(dy-dx);
ds=2*dy;
d=2*dy-dx;
if(dx>0)
{
y=y1;
for(x=x1;x<=x2;x++)
{
if(d<0)
d=d+ds;
else
{
d=d+dt;
y++;
}
putpixel(x,y,6);
}
}
getch();
}

/ * 14 Write a program of text clipping */


#include<conio.h>

#include<graphics.h>
void main()
{
int gm=DETECT,gd,xmin,xmax,ymin,ymax,x,y;
initgraph(&gm,&gd,"d:\\tc\\bgi");
line(100,100,400,100);
line(100,300,400,300);
line(100,100,100,300);
line(400,100,400,300);
xmin=100;
xmax=400;
ymin=100;
ymax=400;
printf("*************OUTPUT***********");
printf("\n Enter value for x and y coordinates:");
scanf("\n%d\n%d",&x,&y);
outtextxy(x,y,"Computer Graphics");
if(x>xmin && x<xmax && y>ymin && y<ymax)
printf("Text is inside the view-plane");
else if((x==xmin || x==xmax) &&(y==ymin || y==ymax))
{
printf("\n Text is on the view-plane");
}
else
{
printf("\n Text is outside the view-plane");
}
getch();
}

/ * 15 Write a program of line clipping * /


#include<conio.h>
#include<graphics.h>
void main()
{
int gm=DETECT,gd,xmin,xmax,ymin,ymax,x,y,z,z1;
initgraph(&gm,&gd,"d:\\tc\\bgi");
line(200,100,400,100);
line(400,100,400,300);
line(200,100,200,300);
line(200,300,400,300);
xmin=100;
xmax=400;
ymin=100;
ymax=300;
printf("*************OUTPUT***********");
printf("\n Enter value for x,y and x1,y1 coordinates:");
scanf("\n%d\n%d\n%d\n%d",&x,&y,&z,&z1);
line(x,y,z,z1);
if(x>xmin && x<xmax && y>ymin && y<ymax)
{
if(z>xmin && z<xmax && z1<ymax && z1>ymin)
{
line(x,y,z,z1);
printf("\n line is inside the view-plane");
}
}
else
{
printf("\n line is outside the view-plane");
}
getch();
}

/ * 16 Write a program of point clipping */


#include<conio.h>
#include<graphics.h>
void main()
{
int gm=DETECT,gd,xmin,xmax,ymin,ymax,x,y;
initgraph(&gm,&gd,"d:\\tc\\bgi");
line(100,100,400,100);
line(100,300,400,300);
line(100,100,100,300);
line(400,100,400,300);
xmin=100;
xmax=400;
ymin=100;
ymax=300;
printf("\n Enter value for x and y coordinates:");
scanf("%d %d",&x,&y);
putpixel(x,y,WHITE);
if(x>xmin && x<xmax && y>ymin && y<ymax)
printf("\n point is inside the view-plane");
elseif((x==xmin || x==xmax)&&(y==ymin || y==ymax))
{
printf("\n point is on the view-plane");
}
else
printf("\n point is outside the view-plane");
getch();
}

You might also like