You are on page 1of 46

Ex.

No:1 (a)
To implement Bresenhams algorithm for line, circle and ellipse drawing.

(a)Line Drawing Using Bresenhams Algorithm.

Aim:
To write a C program to draw a line by using Bresenhams algorithm.

Algorithm:

Step1:Start the program.


Step2:Input the starting and end points and find the dx and dy values.
Step3:Set the p value as p=2*(dy-dx).
Step4:Check xa>xb then store (xb,yb)in (x,y) otherwise store (xa,xb)in (x,y).
Step5:Put a pixel and check p value to set the value by the formula p=p+2*dy-dx.
Step6:Repeat the step 5 until x<xend.
Step7:Display the line.
Step8:Stop the program.

Program:
#include<stdio.h>
#include<graphics.h>
void main()
{
int dx,dy,x,y,xend,p,gd=DETECT,gm,xa,xb,ya,yb;
printf("Line drawing using Bresenham's Algorithm:\n");
printf("Enter the starting point and ending point:");
scanf("%d%d%d%d",&xa,&ya,&xb,&yb);
initgraph(&gd,&gm,"c:/tc/bgi");
dx=abs(xa-xb);
dy=abs(ya-yb);
p=2*(dy-dx);
if(xa>xb)
{
x=xb;
y=yb;
xend=xa;
}
else
{
x=xa;
y=ya;
xend=xb;
}
putpixel(x,y,12);
do
{
if(p<0)

1
p+=2*dy;
else
{
y++;
x++;
p+=2*dy;
}
putpixel(x,y,12);
}
while(x<xend);
getch();
closegraph();
}

2
EX. No 1.(b)
Circle Drawing using Bresenhams Algorithm.

Aim:
To write a C program to draw a Circle by using Bresenhams algorithm.

Algorithm:
Step1:Read the radius(r)of the circle.
Step2:Iniatialize the graphics mode.
Step3:Initialize the starting point as x=0,y=r.
Step4:Initialize the decision variable=3-2*r.
Step5:Assign plot(x,y),if d<0 then store d=d+4x+6 otherwise stored=d+4(x-y)+10,y=y-1 and
increment the value of x by 1.
Step6:Repeat the step 4to5 until (x<y).
Step7:Stop the program.

Program:
#include<stdio.h>
#include<graphics.h>
#include<math.h>
void main()
{
float d;
int gd,gm,x,y;
int r;
clrscr();
printf("Enter the radius of a circle:");
scanf("%d",&r);
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:/tc/bgi");
x=0;
y=r;
d=3-2*r;
do
{
putpixel(200+x,200+y,15);
putpixel(200+y,200+x,15);
putpixel(200+y,200-x,15);
putpixel(200+x,200-y,15);
putpixel(200-x,200-y,15);
putpixel(200-y,200-x,15);
putpixel(200-y,200+x,15);
putpixel(200-x,200+y,15);
if(d<=0)
{
d=d+4*x+6;

3
}
else
{
d=d+4*(x-y)+10;
y=y-1;
}
x=x+1;
delay(1000);
}
while(x<y);
getch();
closegraph();
}

EX. No 1. (c)
Implementation of Ellipse Drawing Algorithm.

4
Aim:
Step1:Start the program.
Step2:Declare all the variables and get the values for the variables.
Step3:Using the center point of the ellipse and x,y& radius calculate the pixels on the outer line
of ellipse using the formula.
Step4:Plot the outer line points of the ellipse.
Step5:Cover the ellipse by plotting pixels in some points by calculating it using the formula.
Step6:Terminate the program.

Program:
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
int xc=0,yc=0,rx,ry,p,px,py,x,y;
int ry2,rx2,try2,trx2;
plotpoints(int xc,int yc,int x,int y)
{
putpixel(xc+x,yc+y,3);
putpixel(xc-x,yc+y,3);
putpixel(xc+x,yc-y,3);
putpixel(xc-x,yc-y,3);
return 1;
}
void main()
{
int gd=DETECT,gm;
int xc=200,yc=200,rx,ry;
initgraph(&gd,&gm,"c:/tc/bgi");
printf("\nEnter the center point and x & y radius:");
scanf("%d%d",&rx,&ry);
ry2=ry*ry;
rx2=rx*rx;
try2=2*ry2;
trx2=2*rx2;
x=0;
y=ry;
plotpoints(xc,yc,x,y);
p=(int)(ry2-rx2*ry+(0.25*rx2));
px=0;
py=trx2*y;
while(px<py)
{
x=x+1;
px=px+try2;
if(p>=0)

5
{
y=y-1;
py=py-trx2;
}
if(p<0)
p=p+ry2+px;
else
p=p+ry2+px-py;
plotpoints(xc,yc,x,y);
}
p=(int)(ry2*(x+0.5)*(x+0.5)+rx2*(y-1)*(y-1)-rx2*ry2);
while(y>0)
{
y=y-1;
py=py-trx2;
if(p<=0)
{
x=x+1;
px=px+try2;
}
if(p>0)
p=p+rx2-py;
else
p=p+rx2-py+px;
plotpoints(xc,yc,x,y);
}
getch();
closegraph();
}

EX NO: 2
Implementation Of Line,Circle & Ellipse Attributes

Aim:
To write a C Program to display the various attributes of line, circle and ellipse.

6
Functions Used:

Line()
line (x1,y1,x2,y2) :
Line function draws a line between two specified points (x,y) towards (x2,y2).This
function comes handy if you want to draw box like shapes or just plotting the graphs
etc.
e.g. line(100,50,100,400);
You can set the line style using setlinestyle functions. This function specifies the type
of line, pattern & the thickness that is going to appear on the screen. You have options
like solid,dotted,centered,dashed etc.
The other attributes with the command are as follows:
setlinestyle(style,0,1);SetLinetype(lt),setLinewidthScaleFactor(lw),sePolylinecolourIn
dex(lc)

