You are on page 1of 52

Experiment No:1

Title- Line Drawing Algorithms


Objective- To study of basic graphics functions defined in “graphics.h”.
Algorithm
Input to the function is two endpoints (x1,y1) and (x2,y2)
1. Start.
2. Declare variables x,y,x1,y1,x2,y2,k,dx,dy,steps,xincrement,yincrement and also declare
gdriver=DETECT, gmode.
3. Initialize the graphic mode with the path location in TC folder.
4. Input the two line end-points and store the left/initial end-points in (x1,y1).
5. Plot the first point. put x=x1,y=y1.
6. Calculate dx=x2-x1 and dy=y2-y1.
7. If abs(dx) > abs(dy), do steps=abs(dx). else steps= abs(dy).
8. Then xincrement=dx/steps and yincrement=dy/steps.
9. Start from k=0 and continuing till k
Code-
#include <graphics.h>
#include <iostream.h>
#include <math.h>
#include <dos.h>
 
void main( )
{
float x,y,x1,y1,x2,y2,dx,dy,step;
int i,gd=DETECT,gm;
 
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
 
cout<<"Enter the value of x1 and y1 : ";
cin>>x1>>y1;
cout<<"Enter the value of x2 and y2: ";
cin>>x2>>y2;
 
dx=abs(x2-x1);
dy=abs(y2-y1);
 
if(dx>=dy)
step=dx;
else
step=dy;
 
dx=dx/step;
dy=dy/step;
 
x=x1;
y=y1;
 
i=1;
while(i<=step)
{
putpixel(x,y,5);
x=x+dx;
y=y+dy;
i=i+1;
delay(100);
}
 
closegraph();
}
Output-
Experiment No- 2
Title- Line Drawing Algorithms
Objective- To study and Implement Bresenham 's Algorithm

Algorithm
Bresenham's Line Drawing Algorithm:
1. Start.
2. Declare variables x,y,x1,y1,x2,y2,p,dx,dy and also declare gdriver=DETECT,gmode.
3. Initialize the graphic mode with the path location in TC folder.
4. Input the two line end-points and store the left end-points in (x1,y1).
5. Load (x1,y1) into the frame buffer; that is, plot the first point put x=x1,y=y1.
6. Calculate dx=x2-x1 and dy=y2-y1,and obtain the initial value of decision parameter p as:
p=(2dy-dx).
7. Starting from first point (x,y) perform the following test:
8. Repeat step 9 while(x<=x2).

Code-
#include<iostream.h>
#include<graphics.h>
 
void drawline(int x0, int y0, int x1, int y1)
{
    int dx, dy, p, x, y;
 
dx=x1-x0;
dy=y1-y0;
 
x=x0;
y=y0;
 
p=2*dy-dx;
 
while(x<x1)
{
if(p>=0)
{
putpixel(x,y,7);
y=y+1;
p=p+2*dy-2*dx;
}
else
{
putpixel(x,y,7);
p=p+2*dy;
}
x=x+1;
}
}
 
int main()
{
int gdriver=DETECT, gmode, error, x0, y0, x1, y1;
initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");
 
cout<<"Enter co-ordinates of first point: ";
cin>>x0>>y0;
 
cout<<"Enter co-ordinates of second point: ";
cin>>x1>>y1;
drawline(x0, y0, x1, y1);
 
return 0;
}

Output-
Experiment No:3
Title-Circle Algorithm
Objective- To implement Bresenham’s algorithms for Circle drawing.
Algorithm
1. Set initial values of (xc, yc) and (x, y)
2. Set decision parameter d to d = 3 – (2 * r).
3. call drawCircle(int xc, int yc, int x, int y) function.
4. Repeat steps 5 to 8 until x < = y
5. Increment value of x.
6. If d < 0, set d = d + (4*x) + 6
7. Else, set d = d + 4 * (x – y) + 10 and decrement y by 1.
8. call drawCircle(int xc, int yc, int x, int y) function
Code-
#include<iostream.h>
#include<graphics.h>
 
