Practical no 4
// C-program for circle drawing
// using Bresenham’s Algorithm
// in computer-graphics
#include <stdio.h>
#include <dos.h>
#include <graphics.h>
// Function to put pixels
// at subsequence points
void drawCircle(int xc, int yc, int x, int y){
putpixel(xc+x, yc+y, RED);
putpixel(xc-x, yc+y, RED);
putpixel(xc+x, yc-y, RED);
putpixel(xc-x, yc-y, RED);
putpixel(xc+y, yc+x, RED);
putpixel(xc-y, yc+x, RED);
putpixel(xc+y, yc-x, RED);
putpixel(xc-y, yc-x, RED);
// Function for circle-generation
// using Bresenham's algorithm
void circleBres(int xc, int yc, int r){
int x = 0, y = r;
int d = 3 - 2 * r;
drawCircle(xc, yc, x, y);
while (y >= x){
// check for decision parameter
// and correspondingly
// update d, y
if (d > 0) {
y--;
d = d + 4 * (x - y) + 10;
else
d = d + 4 * x + 6;
// Increment x after updating decision parameter
x++;
// Draw the circle using the new coordinates
drawCircle(xc, yc, x, y);
delay(50);
int main()
int xc = 50, yc = 50, r = 30;
int gd = DETECT, gm;
initgraph(&gd, &gm, ""); // initialize graph
circleBres(xc, yc, r); // function call
return 0;
}
Practical no 5
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void flood(int,int,int,int);
void main()
intgd=DETECT,gm;
initgraph(&gd,&gm,"C:/TURBOC3/bgi");
rectangle(50,50,250,250);
flood(55,55,10,0);
getch();
void flood(intx,inty,intfillColor, intdefaultColor)
if(getpixel(x,y)==defaultColor)
delay(1);
putpixel(x,y,fillColor);
flood(x+1,y,fillColor,defaultColor);
flood(x-1,y,fillColor,defaultColor);
flood(x,y+1,fillColor,defaultColor);
flood(x,y-1,fillColor,defaultColor);
}
Praactical no 6
/* Boundary Fill Algorithm */
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void boundaryFill4(int x, int y, int fill_color,int boundary_color)
if(getpixel(x, y) != boundary_color &&
getpixel(x, y) != fill_color)
putpixel(x, y, fill_color);
boundaryFill4(x + 1, y, fill_color, boundary_color);
boundaryFill4(x, y + 1, fill_color, boundary_color);
boundaryFill4(x - 1, y, fill_color, boundary_color);
boundaryFill4(x, y - 1, fill_color, boundary_color);
//driver code
int main()
// gm is Graphics mode which is
// a computer display mode that
// generates image using pixels.
// DETECT is a macro defined in
// "graphics.h" header file
int gd = DETECT, gm;
int x , y , radius;
// initgraph initializes the
// graphics system by loading a
// graphics driver from disk
printf("\n\n Enter x,y coordinates and radius of circle: ");
scanf("%d%d%d",&x,&y,&radius);
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
// circle fuction
circle(x, y, radius);
// Function calling
boundaryFill4(x, y, 6, 15);
delay(10000);
getch();
// closegraph function closes the
// graphics mode and deallocates
// all memory allocated by
// graphics system .
closegraph();
return 0;
}
Practical no 7,8,9
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int x=330,y=250;
int a[4][2]={{50,50},{100,50},{100,100},{50,100}};
void draw(int x,int y)
setcolor(WHITE);
line(x,y-250,x,y+250);
line(x-330,y,x+330,y);
void stat(int x,int y)
line(x+a[0][0],y-a[0][1],x+a[1][0],y-a[1][1]);
line(x+a[1][0],y-a[1][1],x+a[2][0],y-a[2][1]);
line(x+a[2][0],y-a[2][1],x+a[3][0],y-a[3][1]);
line(x+a[3][0],y-a[3][1],x+a[0][0],y-a[0][1]);
void translate(int x,int y)
int tx,ty,xt,yt;
cleardevice();
printf("\nEnter translation factor (tx,ty): ");
scanf("%d%d",&tx,&ty);
cleardevice();
draw(x,y);
setcolor(WHITE);
stat(x,y);
xt=x+tx;
yt=y+ty;
getch();
setcolor(GREEN);
stat(xt,yt);
void rotate(int x,int y)
double d,x2r,y2r,x3r,y3r,x4r,y4r;
cleardevice();
printf("\nEnter Angle of Rotation: ");
scanf("%lf",&d);
cleardevice();
draw(x,y);
setcolor(WHITE);
stat(x,y);
d=(d*3.14)/180;
x2r=a[0][0]+(((a[1][0]-a[0][0])*cos(d))-((a[1][1]-a[0][1])*sin(d)));
y2r=a[0][1]+(((a[1][0]-a[0][0])*sin(d))+((a[1][1]-a[0][1])*cos(d)));
x3r=a[0][0]+(((a[2][0]-a[0][0])*cos(d))-((a[2][1]-a[0][1])*sin(d)));
y3r=a[0][1]+(((a[2][0]-a[0][0])*sin(d))+((a[2][1]-a[0][1])*cos(d)));
x4r=a[0][0]+(((a[3][0]-a[0][0])*cos(d))-((a[3][1]-a[0][1])*sin(d)));
y4r=a[0][1]+(((a[3][0]-a[0][0])*sin(d))+((a[3][1]-a[0][1])*cos(d)));
getch();
setcolor(GREEN);
line(x+a[0][0],y-a[0][1],x+x2r,y-y2r);
line(x+x2r,y-y2r,x+x3r,y-y3r);
line(x+x3r,y-y3r,x+x4r,y-y4r);
line(x+x4r,y-y4r,x+a[0][0],y-a[0][1]);
void scale(int x,int y)
{
int sx,sy;
cleardevice();
printf("\nEnter scaling factor (sx,sy): ");
scanf("%d%d",&sx,&sy);
cleardevice();
draw(x,y);
setcolor(WHITE);
stat(x,y);
getch();
setcolor(GREEN);
line(x+a[0][0]*sx,y-a[0][1]*sy,x+a[1][0]*sx,y-a[1][1]*sy);
line(x+a[1][0]*sx,y-a[1][1]*sy,x+a[2][0]*sx,y-a[2][1]*sy);
line(x+a[2][0]*sx,y-a[2][1]*sy,x+a[3][0]*sx,y-a[3][1]*sy);
line(x+a[3][0]*sx,y-a[3][1]*sy,x+a[0][0]*sx,y-a[0][1]*sy);
void reflect(int x,int y)
int x1,y1,ch;
printf("\[Link] X-axis\[Link] Y-axis:\nEnter choice: ");
scanf("%d",&ch);
cleardevice();
draw(x,y);
setcolor(WHITE);
stat(x,y);
getch();
x1=-1;
y1=1;
setcolor(GREEN);
if(ch==2)
line(x+a[0][0]*x1,y-a[0][1]*y1,x+a[1][0]*x1,y-a[1][1]*y1);
line(x+a[1][0]*x1,y-a[1][1]*y1,x+a[2][0]*x1,y-a[2][1]*y1);
line(x+a[2][0]*x1,y-a[2][1]*y1,x+a[3][0]*x1,y-a[3][1]*y1);
line(x+a[3][0]*x1,y-a[3][1]*y1,x+a[0][0]*x1,y-a[0][1]*y1);
else
line(x-a[0][0]*x1,y+a[0][1]*y1,x-a[1][0]*x1,y+a[1][1]*y1);
line(x-a[1][0]*x1,y+a[1][1]*y1,x-a[2][0]*x1,y+a[2][1]*y1);
line(x-a[2][0]*x1,y+a[2][1]*y1,x-a[3][0]*x1,y+a[3][1]*y1);
line(x-a[3][0]*x1,y+a[3][1]*y1,x-a[0][0]*x1,y+a[0][1]*y1);
void shear(int x,int y)
int shr,sh1,sh2,shear;
cleardevice();
printf("\n1: Shear w.r.t X-axis\n2: Shear w.r.t Y-axis");
printf("\nEnter choice: ");
scanf("%d",&shear);
printf("\nEnter shear value: ");
scanf("%d",&shr);
cleardevice();
draw(x,y);
setcolor(WHITE);
stat(x,y);
getch();
setcolor(GREEN);
switch(shear)
case 1:
{
sh1=shr*a[2][1];
sh2=shr*a[3][1];
line(x+a[0][0],y-a[0][1],x+a[1][0],y-a[1][1]);
line(x+a[1][0],y-a[1][1],x+a[2][0]+sh1,y-a[2][1]);
line(x+a[2][0]+sh1,y-a[2][1],x+a[3][0]+sh2,y-a[3][1]);
line(x+a[3][0]+sh2,y-a[3][1],x+a[0][0],y-a[0][1]);
break;
case 2:
sh1=shr*a[1][0];
sh2=shr*a[2][0];
line(x+a[0][0],y-a[0][1],x+a[1][0],y-a[1][1]-sh2);
line(x+a[1][0],y-a[1][1]-sh2,x+a[2][0],y-a[2][1]-sh2);
line(x+a[2][0],y-a[2][1]-sh2,x+a[3][0],y-a[3][1]);
line(x+a[3][0],y-a[3][1],x+a[0][0],y-a[0][1]);
break;
void main()
int ch;
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
cleardevice();
printf("\n1: TRANSLATION \n2: ROTATION \n3: SCALING \n4: REFLECTION \n5: SHEAR");
printf("\nEnter choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: translate(x,y);
break;
case 2: rotate(x,y);
break;
case 3: scale(x,y);
break;
case 4: reflect(x,y);
break;
case 5: shear(x,y);
break;
default:printf("Wrong choice");
break;
getch();
closegraph();
restorecrtmode();
}
Translation
Rotation
Scaling
Reflection
Shear
Practical no 10,11
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int maxx,maxy,midx,midy;
void axis()
getch();
cleardevice();
line(midx,0,midx,maxy);
line(0,midy,maxx,midy);
void main()
int gd,gm,x,y,z,ang,x1,x2,y1,y2;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
setfillstyle(3,25);
maxx=getmaxx();
maxy=getmaxy();
midx=maxx/2;
midy=maxy/2;
outtextxy(100,100,"ORIGINAL OBJECT");
line(midx,0,midx,maxy);
line(0,midy,maxx,midy);
bar3d(midx+100,midy-20,midx+60,midy-90,20,5);
axis();
outtextxy(100,20,"TRANSLATION");
printf("\n\n Enter the Translation vector: ");
scanf("%d%d",&x,&y);
bar3d(midx+100,midy-20,midx+60,midy-90,20,5);
bar3d(midx+(x+100),midy-(y+20),midx+(x+60),midy-(y+90),20,5);
axis();
outtextxy(100,20,"SCALING");
printf("\n Enter the Scaling Factor: ");
scanf("%d%d%d",&x,&y,&z);
bar3d(midx+100,midy-20,midx+60,midy-90,20,5);
bar3d(midx+(x*100),midy-(y*20),midx+(x*60),midy-(y*90),20*z,5);
axis();
outtextxy(100,20,"ROTATION");
printf("\n Enter the Rotation angle: ");
scanf("%d",&ang);
x1=100*cos(ang*3.14/180)-20*sin(ang*3.14/180);
y1=100*sin(ang*3.14/180)+20*sin(ang*3.14/180);
x2=60*cos(ang*3.14/180)-90*sin(ang*3.14/180);
y2=60*sin(ang*3.14/180)+90*sin(ang*3.14/180);
axis();
printf("\n After rotating about z-axis\n");
bar3d(midx+100,midy-20,midx+60,midy-90,20,5);
bar3d(midx+x1,midy-y1,midx+x2,midy-y2,20,5);
axis();
printf("\n After rotating about x-axis\n");
bar3d(midx+100,midy-20,midx+60,midy-90,20,5);
bar3d(midx+100,midy-x1,midx+60,midy-x2,20,5);
axis();
printf("\n After rotating about y-axis\n");
bar3d(midx+100,midy-20,midx+60,midy-90,20,5);
bar3d(midx+x1,midy-20,midx+x2,midy-90,20,5);
axis();
closegraph(); }
3d Translation
Rotation
3D Scaling
Practical no 12
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int xwmax=300,xwmin=200,ywmax=100,ywmin=200,ax,ay,bx,by;
void input()
printf("Enter points in a & b:\n");
scanf("%d%d%d%d",&ax,&ay,&bx,&by);
void draw()
rectangle(xwmin,ywmin,xwmax,ywmax);
void clip(int x,int y,int p[4])
if(y<ywmax)
p[0]=1;
if(y>ywmin)
p[1]=1;
if(x>xwmax)
p[2]=1;
if(x<xwmin)
p[3]=1;
else
p[3]=0;
void main()
int gd=DETECT,gm,y,x,c,p1[4],p2[4],p3[4],i;
float m;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
cleardevice();
input();
cleardevice();
clip(ax,ay,p1);
clip(bx,by,p2);
for(i=0;i<4;i++)
p3[3]=p1[i]&&p2[i];
for(i=0;i<4;i++)
if(p3[i]==1)
break;
draw();
line(ax,ay,bx,by);
getch();
cleardevice();
if(i!=4)
draw();
else
m=(float)(by-ay)/(bx-ax);
if(p1[0]==1)
y=ywmax;
if(p1[1]==1)
y=ywmin;
if(p1[0]==1||p1[1]==1)
ax=ax+(y-ay)/m;
ay=y;
if(p2[0]==1)
y=ywmax;
if(p2[1]==1)
y=ywmin;
if(p2[0]==1||p2[1]==1)
bx=bx+(y-by)/m;
by=y;
if(p1[2]==1)
x=xwmax;
if(p1[3]==1)
x=xwmin;
if(p1[2]==1||p1[3]==1)
ay=ay+m*(x-ax);
ax=x;
if(p2[2]==1)
x=xwmax;
if(p2[3]==1)
x=xwmin;
if(p2[2]==1||p2[3]==1)
by=by+m*(x-bx);
bx=x;
draw();
line(ax,ay,bx,by);
getch();
closegraph();
restorecrtmode();}
Practical no 13
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <dos.h>
#include <math.h>
#include <graphics.h>
typedef struct coordinate
int x,y;
char code[4];
}PT;
void drawwindow();
void drawline (PT p1,PT p2);
PT setcode(PT p);
int visibility (PT p1,PT p2);
PT resetendpt (PT p1,PT p2);
main()
int gd=DETECT, gm,v;
PT p1,p2,ptemp;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
cleardevice();
printf("ENTER END-POINT 1 (x,y):");
scanf("%d%d",&p1.x,&p1.y);
printf("\n ENTER END-POINT 2 (x,y):");
scanf("%d%d",&p2.x,&p2.y);
cleardevice();
drawwindow();
getch();
drawline(p1,p2);
getch();
cleardevice();
drawwindow();
midsub(p1,p2);
getch();
closegraph();
return(0);
}midsub(PT p1,PT p2)
PT mid;
int v;
p1=setcode(p1);
p2=setcode(p2);
v=visibility(p1,p2);
switch(v)
case 0:
drawline(p1,p2);
break;
case 1:break;
case 2:
mid.x = p1.x + (p2.x-p1.x)/2;
mid.y = p1.y + (p2.y-p1.y)/2;
midsub(p1,mid);
mid.x = mid.x+1;
mid.y = mid.y+1;
midsub(mid,p2);
break;
void drawwindow()
{
setcolor(RED);
line(150,100,450,100);
line(450,100,450,400);
line(450,400,150,400);
line(150,400,150,100);
void drawline (PT p1,PT p2)
setcolor(15);
line(p1.x,p1.y,p2.x,p2.y);
PT setcode(PT p)
PT ptemp;
if(p.y<=100) [Link][0]=1; else [Link][0]=0; if(p.y>=400)
[Link][1]=1;
else
[Link][1]=0;
if (p.x>=450)
[Link][2]=1;
else
[Link][2]=0;
if (p.x<=150)
[Link][3]=1;
else
[Link][3]=0;
ptemp.x=p.x;
ptemp.y=p.y;
return(ptemp);
int visibility (PT p1,PT p2)
int i,flag=0;
for(i=0;i<4;i++)
if(([Link][i]!=0)||([Link][i]!=0))
flag=1;
if(flag==0)
return(0);
for(i=0;i<4;i++)
if(([Link][i]==[Link][i]) &&([Link][i]==1))
flag=0;
if(flag==0)
return(1);
return(2);
}
Pracical no 14
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<stdlib.h>
int main()
int gd,gm,n,*x,i,k=0;
//window coordinates int
wx1=220,wy1=140,wx2=420,wy2=140,wx3=420,wy3=340,wx4=220,wy4=340;
int w[]={220,140,420,140,420,340,220,340,220,140};//array for drawing window
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\turboc3\\bgi"); //initializing graphics
printf("Window:-");
setcolor(RED); //red colored window
drawpoly(5,w); //window drawn
printf("Enter the no. of vertices of polygon: ");
scanf("%d",&n);
x = malloc(n*2+1);
printf("Enter the coordinates of points:\n");
k=0;
for(i=0;i<n*2;i+=2) //reading vertices of polygon
printf("(x%d,y%d): ",k,k);
scanf("%d,%d",&x[i],&x[i+1]);
k++;
x[n*2]=x[0]; //assigning the coordinates of first vertex to last additional vertex for drawpoly
method.
x[n*2+1]=x[1];
setcolor(WHITE);
drawpoly(n+1,x);
printf("\nPress a button to clip a polygon..");
getch();
setcolor(RED);
drawpoly(5,w);
setfillstyle(SOLID_FILL,BLACK);
floodfill(2,2,RED);
gotoxy(1,1); //bringing cursor at starting position
printf("\nThis is the clipped polygon..");
getch();
cleardevice();
closegraph();
return 0;
}
Practical no 15
#include<graphics.h>
#include<math.h>
#include<conio.h>
#include<stdio.h>
void main()
int x[4],y[4],i;
double put_x,put_y,t;
int gr=DETECT,gm;
initgraph(&gr,&gm,"C:\\TURBOC3\\BGI");
printf("\n****** Bezier Curve ***********");
printf("\n Please enter x and y coordinates ");
for(i=0;i<4;i++)
scanf("%d%d",&x[i],&y[i]);
putpixel(x[i],y[i],3); // Control Points
for(t=0.0;t<=1.0;t=t+0.001) // t always lies between 0 and 1
put_x = pow(1-t,3)*x[0] + 3*t*pow(1-t,2)*x[1] + 3*t*t*(1-t)*x[2] + pow(t,3)*x[3]; // Formula to draw
curve
put_y = pow(1-t,3)*y[0] + 3*t*pow(1-t,2)*y[1] + 3*t*t*(1-t)*y[2] + pow(t,3)*y[3];
putpixel(put_x,put_y, WHITE); // putting pixel
getch();
closegraph();