Color palettes
The graphics.h has declaration about 16 colors.In order to use the color in your
program you have to use the functions like setcolor( ) ,setbkcolor( ) & setfillstyle(
).The function setcolor( ) sets the value of current drawing color to color.setfillstyle( )
sets the current fill pattern and fill color.setbkcolor( ) sets the value for background
color,which is by default black.
Below is the table that describes the value for each color that are declared in the
graphics.h file.
Color Value
Black 0
Blue 1
GREEN 2
Cyan 3
RED 4
MAGENTA 5
BROWN 6
LIGHTGRAY 7
DARKGRAY 8
LIGHTBLUE 9
LIGHTGREEN 10
LIGHTCYAN 11
LIGHTRED 12
LIGHTMAGENTA 13
YELLOW 14
WHITE 15
CIRCLE DRAWING
FUNCTIONS USED:
Circle()
The function circle() is used to draw a circle using(x,y) as centre point.
Syntax:
circle (x,y,radius)
The other attributes with the command are as follows:
Setfillstyle(style,lc),setcolor(lc)

7
ELLIPSE DRAWING
FUNCTIONS USED:
ellipse (x,y,stangle,endangle,xrad,yrad) :
This function draws an elliptical arc. Here (x,y) are the co-ordinates of center of the
ellipse. (stangle,endangle) are the starting and ending angles. If stangle=0 and
endangle=360 then this function draws complete ellipse.
eg.ellipse(100,150,0,360,100,50);
The other attributes with the command are as follows:
setfillcolor(lc),setfillstyle(ls),setlinecolor(lc),setlinewidth(lw)

ALGORITHM
Step 1: Start
Step 2: Draw the line. Set the attributes like style,type,width etc.
Step 3: Draw the circle. Set the attributes using commands.
Step 4: Draw the ellipse . Set the attributes like fillcolour,fillstyle,linecolour,linewidth
Step 5: stop the program

Program:
#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
void my_circle(int,int,int);
void plot(int,int,int,int);
void main()
{
int gd=DETECT,gm;
int xc,yc,r;
initgraph(&gd,&gm,"");
cout<<"enter X-Axis----->";
cin>>xc;
cout<<"enter Y-Axis----->";
cin>>yc;
cout<<"enter radius----->";
cin>>r;
my_circle(xc,yc,r);
getch();
closegraph();
}
void my_circle(int xc,int yc,int r)
{
int x,y,p;
p=3-2*r;
x=0;
y=r;
while(x<=y)
{

8
if(p<0)
p=p+(4*x+6);
else
{
p=p+10+4*(x-y);
y=y-1;
}
plot(xc,yc,x,y);
x=x+1;
}
}
void plot(int xc,int yc,int x,int y)
{
putpixel(xc+x,yc+y,15);
putpixel(xc+x,yc-y,15);
putpixel(xc-x,yc+y,15);
putpixel(xc-x,yc-y,15);
putpixel(xc+y,yc+x,15);
putpixel(xc+y,yc-x,15);
putpixel(xc-y,yc+x,15);
putpixel(xc-y,yc-x,15);
}

EX.NO: 3(a).
To perform 2D Transformations such as translation, rotation, scaling, Reflection & sharing.

2D Transformations Translation.

Aim:
To write a C program for implementing the two dimensional transformation such as
Translation.

Algorithm:
Step1:Start the program.
Step2:Input the no of vertices and their corresponding co-ordinates.

9
Step3:Display the input figure by using the draw() function.
Step4:Input the Transformation factor for both x and y , then also display the figure before
transformation.
Step5:By using these co-ordinates , we can change the position of the figure.
Step6:Clear the screen and then display the figure after the transformation.
Step7:Terminate the program.

Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int n,a[25],a1[25],i;
void draw(int *,int);
void main()
{
int gm,gd=DETECT;
int tpx,tpy;
initgraph(&gd,&gm,"c:/tc/bgi");
cleardevice();
printf("Enter the no of vertices\n");
scanf("%d",&n);
printf("Enter the co_ordinates\n");
for(i=0;i<n*2;i++)
scanf("%d",&a[i]);
outtextxy(50,50,"The input figure is");
draw(a,4);
clrscr();
cleardevice();
printf("Enter the transformation factor x and y\n");
scanf("%d%d",&tpx,&tpy);
for(i=0;i<n*2;i+=2)
a1[i]=a[i]+tpx;
for(i=1;i<n*2;i+=2)
a1[i]=a[i]+tpy;
cleardevice();
outtextxy(50,50,"Before transformation");
draw(a,2);
outtextxy(50,50,"After transformation");
draw(a1,4);
getch();
}
void draw(int *a,int col)
{
setfillstyle(BKSLASH_FILL,col);
fillpoly(n,a);
getch();
cleardevice();
}

10
EX.NO 3 (b)
2D Transformations Rotation.
Aim:
To write a C program for implementing the two dimensional transformation such as
Rotation.
Algorithm:
Step1:Start the program.
Step2:Declare the variables and initiate the initgraph() method.
Step3:Get the no of vertices and their corresponding co-ordinates.
Step4:Using them, display the input figure then get the rotation angle and fixed point of rotation.
Step5:Depending upon the fixed point, rotate the figure by changing the co-ordinates of the
vertex.
Step6:Display the given figure, before rotation and also after rotation.
Step7:Terminate the program.
Program:
#include<stdio.h>
#include<graphics.h>