void drawcircle(int x0, int y0, int radius)
{
    int x = radius;
    int y = 0;
    int err = 0;
 
    while (x >= y)
    {
putpixel(x0 + x, y0 + y, 7);
putpixel(x0 + y, y0 + x, 7);
putpixel(x0 - y, y0 + x, 7);
putpixel(x0 - x, y0 + y, 7);
putpixel(x0 - x, y0 - y, 7);
putpixel(x0 - y, y0 - x, 7);
putpixel(x0 + y, y0 - x, 7);
putpixel(x0 + x, y0 - y, 7);
 
if (err <= 0)
{
    y += 1;
    err += 2*y + 1;
}
 
if (err > 0)
{
    x -= 1;
    err -= 2*x + 1;
}
    }
}
 
int main()
{
int gdriver=DETECT, gmode, error, x, y, r;
initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");
 
cout<<"Enter radius of circle: ";
cin>>r;
 
cout<<"Enter co-ordinates of center(x and y): ";
cin>>x>>y;
drawcircle(x, y, r);
 
return 0;
}

Output-
Experiment No:4
Title- Circle Drawing Algorithms
Objective-To study and Implement Midpoint Circle Algorithm
Algorithms
1. Start.
2. Declare variables x, y, p and also declare gdriver= DETECT, gmode.
3. Initialize the graphic mode with the path location in TC folder.
4. Input the radius r and center xc , yc of the circle .
5. Load x=0, y=r, initial decision parameter p=1-r.so the plot first point is (x, y).
6. Repeat Step 7 to 9 while (x≤y).
7. If (p>0), then plot(x=x+1, y=y-1) do p=p+2*(x-y)+1. Else plot(x=x+1, y) p=p+2*x+1.
8. Determine symmetry in seven octants.
9. Place pixels using put pixel at points (x+ xc , y+ yc) in specified colour by shifting the origin to
center (xc , yc) on both x-axis and y-axis.
10. Close Graph.
11. Stop
Code-
#include<dos.h>
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void draw_circle(int,int,int);
void symmetry(int,int,int,int);
void main()
{
int xc,yc,R;
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
printf("Enter the center of the circle:\n");
printf("Xc =");
scanf("%d",&xc);
printf("Yc =");
scanf("%d",&yc);
printf("Enter the radius of the circle :");
scanf("%d",&R);
draw_circle(xc,yc,R);
getch();
closegraph();
}
void draw_circle(int xc,int yc,int rad)
{
int x = 0;
int y = rad;
int p = 1-rad;
symmetry(x,y,xc,yc);
for(x=0;y>x;x++)
{
if(p<0)
p += 2*x + 3;
else
{
p += 2*(x-y) + 5;
y--;
}
symmetry(x,y,xc,yc);
delay(0);
}
}
void symmetry(int x,int y,int xc,int yc)
{
putpixel(xc+x,yc-y,14);
delay(10);
putpixel(xc+y,yc-x, 1);//For pixel (y,x)
delay(10);
putpixel(xc+y,yc+x, 2);
delay(10);
putpixel(xc+x,yc+y,3);
delay(10);
putpixel(xc-x,yc+y,4);
delay(10);
putpixel(xc-y,yc+x, 5);
delay(10);
putpixel(xc-y,yc-x, 6);
delay(10);
putpixel(xc-x,yc-y, 8);
delay(10);
}
Output-
Experiment No:5
Title- Mid Point Ellipse Algorithm
Objective- To study and Implement Midpoint Ellipse Algorithm

Algorithm

1. Include the graphics header file and obtain graphics mode and driver.

2. Input rx,ry and ellipse center(xc,yc) and obtain the first point on an ellipse centered at origin
as (x0,y0)=(0,ry).

3. Calculate the initial value of the decision parameter in region 1 as p10= ry2- rx2 ry+(1/4)rx2.

4. At each xk position in region 1,starting at k=0,perform the following text: a)If p1k0, the next
point along the ellipse centered on (0,0) is (xk,yk-1) and p2 k+1=p2 k-2rx2 y k+1+ rx2
b)Otherwise, the next point along the ellipse is (xk+1,yk-1) and p2 k+1=p2 k+2 ry2 xk+1-2
rx2yk+1+ rx2 using the same incremental calculations for x and y as in region 1.

7. Repeat the steps for region 1 until 2 ry2>=2 rx2y.

8. Plot the pixel to display the ellipse using put pixel function. Midpoint Ellipse Drawing
algorithm.

Code-

#include<graphics.h>

#include<stdlib.h>

#include<iostream.h>

#include<conio.h>

void main()

clrscr();

int gd = DETECT, gm;

int xc,yc,x,y;float p;
long rx,ry;