11
#include<math.h>
int a[25],a1[25],i,n;
void draw(int *,int);
void main()
{
double angle;
float pi;
int px,py,t,j;
int gm,gd=DETECT;
initgraph(&gd,&gm,"c:/tc/bgi");
printf("\t\n**Rotation***");
printf("Enter the no of vertices:");
scanf("%d",&n);
printf("Enter the coordinates:");
for(i=0;i<n*2;i++)
scanf("%d",&a[i]);
outtextxy(50,50,"Input figure");
draw(a,4);
pi=3.14/180;
printf("Enter the rotation angle:");
scanf("%d",&t);
printf("Enter the rotation point:");
scanf("%d%d",&px,&py);
j=0;
angle=(double)(t*pi);
for(i=0;i<n*2;i+=2)
{
a1[j]=px+((a[i]-px)*cos(angle))-((a[i+1]-py)*sin(angle));
a1[++j]=py+((a[i]-px)*sin(angle))-((a[i+1]-py)*cos(angle));
j++;
}
cleardevice();
outtextxy(50,50,"Before Rotation:");
circle(px,py,2);
line(px,py,px,0);
line(px,py,getmaxx(),py);
line(px,py,px,getmaxy());
outtextxy(px,py,"axis");
draw(a,2);
outtextxy(300,50,"After Rotation");
circle(px,py,2);
line(px,py,0,py);
line(px,py,getmaxx(),py);
line(px,py,px,getmaxy());
outtextxy(px,py,"axis");
draw(a1,4);
}
void draw(int *a,int col)

12
{
setfillstyle(BKSLASH_FILL,col);
fillpoly(n,a);
getch();
cleardevice();
}

EX.NO:3.(c)
2D Transformations Scaling.
Aim:
To write a C program for implementing the two dimensional transformation such as
Scaling.
Algorithm:
Step1:Start the program.
Step2:Get the no of vertices and their corresponding co-ordinates of the vertices
Step3:Display the input figure by using the co-ordinates.
Step4:Cut the Scaling factor and multiply this with co-ordinates.
Step5:Display the Zooming figure.
Step6:Terminate the program.
Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int n,a[25],a1[25],i,k;
void draw(int *,int);
void main()
{

13
int gm,gd=DETECT;
int tpx,tpy;
initgraph(&gd,&gm,"c:/tc/bgi");
cleardevice();
printf("***SCALLING**8\n");
printf("Enter the no of vertices:\n");
scanf("%d",&n);
printf("Enter the coordinates:\n");
for(i=0;i<n*2;i++)
scanf("%d",&a[i]);
clrscr();
cleardevice();
outtextxy(50,50,"The input figure is");
draw(a,4);
clrscr();
cleardevice();
printf("Enter the value to zoom:\n");
scanf("%d",&k);
for(i=0;i<n*2;i+=2)
a1[i]=a[i]*k;
for(i=1;i<n*2;i+=2)
a1[i]=a[i]*k;
outtextxy(50,50,"after zooming");
gotoxy(10,10);
draw(a1,4);
}
void draw(int *a,
int col)
{
setfillstyle(SLASH_FILL,col);
fillpoly(n,a);
getch();
cleardevice();
}

14
EX: NO: 3.(D)
2D Transformations Shearing & Reflection.
Aim:
To write a C program for implementing the two dimensional transformation such as
Shearing & Reflection.
Algorithm:
Step1:Start the program.
Step2:Initialize the graphics drive and mode.
Step3:Read the co-ordinates of 2D object, which is transformed.
Step4:For shearing read the shearing factor and apply the shearing factor formula with respect to
x & y.
i)x = x + shx*y
y = y
ii)y=y + shy*x
x= x
Step5:Stop the program.
Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<string.h>
#include<stdlib.h>
void main()
{

15
int gd=DETECT,gm,midx,midy,midw,n;
int xs1,xs2,xs3,ys1,ys2,ys3,ch,x1,y1,x2,y2,x3,y3,abs,c,b,w,z,sh;
initgraph(&gd,&gm,"c:/tc/bgi");
printf("\nEnter the I co-ordinate***\n");
scanf("%d%d",&x1,&y1);
printf("\nEnter the II coordinate***\n");
scanf("%d%d",&x2,&y2);
midx=abs((x2-x1)/2);
midy=abs((y2-y1)/2);
cleardevice();
do
{
printf("1.Shearing\n2.Reflection\n3.Quit\n");
scanf("%d",&n);
switch(n)
{
case 1:
cleardevice();
line(x1,y1,x2,y2);
moveto(x2,y2);
lineto(x1+midx,y1+midy);
lineto(x1,y1);
printf("Enter the shearing factor\n");
scanf("%d",&sh);
printf("Enter the choice 1 with respect to x,2 with respect to y\n");
scanf("%d",&ch);
printf("%d",ch);
if(ch==1)
{
xs1=x1+sh*y1;
xs2=x2+sh*y2;
line(x1,y1,xs2,y2);
moveto(xs2,y2);
lineto(xs1+abs((xs2-xs1)/2),y1+abs((y2-y1)/2));
lineto(x1,y1);
}
else
{
ys1=y1+sh*x1;
ys2=y2+sh*x2;
line(x1,y1,x2,ys2);
moveto(x2,ys2);
lineto(x1+abs((x2-x1)/2),ys1+abs((ys2-ys1)/2));
lineto(x1,y1);
}
getch();
break;
case 2:

16
cleardevice();
line(x1,y1,x2,y2);
moveto(x2,y2);
lineto(x1+midx,y1+midy);
lineto(x1,y1);
if(y1>y2)
z=y1;
else
z=y2;
if(x1<x2)
w=x2;
else
w=x1;
line(0,z+10,w+50,z+10);
line(x1,z+20+(z-y1),x2,z+20+(z-y2));
moveto(x2,z+20+(z-y2));
lineto(x1+midx,z+20+(z-(y1+midy)));
lineto(x1,z+20+(z-y1));
getch();
break;
case 3:
exit(0);
break;
}}
while(n<=3);
getch();
}

17
EX.NO:4
Composite 2D Transformations.

AIM:
To write a C program to implement 2D transformations.

Algorithm
1. Enter the choice for transformation.
2. Perform the translation, rotation, scaling, reflection and shearing of 2D object.
3. Get the needed parameters for the transformation from the user.
4. Incase of rotation, object can be rotated about x or y axis.
5. Display the transmitted object in the screen.

Program
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
int ch,x,y,az,i,w,ch1,ch2,xa,ya,ra,a[10],b[10],da,db;
float x1,y1,az1,w1,dx,dy,theta,x1s,y1s,sx,sy,a1[10],b1[10];
void main()
{
int gm ,gr;
clrscr();
detectgraph(&gm,&gr);
initgraph(&gm,&gr,"d:\\tc\\BGI");
printf("Enter the upper left corner of the rectangle:\n");
scanf("%d%d",&x,&y);
printf("Enter the lower right corner of the rectangle:\n");
scanf("%d%d",&az,&w);
rectangle(x,y,az,w);

18
da=az-x;
db=w-y;
a[0]=x;
b[0]=y;
a[1]=x+da;
b[1]=y;
a[2]=x+da;
b[2]=y+db;
a[3]=x;b[3]=y+db;
while(1)
{
printf("******2D Transformations*******\n");
printf("1.Translation\n2.Rotation\n3.Scaling\n4.Reflection\n5.Shearing\n6.Exit\nEnter your
choice:\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
detectgraph(&gm,&gr);
initgraph(&gm,&gr,"d:\\tc\\BGI");
rectangle(x,y,az,w);
printf("*******Translation*******\n\n");
printf("Enter the value of shift vector:\n");
scanf("%f%f",&dx,&dy);
x1=x+dx;
y1=y+dy;
az1=az+dx;
w1=w+dy;
rectangle(x1,y1,az1,w1);
break;
case 2:
detectgraph(&gm,&gr);
initgraph(&gm,&gr,"d:\\tc\\BGI");
rectangle(x,y,az,w);
printf("*******Rotation*******\n\n");
printf("Enter the value of fixed point and angle of rotation:Enter the value of fixed point and
angle of rotation:\n");
scanf("%d%d%d",&xa,&ya,&ra);
theta=(float)(ra*(3.14/180));
for(i=0;i<4;i++)
{
a1[i]=(xa+((a[i]-xa)*cos(theta)-(b[i]-ya)*sin(theta)));
b1[i]=(ya+((a[i]-xa)*sin(theta)+(b[i]-ya)*cos(theta)));
}
for(i=0;i<4;i++)
{
if(i!=3)
line(a1[i],b1[i],a1[i+1],b1[i+1]);

19
else
line(a1[i],b1[i],a1[0],b1[0]);
}
break;
case 3:
detectgraph(&gm,&gr);
initgraph(&gm,&gr,"d:\\tc\\BGI");
rectangle(x,y,az,w);
printf("********Scaling*******\n\n");
printf("Enter the value of scaling factor:\n");
scanf("%f%f",&sx,&sy);
x1=x*sx;
y1=y*sy;
az1=az*sx;
w1=w*sy;
rectangle(x1,y1,az1,w1);
break;
case 4:
detectgraph(&gm,&gr);
initgraph(&gm,&gr,"d:\\tc\\BGI");
rectangle(x,y,az,w);
printf("*******Reflection*********\n");
printf("1.About x-axis\n2.About y-axis\n3.About both axis\nEnter your choice:\n");
scanf("%d",&ch1);
switch(ch1)
{
case 1:
printf("Enter the fixed point\n");
scanf("%d%d",&xa,&ya);
theta=(float)(90*(3.14/180));
for(i=0;i<4;i++)
{
a1[i]=(xa+((a[i]-xa)*cos(theta)-(-b[i]-ya)*sin(theta)));
b1[i]=(ya+((a[i]-xa)*sin(theta)+(-b[i]-ya)*cos(theta)));
}
for(i=0;i<4;i++)
{
if(i!=3)
line(a1[i],b1[i],a1[i+1],b1[i+1]);
else
line(a1[i],b1[i],a1[0],b1[0]);
}
break;
case 2:
printf("Enter the fixed point\n");
scanf("%d%d",&xa,&ya);
theta=(float)(270*(3.14/180));
for(i=0;i<4;i++)

20
{
a1[i]=(xa+((-a[i]-xa)*cos(theta)-(b[i]-ya)*sin(theta)));
b1[i]=(ya+((-a[i]-xa)*sin(theta)+(b[i]-ya)*cos(theta)));
}
for(i=0;i<4;i++)
{
if(i!=3)
line(a1[i],b1[i],a1[i+1],b1[i+1]);
else
line(a1[i],b1[i],a1[0],b1[0]);
}
break;
case 3:
printf("Enter the fixed point\n");
scanf("%d%d",&xa,&ya);
theta=(float)(180*(3.14/180));
for(i=0;i<4;i++)
{
a1[i]=(xa+((-a[i]-xa)*cos(theta)-(-b[i]-ya)*sin(theta)));
b1[i]=(ya+((-a[i]-xa)*sin(theta)+(-b[i]-ya)*cos(theta)));
}
for(i=0;i<4;i++)
{
if(i!=3)
line(a1[i],b1[i],a1[i+1],b1[i+1]);
else
line(a1[i],b1[i],a1[0],b1[0]);
}
break;
}
break;
case 5:
detectgraph(&gm,&gr);
initgraph(&gm,&gr,"d:\\tc\\BGI");
rectangle(x,y,az,w);
printf("*******Shearing******\n\n");
printf("1.x-direction shear\n2.y-direction shear\nEnter your choice:\n");
scanf("%d",&ch2);
switch(ch2)
{
case 1:
printf("Enter the value of shear:\n");
scanf("%f",&x1s);
x1=x+(y*x1s);
y1=y;
az1=az+(w*x1s);
w1=w;
rectangle(x1,y1,az1,w1);

21
break;
case 2:
printf("Enter the value of shear:\n");
scanf("%f",&y1s);
x1=x;
y1=y+(x*y1s);
az1=az;
w1=w+(az*y1s);
rectangle(x1,y1,az1,w1);
break;
}
break;
case 6:
exit(0);
}
}
getch();
}

22
EX.NO:5
Cohen Sutherland 2D line clipping and Windowing
Aim:
To write a C program to implement the Cohen-Sutherland 2D line clipping.

Algorithm:
Step1:Start the program.
Step2:Read two end points of the line say p1(x1,y1) and p2(x2,y2)
Step3:Read two corners (left-top and right-bottom)of the window , say (Wx1,Wy1 and
Wx2,Wy2)
Step4:Assign the region codes for two endpoints p1 and p2 using following steps:
Initialize code with bits 0000
Set Bit 1-if(x<Wx1)
Set Bit 2-if(x>Wx2)
Set Bit 3-if(y<Wy2)
Set Bit 4-if(y>Wy1)
Step5:Check for visibility of line p1p2
a)If region codes for both endpoints p1 and p2 are zero then the line is completely visible.
Hence draw the line and go to step10.
b)If region codes for endpoints are not zero and the logical ANDing of them is also nonzero
then the line is completely invisible, so reject the line and go to step10.
c)If region codes for two endpoints do not satisfy the conditions in step 4a & 4b the line is
partially visible.
Step6:Determine the intersecting edge of the clipping window by inspecting the region codes of
two endpoints.
a)If region codes for both the endpoints are non-zero, find intersection points p1 and p2
with boundary edges of clipping window with respect to point p1 and point p2,respectively.
b) If region codes for any one endpoints is non-zero, find intersection points p1 and p2
with boundary edge of clipping window with respect to it.
Step7:Divide the line segments considering intersection points.
Step8:Reject the line segments if any one end point of it appears outside the clipping window.
Step9:Draw the remaining line segments.
Step10:Stop the program.

Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<dos.h>
#include<math.h>
#include<graphics.h>