initgraph(&gd, &gm, "C:\\TC\\BGI");

cout<<"Enter coordinates of centre : ";

cin>>xc>>yc;

cout<<"Enter x,y radius of ellipse: ";

cin>>rx>>ry;

//Region 1

p=ry*ry-rx*rx*ry+rx*rx/4;

x=0;y=ry;

while(2.0*ry*ry*x <= 2.0*rx*rx*y)

if(p < 0)

x++;

p = p+2*ry*ry*x+ry*ry;

else

x++;y--;

p = p+2*ry*ry*x-2*rx*rx*y-ry*ry;

}
putpixel(xc+x,yc+y,RED);

putpixel(xc+x,yc-y,RED);

putpixel(xc-x,yc+y,RED);

putpixel(xc-x,yc-y,RED);

//Region 2

p=ry*ry*(x+0.5)*(x+0.5)+rx*rx*(y-1)*(y-1)-rx*rx*ry*ry;

while(y > 0)

if(p <= 0)

x++;y--;

p = p+2*ry*ry*x-2*rx*rx*y+rx*rx;

else

y--;

p = p-2*rx*rx*y+rx*rx;

putpixel(xc+x,yc+y,RED);

putpixel(xc+x,yc-y,RED);
putpixel(xc-x,yc+y,RED);

putpixel(xc-x,yc-y,RED);

getch();

closegraph();

Output-
Experiment No: 6,7
Title- 2D Transformations
Objective- To study and Implement 2D Transformations such as translation, rotation, scaling,
reflection and sharing. Transformations are used to position objects, to shape objects, to
change viewing positions, and even to change how something is viewed without deformation.
Algorithm
1. Start
2. Draw the image with default parameters.
3. Get the choice from the user.
4. Get the parameters for transformation.
5. Perform the transformation.
6. Draw the image.
7. Stop
Code-

#include<graphics.h>

#include<conio.h>

#include<iostream.h>

#include<math.h>

#include<stdlib.h>

void main()

clrscr();

int tx,ty,c,t,i,j,k,sx,sy, x1,x2,y1,y2;

float tx1;

int gd = DETECT, gm;


initgraph(&gd, &gm,"C:\\TC\\BGI");

x1 = y2 = 100;

x2 = y1 = 150;

rectangle(x1,y1,x2,y2);

cout<<"Enter Choice:\n1 :Translation \n2 :Rotation \n3 :Scaling";

cout<<"\nChoice :";

cin>>c;

switch(c)

case 1:

cout<<"Enter tx & ty :";

cin>>tx>>ty;

rectangle(x1+tx, y1+ty, x2+tx, y2+ty);

break;

case 2 :

int xx1, yy1, xx2, yy2, xx3, yy3, xx4, yy4;

int ax1, ay1, ax2, ay2, ax3, ay3, ax4, ay4;

int refx, refy;

cout<<"Enter angle :";


cin>>tx1;

tx1=tx1*(3.14/180);

refx = refy = 100;

xx1 = yy1 = yy2 = xx4 = 100;

xx2 = xx3 = yy3 = yy4 = 150;

ax1 = refy +(xx1-refx)* cos(tx1)-(yy1-refy)*sin(tx1);

ay1 = refy +(xx1-refx)* sin(tx1)+(yy1-refy)*cos(tx1);

ax2 = refy +(xx2-refx)* cos(tx1)-(yy2-refy)*sin(tx1);

ay2 = refy +(xx2-refx)* sin(tx1)+(yy2-refy)*cos(tx1);

ax3 = refy +(xx3-refx)* cos(tx1)-(yy3-refy)*sin(tx1);

ay3 = refy +(xx3-refx)* sin(tx1)+(yy3-refy)*cos(tx1);

ax4 = refy +(xx4-refx)* cos(tx1)-(yy4-refy)*sin(tx1);

ay4 = refy +(xx4-refx)* sin(tx1)+(yy4-refy)*cos(tx1);

line(ax1,ay1, ax2, ay2);

line(ax2,ay2, ax3, ay3);


line(ax3,ay3, ax4, ay4);

line(ax4,ay4, ax1, ay1);

break;

case 3 :

cout<<"Enter sx & sy :";

cin>>sx>>sy;

rectangle(x1*sx, y1*sy, x2*sx, y2*sy);

break;

default :

cout<<"Not a valid choice";

getch();

closegraph();