23
typedef struct coordinate
{
int x,y;
char code[4];
}pt;
void drawwindow();
void drawline(pt p1,pt p2,int c1);
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:/tc/bgi");
cleardevice();
printf("\n\n Enter the End-point1(x,y):");
scanf("%d%d",&p1.x,&p1.y);
printf("\n\n Enter the End-point2(x,y):");
scanf("%d%d",&p2.x,&p2.y);
cleardevice();
drawwindow();
getch();
drawline(p1,p2,15);
getch();
p1=setcode(p1);
p2=setcode(p2);
v=visibility(p1,p2);
switch(v)
{
case 0:
cleardevice();
drawwindow();
drawline(p1,p2,15);
break;
case 1:
cleardevice();
drawwindow();
break;
case 2:
cleardevice();
p1=resetendpt(p1,p2);
p2=resetendpt(p2,p1);
drawwindow();
drawline(p1,p2,15);
break;
}
getch();

24
closegraph();
return(0);
}
void drawwindow()
{
setcolor(RED);
line(150,100,450,100);
line(450,100,450,350);
line(450,350,150,350);
line(150,350,150,100);
}
void drawline(pt p1,pt p2,int c1)
{
setcolor(c1);
line(p1.x,p1.y,p2.x,p2.y);
}
pt setcode(pt p)
{
pt ptemp;
if(p.y<100)
ptemp.code[0]='1';
else
ptemp.code[0]='0';
if(p.y>350)
ptemp.code[1]='1';
else
ptemp.code[1]='0';
if(p.x>450)
ptemp.code[2]='1';
else
ptemp.code[2]='0';
if(p.x<150)
ptemp.code[3]='1';
else
ptemp.code[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((p1.code[i]!='\0')||(p2.code[i]!='0'))
flag=1;
}
if(flag==0)

25
return 1;
return 2;
}
pt resetendpt(pt p1,pt p2)
{
pt temp;
int x,y,i;
float m,k;
if(p1.code[3]=='1');
x=150;
if(p1.code[2]=='1')
x=450;
if((p1.code[3]=='1')||(p1.code[2]=='1'))
{
m=(float)(p2.y-p1.y)/(p2.x-p1.x);
k=(p1.y+(m*(x-p1.x)));
temp.y=k;
temp.x=x;
for(i=0;i<4;i++)
temp.code[i]=p1.code[i];
if(temp.y<=350 && temp.y>=100)
return(temp);
}
if(p1.code[0]=='1')
y=100;
if(p1.code[1]=='1')
y=350;
if((p1.code[0]=='1')||(p1.code[1]=='1'))
{
m=(float)(p2.y-p1.y)/(p2.x-p1.x);
k=(float)p1.x+(float)(y-p1.y)/m;
temp.x=k;
temp.y=y;
for(i=0;i<4;i++)
temp.code[i]=p1.code[i];
return(temp);
}
else return(p1);
}

EX.NO:6

26
Sutherland-Hodgeman Polygon Clipping
AIM:
To write a C program to implement polygon clipping.

Algorithm
1. Get the minimum and maximum coordinates of both window and view port.
2. Get the number of sides of a polygon and its corresponding coordinates.
3. If the polygon lies within region code window, display it.
4. If any one of the polygon side is neither inside nor outside the boundary, find point of
intersection and clip the regions that lies outside the boundary.
5. Display the polygon after clipping

Program
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
typedef enum { left,right,bottom,top } edge;
#define N_EDGE 4
#define TRUE 1
#define FALSE 0
struct point
{
int x;
int y;
}p,wmin,wmax,p1,p2,ipt,i,pin[50],pout[50],first[50],s[50],i;
int inside(struct point p,int b,struct point wmin,struct point wmax)
{
switch(b)
{
case left:
if(p.x<wmin.x)
return (FALSE);
break;
case right:
if(p.x>wmax.x)
return (FALSE);
break;
case bottom:
if(p.y<wmin.y)
return (FALSE);
break;
case top:
if(p.y>wmax.y)
return (FALSE);
break;
}
return (TRUE);
}

27
int cross(struct point p1,struct point p2,int b,struct point wmin,struct point wmax)
{
if(inside(p1,b,wmin,wmax)==inside(p2,b,wmin,wmax))
return (FALSE);
else
return (TRUE);
}
struct point intersect(struct point p1,struct point p2,int b,struct point wmin,struct point wmax)
{
float m;
if(p1.x!=p2.x)
m=(p1.y-p2.y)/(p1.x-p2.x);
switch(b)
{
case left:
ipt.x=wmin.x;
ipt.y=p2.y+(wmin.x-p2.x)*m;
break;
case right:
ipt.x=wmax.x;
ipt.y=p2.y+(wmax.x-p2.x)*m;
break;
case bottom:
ipt.y=wmin.y;
if(p1.x!=p2.x)
ipt.x=p2.x+(wmin.y-p2.y)/m;
else
ipt.x=p2.x;
break;
case top:
ipt.y=wmax.y;
if(p1.x!=p2.x)
ipt.x=p2.x+(wmax.y-p2.y)/m;
else
ipt.x=p2.x;
break;
}
return(ipt);
}
void clippoint(struct point p,int b,struct point wmin,struct point wmax,struct point *pout,int
*cnt,struct point *first[],struct point *s)
{
if(!first[b])
first[b]=&p;
else
if(cross(p,s[b],b,wmin,wmax))
{
ipt=intersect(p,s[b],b,wmin,wmax);

28
if(b<top)
clippoint(ipt,b+1,wmin,wmax,pout,cnt,first,s);
else
{
pout[*cnt]=ipt;
(*cnt)++;
}
}
s[b]=p;
if(inside(p,b,wmin,wmax))
if(b<top)
clippoint(p,b+1,wmin,wmax,pout,cnt,first,s);
else
{
pout[*cnt]=p;
(*cnt)++;
}
}
void closeclip(struct point wmin,struct point wmax,struct point *pout,int *cnt,struct point
*first[],struct point *s)
{
int b;
for(b=left;b<=top;b++)
{
if(cross(s[b],*first[b],b,wmin,wmax))
{
i=intersect(s[b],*first[b],b,wmin,wmax);
if(b<top)
clippoint(i,b+1,wmin,wmax,pout,cnt,first,s);
else
{
pout[*cnt]=i;
(*cnt)++;
}
}
}
}
int clippolygon(struct point wmin,struct point wmax,int n,struct point *pin,struct point *pout)
{
struct point *first[N_EDGE]={0,0,0,0},s[N_EDGE];
int i,cnt=0;
for(i=0;i<n;i++)
clippoint(pin[i],left,wmin,wmax,pout,&cnt,first,s);
closeclip(wmin,wmax,pout,&cnt,first,s);
return(cnt);
}
void main()
{

29
int c,gm,gr,n,j,np;
clrscr();
detectgraph(&gm,&gr);
initgraph(&gm,&gr,"d:\\tc\\BGI");
printf("Enter the window minimum coordinates");
scanf("%d%d",&wmin.x,&wmin.y);
printf("Enter the window max coordinates");
scanf("%d%d",&wmax.x,&wmax.y);
rectangle(wmin.x,wmax.y,wmax.x,wmin.y);
printf("Enter the no of sides in polygon:\n");
scanf("%d",&n);
printf("Enter the coordinates(x,y)for pin ,pout:\n");
for(j=0;j<n;j++)
{
scanf("%d%d",&pin[j].x,&pin[j].y);
scanf("%d%d",&pout[j].x,&pout[j].y);
}
detectgraph(&gm,&gr);
initgraph(&gm,&gr,"d:\\tc\\BGI");
for(j=0;j<n;j++)
{
if(j!=n-1)
line(pin[j].x,pin[j].y,pout[j].x,pout[j].y);
else
line(pin[j].x,pin[j].y,pout[j].x,pout[j].y);
}
rectangle(wmin.x,wmax.y,wmax.x,wmin.y);
printf("\n1.polygon clipping 2.exit");
scanf("%d",&c);
switch(c)
{
case 1:
detectgraph(&gm,&gr);
initgraph(&gm,&gr,"d:\\tc\\BGI");
rectangle(wmin.x,wmax.y,wmax.x,wmin.y);
np=clippolygon(wmin,wmax,n,pin,pout);
for(j=0;j<np;j++)
{
line(pout[j].x,pout[j].y,pout[(j+1)].x,pout[(j+1)].y);
}
break;
case 2:
exit(0);
}
getch();
}

30
EX.NO:7
Three dimensional transformations - Translation, Rotation, Scaling.
Aim:
To write a C program to implement three dimensional transformations such as Translation ,
Rotation and Scaling.

Algorithm:
Step1:Start the program.
Step2:Draw the 3D object and also display the choices.
Step3:Get the choice and perform the corresponding actions.
Step4:The choices is 1 then get the translation values and move the 3D-object.

31
Step5:The choices is 2 then get the scaling vectors and change the size of 3D-object.
Step6:The choices is 3 then get the rotation angle to rotate the 3D-object.
Step7:The choices is 4 then exit from the output window.
Step8:Terminate the program.