Output-
Experiment No: 8
Title-Line Clipping(2 D Clipping)
Objective- To implement Cohen-Sutherland 2D Line clipping This method speeds up the
processing of line segments by performing initial tests that reduce the number of intersections
that must be calculated. Every line endpoint is assigned to four digit, binary code called‖ region
code‖. Region code identifies the location of the pint relative to the boundaries of the clipping
rectangle
Algorithm
1. Read 2 end points of line as p1(x1,y1) and p2(x2,y2)
2. Read 2 corner points of the clipping window (left-top and right-bottom) as
(wx1,wy1) and (wx2,wy2)
3. Assign the region codes for 2 endpoints p1 and p2 using following steps:-
initialize code with 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

4. Check for visibility of line


a. If region codes for both endpoints are zero then line is completely
visible. Draw the line go to step 9.
b. If region codes for endpoints are not zero and logical ANDing of them
is also nonzero then line is invisible. Discard the line and move to step
9.
c. If it does not satisfy 4.a and 4.b then line is partially visible.
5. Determine the intersecting edge of clipping window as follows:-
a. If region codes for both endpoints are nonzero find intersection points
p1’ and p2’ with boundary edges.
b. If region codes for any one end point is non zero then find intersection
point p1’ or p2’.
6. Divide the line segments considering intersection points.
7. Reject line segment if any end point of line appears outside of any
boundary.
8. Draw the clipped line segment.
9. Stop.

Code-
#include<iostream.h>
#include<stdlib.h>
#include<math.h>
#include<graphics.h>
#include<dos.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);
 
void main()
{
int gd=DETECT,v,gm;
PT p1,p2,p3,p4,ptemp;

cout<<"\nEnter x1 and y1\n";


cin>>p1.x>>p1.y;
cout<<"\nEnter x2 and y2\n";
cin>>p2.x>>p2.y;

initgraph(&gd,&gm,"c:\\turboc3\\bgi");
drawwindow();
delay(500);

drawline(p1,p2);
delay(500);
cleardevice();

delay(500);
p1=setcode(p1);
p2=setcode(p2);
v=visibility(p1,p2);
delay(500);

switch(v)
{
case 0: drawwindow();
delay(500);
drawline(p1,p2);
break;
case 1: drawwindow();
delay(500);
break;
case 2: p3=resetendpt(p1,p2);
p4=resetendpt(p2,p1);
drawwindow();
delay(500);
drawline(p3,p4);
break;
}

delay(5000);
closegraph();
}
 
void drawwindow()
{
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)
{
line(p1.x,p1.y,p2.x,p2.y);
}
 
PT setcode(PT p) //for setting the 4 bit code
{
PT ptemp;

if(p.y<100)
ptemp.code[0]='1'; //Top
else
ptemp.code[0]='0';

if(p.y>350)
ptemp.code[1]='1'; //Bottom
else
ptemp.code[1]='0';

if(p.x>450)
ptemp.code[2]='1'; //Right
else
ptemp.code[2]='0';
if(p.x<150)
ptemp.code[3]='1'; //Left
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)
return(0);

for(i=0;i<4;i++)
{
if((p1.code[i]==p2.code[i]) && (p1.code[i]=='1'))
flag='0';
}

if(flag==0)
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);
}

Output
Before Clipping

After Clipping
Experiment No: 9
Title- Line Clipping(2 D Clipping)
Objective- To implement Liang-Barsky 2D Line clipping
Algorithm

1. Read 2 endpoints of line as p1 (x1, y1) & p2 (x2, y2).


2. Read 2 corners (left-top & right-bottom) of the clipping window as
(xwmin, ywmin, xwmax, ywmax).
3. Calculate values of parameters pi and qi for i = 1, 2, 3, 4 such that

p1 = -dx, q1 = x1 – xwmin

p2 = dx, q2 = xwmax – x1

p3 = -dy, q3 = y1 – ywmin

p4 = dy, q4 = ywmax – y1

4. if pi = 0 then line is parallel to ith boundary

if qi < 0 then line is completely outside boundary so discard line

else, check whether line is horizontal or vertical and then check the line
endpoints with the corresponding boundaries.

5. Initialize t1 & t2 as

t1 = 0 & t2 = 1

6. Calculate values for qi/pi for i = 1, 2, 3, 4.


7. Select values of qi/pi where pi < 0 and assign maximum out of them as
t1.
8. Select values of qi/pi where pi > 0 and assign minimum out of them as
t2.
9. if (t1 < t2)
{
xx1 = x1 + t1dx

xx2 = x1 + t2dx

yy1 = y1 + t1dy
yy2 = y1 + t2dy

line (xx1, yy1, xx2, yy2)


}

10. Stop.

Code-
#include<iostream.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
 
void main()
{
int i,gd=DETECT,gm;
int x1,y1,x2,y2,xmin,xmax,ymin,ymax,xx1,xx2,yy1,yy2,dx,dy;
float t1,t2,p[4],q[4],temp;

x1=120;
y1=120;
x2=300;
y2=300;

xmin=100;
ymin=100;
xmax=250;
ymax=250;

initgraph(&gd,&gm,"c:\\turboc3\\bgi");
rectangle(xmin,ymin,xmax,ymax);
dx=x2-x1;
dy=y2-y1;

p[0]=-dx;
p[1]=dx;
p[2]=-dy;
p[3]=dy;

q[0]=x1-xmin;
q[1]=xmax-x1;
q[2]=y1-ymin;
q[3]=ymax-y1;

for(i=0;i<4;i++)
{
if(p[i]==0)
{
cout<<"line is parallel to one of the clipping boundary";
if(q[i]>=0)
{
if(i<2)
{
if(y1<ymin)
{
y1=ymin;
}

if(y2>ymax)
{
y2=ymax;
}

line(x1,y1,x2,y2);
}

if(i>1)
{
if(x1<xmin)
{
x1=xmin;
}

if(x2>xmax)
{
x2=xmax;
}

line(x1,y1,x2,y2);
}
}
}
}

t1=0;
t2=1;

for(i=0;i<4;i++)
{
temp=q[i]/p[i];

if(p[i]<0)
{
if(t1<=temp)
t1=temp;
}
else
{
if(t2>temp)
t2=temp;
}
}

if(t1<t2)
{
xx1 = x1 + t1 * p[1];
xx2 = x1 + t2 * p[1];
yy1 = y1 + t1 * p[3];
yy2 = y1 + t2 * p[3];
line(xx1,yy1,xx2,yy2);
}

delay(5000);
closegraph();
}

Output-

Experiment No: 10
Title Polygon-
Clipping
Objective- Pre-
requisite To study
and Implement
Polygon Clipping
Algorithm using
Sutherland Hodgman
Algorithm
Algorithm
1. Read coordinates of all vertices of the Polygon.
2. Read coordinates of the dipping window
3. Consider the left edge of the window
4. Compare the vertices of each edge of the polygon, individually with the clipping plane.
5. Save the resulting intersections and vertices in the new list of vertices according to four
possible relationships between the edge and the clipping boundary.
6. Repeat the steps 4 and 5 for remaining edges or the clipping window. Each time the
resultant list of vertices is successively passed to process the next edge of the clipping
window.
7. Stop

Code-

#include<iostream.h>

#include<conio.h>

#include<graphics.h>

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

int k;

float xmin,ymin,xmax,ymax,arr[20],m;

void clipl(float x1,float y1,float x2,float y2)

if(x2-x1)

m=(y2-y1)/(x2-x1);

else

m=100000;

if(x1 >= xmin && x2 >= xmin)

arr[k]=x2;
arr[k+1]=y2;

k+=2;

if(x1 < xmin && x2 >= xmin)

arr[k]=xmin;

arr[k+1]=y1+m*(xmin-x1);

arr[k+2]=x2;

arr[k+3]=y2;

k+=4;

if(x1 >= xmin && x2 < xmin)

arr[k]=xmin;

arr[k+1]=y1+m*(xmin-x1);

k+=2;

void clipt(float x1,float y1,float x2,float y2)

if(y2-y1)
m=(x2-x1)/(y2-y1);

else

m=100000;

if(y1 <= ymax && y2 <= ymax)

arr[k]=x2;

arr[k+1]=y2;

k+=2;

if(y1 > ymax && y2 <= ymax)

arr[k]=x1+m*(ymax-y1);

arr[k+1]=ymax;

arr[k+2]=x2;

arr[k+3]=y2;

k+=4;

if(y1 <= ymax && y2 > ymax)

arr[k]=x1+m*(ymax-y1);

arr[k+1]=ymax;

k+=2;
}

void clipr(float x1,float y1,float x2,float y2)