Program:
#include<stdio.h>
#include<graphics.h>
#include<dos.h>
#include<conio.h>
#include<math.h>
#include<process.h>
int points[50];
int points1[50];
int x=25;
int n=5;
void construct(int[]);
void construct1(int[],int[]);
void main()
{
int gd=DETECT;
int gm=DETECT;
char z;
initgraph(&gm,&gd,"C:/tc/bgi");
do
{
int points[50]={100,100,100,150,150,150,150,100,100,100};
int a;
cleardevice();
printf("\n\n\n\t\t\t\t1.Translation");
printf("\n\n\n\t\t\t\t2.Scaling");
printf("\n\n\n\t\t\t\t3.Rotation");
printf("\n\n\n\t\t\t\t4.Exit");
printf("\n\n\n\tEnter the choice:");
scanf("%d",&a);
switch(a)
{
case 1:
{
int tx,ty,i,point[50],point1[50];
construct(points);
printf("Enter the Translation Values:\n");
scanf("%d%d",&tx,&ty);
for(i=0;i<=2*n;i=i+2)
{
point[i]=points[i]+tx;
point1[i]=points1[i]+tx;
}

32
for(i=1;i<2*n;i=i+2)
{
point[i]=points[i]+ty;
point1[i]=points1[i]+ty;
}
construct1(point,point1);
getch();
break;
}
case 2:
{
int sx,sy,sz,point[50],point1[50],i;
construct(points);
printf("Enter the scaling values:\n");
scanf("%d%d%d",&sx,&sy,&sz);
point[0]=points[0];
point[1]=points[1];
point[2]=points[2];
point[3]=points[1]+sy*(points[3]-points[1]);
point[4]=points[2]+sx*(points[4]-points[2]);
point[5]=points[7]+sy*(points[5]-points[7]);
point[6]=points[0]+sx*(points[6]-points[0]);
point[7]=points[7];
point[8]=points[8];
point[9]=points[9];
for(i=0;i<2*n;i++)
{
point1[i]=point[i]+(x*sz);
}
construct1(point,point1);
getch();
break;
}
case 3:
{
int point[50],point1[50],i;
float an;
construct(points);
printf("Enter the angle:\n");
scanf("%f",&an);
an=(float)an*3.14/180;
for(i=0;i<=8;i+=2)
{
point[i]=points[0]+(points[i]-points[0])*cos(an)-(points[i+1]-points[1])*sin(an);
}
for(i=1;i<=9;i+=2)
{
point[i]=points[1]+(points[i]-points[1])*cos(an)-(points[i-1]-points[0])*sin(an);

33
}
for(i=0;i<=9;i+=2)
{
point1[i]=points1[0]+(points1[i]-points1[0])*cos(an)-(points1[i+1]-points1[1])*sin(an);
}
for(i=1;i<=9;i+=2)
{
point1[i]=points1[1]+(points1[i]-points1[1])*cos(an)-(points1[i-1]-points1[0])*sin(an);
}
construct1(point,point1);
getch();
break;
}
case 4:
{
exit(0);
break;
}
}
printf("Do you want to continue say(Y/N)?:");
scanf("%s",&z);
}while(z=='Y');
getch();
closegraph();
}
void construct(int points[50])
{
int x=25,n=5,i;
cleardevice();
for(i=0;i<2*n;i++)
{
points1[i]=points[i]+x;
}
construct1(points,points1);
}
void construct1(int x[50],int y[50])
{
drawpoly(n,x);

drawpoly(n,y){
line(x[0],x[1],y[0],y[1]);
line(x[2],x[3],y[2],y[3]);
line(x[4],x[5],y[4],y[5]);
line(x[6],x[7],y[6],y[7]);
}

34
EX.NO:8
Composite 3D transformations.

AIM
To write a C program to implement 3D transformations. 3 Dimensional Transformations

ALGORITHM
1. Enter the choice for transformation.
2. Perform the translation, rotation, scaling of 3D object.
3. Get the needed parameters for the transformation from the user.
4. Increase of rotation, object can be rotated about x or y or z axis.
5. Display the transmitted object in the screen

Source Code
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int maxx,maxy,midx,midy;

35
void axis()
{
getch();
cleardevice();
line(midx,0,midx,maxy);
line(0,midy,maxx,midy);
}
void main()
{
int gd,gm,x,y,z,o,x1,x2,y1,y2;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"d:\\tc\\bgi");
setfillstyle(0,getmaxcolor());
maxx=getmaxx();
maxy=getmaxy();
midx=maxx/2;
midy=maxy/2;
axis();
bar3d(midx+100,midy-150,midx+60,midy-100,10,1);
printf("\Enter the translation factor");
scanf("%d%d",&x,&y);
axis();
printf("After translation");
bar3d(midx+100,midy-150,midx+60,midy-100,10,1);
bar3d(midx+x+100,midy-(y+150),midx+x+60,midy-(y+100),10,1);
axis();
bar3d(midx+100,midy-150,midx+60,midy-100,10,1);
printf("Enter the scaling factor");
scanf("%d%d%d",&x,&y,&z);
axis();
printf("After scaling");
bar3d(midx+100,midy-150,midx+60,midy-100,10,1);
bar3d(midx+(x*100),midy-(y*150),midx+(x*60),midy-(y*100),10*z,1);
axis();
bar3d(midx+100,midy-150,midx+60,midy-100,10,1);
printf("Enter the rotation angle");
scanf("%d",&o);
x1=50*cos(o*3.14/180)-100*sin(o*3.14/180);
y1=50*sin(o*3.14/180)+100*cos(o*3.14/180);
x2=60*cos(o*3.14/180)-90*sin(o*3.14/180);
y2=60*sin(o*3.14/180)+90*cos(o*3.14/180);
axis();
printf("After rotating about Z-axis");
bar3d(midx+100,midy-150,midx+60,midy-100,10,1);
bar3d(midx+x1,midy-y1,midx+x2,midy-y2,10,1);
axis(); printf("After rotating about x-axis");
bar3d(midx+100,midy-150,midx+60,midy-100,10,1);
bar3d(midx+100,midy-x1,midx+60,midy-x2,10,1);

36
axis(); printf("After rotating about Y-axis");
bar3d(midx+100,midy-150,midx+60,midy-100,10,1);
bar3d(midx+x1,midy-150,midx+x2,midy-100,10,1);
getch(); closegraph();
}

Ex NO 9
Drawing three dimensional objects and Scenes
AIM:
To draw 3D objects and scenes