if(x2-x1)

m=(y2-y1)/(x2-x1);

else

m=100000;

if(x1 <= xmax && x2 <= xmax)

arr[k]=x2;

arr[k+1]=y2;

k+=2;

if(x1 > xmax && x2 <= xmax)

arr[k]=xmax;

arr[k+1]=y1+m*(xmax-x1);

arr[k+2]=x2;

arr[k+3]=y2;

k+=4;
}

if(x1 <= xmax && x2 > xmax)

arr[k]=xmax;

arr[k+1]=y1+m*(xmax-x1);

k+=2;

void clipb(float x1,float y1,float x2,float y2)

if(y2-y1)

m=(x2-x1)/(y2-y1);

else

m=100000;

if(y1 >= ymin && y2 >= ymin)

arr[k]=x2;

arr[k+1]=y2;

k+=2;

if(y1 < ymin && y2 >= ymin)


{

arr[k]=x1+m*(ymin-y1);

arr[k+1]=ymin;

arr[k+2]=x2;

arr[k+3]=y2;

k+=4;

if(y1 >= ymin && y2 < ymin)

arr[k]=x1+m*(ymin-y1);

arr[k+1]=ymin;

k+=2;

void main()

int gdriver=DETECT,gmode,n,poly[20];

float xi,yi,xf,yf,polyy[20];

clrscr();

cout<<"Coordinates of rectangular clip window :\nxmin,ymin :";

cin>>xmin>>ymin;
cout<<"xmax,ymax :";

cin>>xmax>>ymax;

cout<<"\n\nPolygon to be clipped :\nNumber of sides :";

cin>>n;

cout<<"Enter the coordinates :";

for(int i=0;i < 2*n;i++)

cin>>polyy[i];

polyy[i]=polyy[0];

polyy[i+1]=polyy[1];

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

poly[i]=round(polyy[i]);

initgraph(&gdriver,&gmode,"C:\\TC\\BGI");

setcolor(RED);

rectangle(xmin,ymax,xmax,ymin);

cout<<"\t\tUNCLIPPED POLYGON";

setcolor(WHITE);

fillpoly(n,poly);

getch();

cleardevice();

k=0;

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

clipl(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
n=k/2;

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

polyy[i]=arr[i];

polyy[i]=polyy[0];

polyy[i+1]=polyy[1];

k=0;

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

clipt(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);

n=k/2;

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

polyy[i]=arr[i];

polyy[i]=polyy[0];

polyy[i+1]=polyy[1];

k=0;

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

clipr(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);

n=k/2;

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

polyy[i]=arr[i];

polyy[i]=polyy[0];

polyy[i+1]=polyy[1];

k=0;
for(i=0;i < 2*n;i+=2)

clipb(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);

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

poly[i]=round(arr[i]);

if(k)

fillpoly(k/2,poly);

setcolor(RED);

rectangle(xmin,ymax,xmax,ymin);

cout<<"\tCLIPPED POLYGON";

getch();

closegraph();

OUTPUT:
Experiment No: 11

Title- WINDOW-VIEWPORT MAPPING

Objective Window- A region of the scene selected for viewing (also called clipping window)
Viewport : A region on display device form aping to window Translate lower-left corner of
window to origin Scale width and height of window to match viewport’s Translate corner at
origin to lower-left corner in viewport.

Algorithm-
1. Read the window & view port co-ordinates.
2. Read the number of polygon vertices
3. Draw the filled polygon inside the window.
4. Draw the view port
5. Map the polygon which is in the window to the view port.
6. Display the polygon inside the view port.

Code
#include"stdio.h"
#include"conio.h"
#include"graphics.h"
#include"stdlib.h"
void main()
{
    float xwmin,xwmax,ywmax,ywmin;
    float xvmin,xvmax,yvmax,yvmin;
    float x[10],y[10],yv,xv,sx,sy;
    int gd=DETECT,gm,i;
    clrscr();
    printf("\n enter window port coordinates:\n(xwmin,ywmin,xwmax,ywmax): ");
    scanf("%f%f%f%f",&xwmin,&ywmin,&xwmax,&ywmax);
    printf("\n enter view port coordinates:\n(xvmin,yvmin,xvmax,yvmax): ");
    scanf("%f%f%f%f",&xvmin,&yvmin,&xvmax,&yvmax);
    printf("\n enter vertices for triangle: ");
    for(i=0;i < 3;i++)
    {
    printf("\n enter(x%d,y%d):",i,i);
    scanf("%f%f",&x[i],&y[i]);
    }
    sx=((xvmax-xvmin)/(xwmax-xwmin));
    sy=((yvmax-yvmin)/(ywmax-xwmin));

    initgraph(&gd,&gm," ");
    outtextxy(80,30,"window port");
    rectangle(xwmin,ywmin,xwmax,ywmax);
    for(i=0;i <2;i++)
    {
    line(x[i],y[i],x[i+1],y[i+1]);
    }
    line(x[2],y[2],x[0],y[0]);
    getch();
    cleardevice();
    for(i=0;i <3;i++)
    {
    x[i]=xvmin+((x[i]-xwmin)*sx);
    y[i]=yvmin+((y[i]-ywmin)*sy);
    }
    outtextxy(150,10,"view port");
    rectangle(xvmin,yvmin,xvmax,yvmax);
    for(i=0;i <2;i++)
    {
    line(x[i],y[i],x[i+1],y[i+1]);
    }
    line(x[2],y[2],x[0],y[0]);
    getch();
}

Output
Experiment No:12

Title- 3D TRANSFORMATIONS

Objective- To study and Implement 3D Transformation such as Translation, Rotation and


Scaling. Pre-requisite Knowledge of  Point Plotting Methods  GraphicsInitializations  3D
Transformation

Algorithm

1. Start.

2. Draw an image with default parameters.

3. Get the choice from user.

4. Get the parameters for transformation.

5. Perform the transformation.

6. Display the output.

7. Stop

Code-

#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 x,y,z,o,x1,x2,y1,y2;

int gd=DETECT,gm;

detectgraph(&gd,&gm);

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

//setfillstyle(0,getmaxcolor());

maxx=getmaxx();

maxy=getmaxy();

midx=maxx/2;

midy=maxy/2;

axis();

bar3d(midx+50,midy-100,midx+60,midy-90,10,1);

printf("Enter translation factor");

scanf("%d%d",&x,&y);

//axis();

printf("After translation:");
bar3d(midx+x+50,midy-(y+100),midx+x+60,midy-(y+90),10,1);

getch();

closegraph();

OUTPUT:
Experiment No:13

Title- Projections of 3D images.

Objective-The main Objective is to project the 3D image using the parallel line projection

Algorithm

1. Draw the 3D bar using the inbuilt function bar3d.

2. Draw the front, side & top elevation of the 3D image by projecting the 3D image using the
parallel line projection.

3. Display the 3D image along with the front, side & top view.

Code-

#include "stdio.h"

#include "stdlib.h"

#include"graphics.h"

#include"conio.h"

void draw3d(int s,int x[20],int y[20],int d);

void main()

int gd=DETECT,gm;

int x[20],y[20],i,s,d;

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

printf("Enter the No of sides : ");

scanf("%d",&s);

for(i=0;i< s;i++)>
{

printf("(x%d,y%d) :",i,i);

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

printf("Depth :");

scanf("%d",&d);

draw3d(s,x,y,d);

getch();

setcolor(14);

for(i=0;i< s-1;i++)

line(x[i]+200,y[i],x[i+1]+200,y[i+1]);

line(x[i]+200,y[i],x[0]+200,y[0]);

getch();//top view

for(i=0;i< s-1;i++)

line(x[i],300,x[i+1],300);

line(x[i],300+d*2,x[i+1],300+d*2);

line(x[i],300,x[i],300+d*2);

line(x[i+1],300,x[i+1],300+d*2);

}
getch();//side view

for(i=0;i< s-1;i++)

line(10,y[i],10,y[i+1]);

line(10+d*2,y[i],10+d*2,y[i+1]);

line(10,y[i],10+d*2,y[i]);

line(10,y[i+1],10+d*2,y[i+1]);

getch();

closegraph();

void draw3d(int s,int x[20],int y[20],int d)

int i,j,k=0;

for(j=0;j< 2;j++)

for(i=0;i< s-1;i++)>

line(x[i]+k,y[i]-k,x[i+1]+k,y[i+1]-k);

line(x[i]+k,y[i]-k,x[0]+k,y[0]-k);

k=d;

for(i=0;i< s;i++)
line(x[i],y[i],x[i]+d,y[i]-d);

Output:

You might also like