ALGORITHM:
1. Parallel Projection
a) Enter the boundary coordinates of the object.
b) Define the direction of the projection line.
c) Align the projection plane so that it intersects each coordinate axis in
which the object is defined at the same distance from the origin.
d) The projection is perpendicular to the view plane
e) If the view plane is placed at position Z1 along the z axis , then any
point (x,y,z) in viewing coordinates is transformed to projection
coordinates xp=x and yp=y.
2.Perspective Projection
a) Enter the boundary coordinates of the object.
b) Define the projection reference point and thus the direction of the
projection lines.
c) The projected view is obtained by transforming points along projection
lines that meet at the projection reference point.
d) Calculate the intersection of the projection lines with the view plane.
e) Parallel lines in the object that are not parallel to the plane are

37
projected into converging lines.
f) Parallel lines that are parallel to the view plane will be projected as
parallel lines.

EX NO 10
Generating Fractal images

AIM:
To generate fractal images.

ALGORITHM
The sierpinski triangle-A fractal image
1.The sierpinski Triangle is created by infinite removals
2.Each triangle is divided into 4 smaller upside down triangles
3.The center of the 4 triangles is removed
4. As the process is iterated infinite number of times, the total area of the
set goes to infinity as the size of the each new triangle goes to zero
5.After closer examination magnification factor is 2.
6.With each magnification there are 3 divisions of a triangle
Dimension D=ln(3)/ln(2)
D=1.5850

38
Ex. No: 11 APPLICATION PROGRAMS

(a) Implementation of DDA algorithm

#include <graphics.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>

void main()
{

int gd=DETECT,gm=DETECT,s,dx,dy,m,x1,y1,x2,y2;
float xi,yi,x,y;

clrscr();

printf("Enter the sarting point x1 & y1n");


scanf("%d%d",&x1,&y1);

printf("Enter the end point x2 & y2n");


scanf("%d%d",&x2,&y2);

initgraph(&gd,&gm,"");
cleardevice();

dx=x2-x1;

39
dy=y2-y1;

if(abs(dx)>abs(dy))
s=abs(dx);
else
s=abs(dy);

xi=dx/(float)s;
yi=dy/(float)s;

x=x1;
y=y1;

putpixel(x1,y1,4);

for(m=0; m < s; m++)


{
x+=xi;
y+=yi;
putpixel(x,y,4);
}
getch();
}
(b) Implementation of Boundary Filling algorithm

#include<conio.h>

#include<stdio.h>

#include<graphics.h>7

#include<dos.h>

#include<math.h>

void boundaryfill(int x,int y,int fcol,int bcol)

int col;

col=getpixel(x,y);

if(col!=bcol&&col!=fcol)

putpixel(x,y,fcol);

delay(10);

40
boundaryfill(x+1,y-1,fcol,bcol);

boundaryfill(x-1,y-1,fcol,bcol);

boundaryfill(x+1,y+1,fcol,bcol);

boundaryfill(x-1,y+1,fcol,bcol);

void main()

int gd=DETECT,gm;

int sx,sy,bcol,fcol;

initgraph(&gd,&gm,"d:\\turboc\\bgi");

printf("Press any key to start filling");

rectangle(200,200,300,300);

rectangle(150,150,250,250);

rectangle(270,210,350,310);

rectangle(150,270,260,350);

rectangle(260,260,265,265);

getch();

boundaryfill(255,255,12,15);

getch();

(c)Design a Bar Chart using any algorithm

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<dos.h>

41
#include<math.h>

void main()

int gd=DETECT,gm;

int x,y,maxx,maxy,i,n,gap,sty,a[10];

char str[10];

int te,inter;

initgraph(&gd,&gm,"d:\\tc\\bgi");

cleardevice();

x=getmaxx()-30;

y=getmaxy()-30;

printf("\n ENTER THE TOTAL NUMBER OF DATA (integer): ");

scanf("%d",&n);

for(i=0;i<n;i++)

printf("\n ENTER THE VALUE OF %d NUMBER (int): ",i);

scanf("%d",&a[i]);

cleardevice();

inter=0;

for(i=y;i>=30;i++)

sprintf(str,"%d",inter);

outtextxy(5,i,str);

inter+=50;

42
gap=50;

cleardevice();

setcolor(RED);

line(30,30,30,y);

line(30,y,x,y);

setcolor(WHITE);

for(i=0;i<n;i++)

sty=y-a[i];

sprintf(str,"%d",a[i]);

outtextxy(gap,sty-10,str);

line(gap,sty,gap+20,sty);

line(gap,sty,gap+20,y);

line(gap+20,sty,gap+20,y);

floodfill(gap+10,sty+5,15);

setfillstyle(i+1,i+1);

bar3d(gap,sty,gap+20,y,5,13);

gap+=50; }

getch();}

(d) Design an algorithm for Rotating Circle within a Circle

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

#include<graphics.h>

#include<math.h>

#include<dos.h>

43
#define RAD 0.01745329251994

#define round(x) (int) ( (x) + 0.5)

int _rx,_ry;

int rx,ry;

void Rotate(int x,int y,int rx,int ry,int ang)

_rx=rx+(x-rx)*cos(ang*RAD)-(y-ry)*sin(ang*RAD);

_ry=ry+(x-rx)*sin(ang*RAD)+(y-ry)*cos(ang*RAD);

void main()

int gd=DETECT,gm;

int x,y,mx,my;

int rad;

int irad;

int brad;

int i,j,ang,iang;

int xp[6],yp[6];

initgraph(&gd,&gm,"d:\\turboc\\bgi");

mx=320;

my=240;

rad=100;

irad=30;

44
brad=irad+(rad-irad)/2;

ang=iang=360/6;

for(i=0;i<6;i++)

xp[i]=mx+brad*cos(ang*RAD);

yp[i]=my-brad*sin(ang*RAD);

ang+=iang;

ang=0;

while(!kbhit())

ang+=1;

if(ang==360) ang=0;

setcolor(15);

circle(mx,my,rad);

circle(mx,my,irad);

for(i=0;i<6;i++)

Rotate(xp[i],yp[i],mx,my,ang);

setcolor(9+i);

circle(round(_rx), round(_ry), brad/2-5);

delay(20);

cleardevice();

45
getch();

closegraph();

46

You might also like