You are on page 1of 40

Enr No.

:201403100810022

Practical:1
Aim: Study Graphics library functions initgraph, detectgraph,
closegraph, cleardevice, putpixel, getpixel, getmaxx, getmaxy, line, circle,
rectangle, ellipse, arc, drawpoly, fillpoly, outtextxy, pieslice, setcolor,
getcolor, setbkcolor, getbkcolor.

1. initgraph - start the graphics window

Declaration:

void initgraph (int *graphdriver, int *graphmode, char *pathtodriver);

Description:

- To start the graphics system, you must first call initgraph.


- initgraph initializes the graphics system by loading a graphics driver from disk
(or validating a registered driver) then putting the system into graphics mode.
- initgraph also resets all graphics settings (color, palette, current position,
viewport, etc.) to their defaults, then resets graph result to 0.

Return Value:

initgraph always sets the internal error code.


- On success, initgraph sets the code to 0.
- On error, initgraph sets *graphdriver to -2, -3, -4, or -5.

2. detectgraph - set the highest available graphics mode

Declaration:

void detectgraph(int *graphdriver, int *graphmode);

Description:

- detectgraph() initializes the graphics system to the highest mode available.


The mode selected is returned in graphmode.

- Since the graphdriver argument is ignored the mode set is always


VGAMAX (i.e 800x600x8 windowed). Hence after this call graphmode is
always equal to VGAMAX.

3. closegraph :close the graphic window.

Declaration:

void closegraph (void);


CGPIT/IT/SEM-5/Computer Graphics
1
Enr No.:201403100810022

Description:

- closegraph() is used to shutdown the graphics system and close the


graphics window. Once it is called the graphics window will have to be
reopened with another call to initgraph() if needed.
- closegraph() is called by default when your program quits but it is better
to call it explicitly before main() returns.

4. cleardevice - clears the graphics window

Declaration:

void cleardevice (void);

Description:

- cleardevice() erases the entire graphics screen and moves the current position
as given by (getx(3), gety(3)) to (0,0). This function is the graphics equivalent
of clear or clrscr(3).

(Erasing consists of filling with the current background color.)

5. putpixel - set the color of pixel at (x,y)

Declaration:

void putpixel (int x, int y, int color);

Description:

- putpixel() plots a point in the color defined by color in graphics.h at (x, y).

6. getpixel - get the color of pixel at (x,y)

Declaration:

int getpixel (int x, int y);


Description:
-
getpixel() gets the color of the pixel located at (x, y).

7. getmaxx, getmaxy - get maximum (x, y) screen values

Declaration:

int getmaxx (void);


int getmaxy (void);

CGPIT/IT/SEM-5/Computer Graphics
2
Enr No.:201403100810022

Description:

- getmaxx() returns the maximum x value (screen-relative) for the current


graphics driver and mode.

- getmaxy() returns the maximum y value (screen-relative) for the current


graphics driver and mode.

8. line, lineto, linerel - draw a line between two points

Declaration:

void line (int x1, int y1, int x2, int y2);
void lineto (int x, int y);
void linerel (int dx, int dy);

Description:

- line() draws a line from (x1, y1) to (x2, y2) using the current color. It does
not update the current position (CP).

- lineto() draws a line from the current position CP to (x, y), then moves the
CP to (x, y).

- linerel() draws a line from the CP to a point that is a relative distance (dx,
dy) from the CP, then advances the CP by (dx, dy).

9. circle - draws a circle with given center and radius

Declaration:

void circle (int xc, int yc, int rad);

Description:

- circle() draws a circle in the current drawing color using (xc, yc) as center
and rad as radius.

10. rectangle - draw a rectangle with given corner co-ordinates

Declaration:

void rectangle (int left, int top, int right, int bottom);

CGPIT/IT/SEM-5/Computer Graphics
3
Enr No.:201403100810022

Description:

- rectangle() draws a rectangle in the current drawing color.


- (left, top) is the upper left corner of the rectangle
- (right, bottom) is its lower right corner.

11. ellipse- draw an ellipse given center, radii and angle range

Declaration:

ellipse (int xc, int yc, int stangle, int endangle,


int xradius, int yradius);
Declaration:

- ellipse() draws an elliptical arc in the current drawing color.

Arguments :
(xc, yc) -- Center of ellipse
xradius -- Horizontal axis
yradius -- Vertical axis
stangle -- Starting angle
endangle -- Ending angle

The ellipse travels from stangle to endangle. If stangle = 0 and endangle = 360,
the call to ellipse() draws a complete ellipse.

12. arc, pieslice - draw an arc or pieslice of given angle-range

Declaration:

void arc (int xc, int yc, int stangle, int endangle, int radius);
void pieslice (int xc, int yc, int stangle, int endangle, int radius);

Description:

- arc() draws a circular arc using the current drawing color.


- pieslice() draws and fills a pie-slice in the current drawing color.

Arguments :
- (xc, yc) -- Center point of arc or pieslice.
- stangle -- start angle in degrees(as measured on the standard circle).
- endangle -- end angle in degrees(as measured on the standard circle).
- radius -- radius of arc or pieslice.

- The arc() or pieslice() travels from stangle to endangle.


- If stangle = 0 and endangle = 360 the call to arc() or pieslice() draws a
complete circle.

CGPIT/IT/SEM-5/Computer Graphics
4
Enr No.:201403100810022

13. drawpoly, fillpoly - draw/fill a polygon given a set of points

Declaration:

void drawpoly (int numpoints, int *polypoints);


void fillpoly (int numpoints, int *polypoints);

Description:

- drawpoly() draws a polygon using the current drawing color.

- fillpoly() draws the outline of a polygon using the current color, then fills
the polygon using the current color.

Arguments :
- numpoints -- Specifies number of points
- *polypoints -- Points to an array of (numpointsx2) integers Each pair of
integers gives the x and y coordinates of a point on the polygon.

- To draw a closed figure with N vertices, you must pass N+1 coordinates
to drawpoly(), where the Nth coordinate == the 0th coordinate.

14. outtext, outtextxy - prints a string at current-position(CP)/ specified x-y location

Declaration:

void outtext (char *textstring);


void outtextxy (int x, int y, char *textstring);

Description:

- outtext() and outtextxy() display a text string, using the *current* font-color.

- outtext() outputs textstring at the current position (CP) and updates this
position by the length of the text.

- outtextxy() displays textstring in the viewport at the position (x, y) and does
not affect CP.

15. setcolor, getcolor - set/get the current drawing color

Declaration:

void setcolor (int color);


int getcolor (void);

Description:
CGPIT/IT/SEM-5/Computer Graphics
5
Enr No.:201403100810022

- getcolor() returns the current drawing color.


- setcolor() sets the current drawing color to color, which can range from 0 to
getmaxcolor(3).
- To select a drawing color with setcolor(), you can pass either the color number
or the equivalent color name.
- The drawing color is the value that pixels are set to when the program draws
lines, etc.

16. setbkcolor, getbkcolor - set/get the current background color

Declaration:

void setbkcolor (int color);


int getbkcolor (void);

Description:

- getbkcolor() returns the current background color.


- setbkcolor() sets the background to the color specified by color.
color is either a number or symbolic name specifying the color to set.

CGPIT/IT/SEM-5/Computer Graphics
6
Enr No.:201403100810022

Practical:2
Aim: Implement a program that create and fill 2D objects using graphics
library function.
- House
- Bike

Code:
1.House
#include<stdio.h>
#include<graphics.h>

void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,NULL);

rectangle(100,400,600,150);
rectangle(100,400,300,150);
line(100,150,200,50);
line(200,50,300,150);
line(200,50,500,50);
line(500,50,600,150);
rectangle(150,400,250,220);
rectangle(350,220,420,270);
rectangle(480,220,550,270);
//rectangle(300,400,400,150);
getch();
closegraph();
}

2.Bike
#include<stdio.h>
#include<graphics.h>

void main()
{
int gd=DETECT,gm;
int x[]={30,400,90,280,200,280,230,200,420,200,450,280,520,
280,580,400,520,400,380,400,270,400,130,400,30,400};
initgraph(&gd,&gm,NULL);
//setcolor(3);

circle(200,400,50);
circle(450,400,50);

CGPIT/IT/SEM-5/Computer Graphics
7
Enr No.:201403100810022

arc(200,400,180,360,70);
arc(450,400,180,360,70);
line(270,400,380,400);
line(30,400,130,400);
line(520,400,580,400);
line(30,400,90,280);
line(580,400,520,280);
line(200,280,90,280);
line(520,280,450,280);
line(200,280,230,200);
line(450,280,420,200);
line(230,200,420,200);

arc(325,200,180,360,20);
line(240,110,320,180);
line(235,115,310,186);
//setcolor(GREEN);
//fillpoly(13,x);
//setcolor(WHITE);
//pieslice(200,400,0,360,50);
//pieslice(450,400,0,360,50);

getch();
closegraph();

Output:

CGPIT/IT/SEM-5/Computer Graphics
8
Enr No.:201403100810022

CGPIT/IT/SEM-5/Computer Graphics
9
Enr No.:201403100810022

Practical:3
Aim: Implement the program that draws square using DDA Line drawing
algorithm.

Code:
#include<stdio.h>
#include<graphics.h>
#include<math.h>

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


{
int i,s;
float dx,dy,xi,yi;
dx=abs(x2-x1);
dy=abs(y2-y1);

if(dx>dy)
{ s=dx; }
else
{ s=dy; }

xi=dx/(float)s;
yi=dy/(float)s;
putpixel(x1,y1,4);
for(i=0;i<s;i++)
{
x1=x1+xi;
y1=y1+yi;

putpixel(x1,y1,4);
delay(10);
}
}

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


{
int i,s;
float dx,dy,xi,yi;
dx=abs(x2-x1);
dy=abs(y2-y1);

if(dx>dy)
{ s=dx; }
else
{ s=dy; }

CGPIT/IT/SEM-5/Computer Graphics
10
Enr No.:201403100810022

xi=dx/(float)s;
yi=dy/(float)s;
putpixel(x1,y1,4);
for(i=0;i<s;i++)
{
x1=x1-xi;
y1=y1-yi;

putpixel(x1,y1,4);
delay(10);
}
}
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,NULL);

ddaline(100,100,300,100);
ddaline(300,100,300,300);
//ddaline(100,100,100,300);
//ddaline(100,300,300,300);

ddaline1(300,300,100,300);
ddaline1(100,300,100,100);

getch();
closegraph();
}

Output:

CGPIT/IT/SEM-5/Computer Graphics
11
Enr No.:201403100810022

Practical:4
Aim: Implement the program that draws line using Bresenhams Line
drawing algorithm.

Code:
#include<stdio.h>
#include<graphics.h>
#include<stdlib.h>
#include<math.h>

void main()
{
int x1,y1,x2,y2,pk,p0,k;
float dx,dy,s;
printf("x1= "); scanf("%d",&x1);
printf("y1= "); scanf("%d",&y1);
printf("x2= "); scanf("%d",&x2);
printf("y2= "); scanf("%d",&y2);

int gd=DETECT,gm;
initgraph(&gd,&gm,NULL);
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>abs(dy))
s=abs(dx);
else
s=abs(dy);
for(k=0;k<=s;k++)
{
p0=(2*dy)-dx;
pk=p0;
if(pk<0)
{ pk=pk+(2*dy);
x1++;
}
else if(pk>=0)
{ pk=pk+((2*dy)-(2*dx));
x1++;
y1++;
}
putpixel(x1,y1,4);
delay(50);
}
getch();
closegraph();
}

CGPIT/IT/SEM-5/Computer Graphics
12
Enr No.:201403100810022

Output:

CGPIT/IT/SEM-5/Computer Graphics
13
Enr No.:201403100810022

Practical:5
Aim: Implement Midpoint Circle Algorithm.
Implement Midpoint Ellipse Algorithm.

Code:
1.Midpoint Circle
#include<stdio.h>
#include<graphics.h>
#include<math.h>

void main()
{
int gd=DETECT,gm;
float pk;
int i,r,x,y,xc,yc;

printf("Enter the radius of the circle:");


scanf("%d",&r);
printf("Enter the mid points\nxc=");
scanf("%d",&xc);
printf("yc=");
scanf("%d",&yc);
initgraph(&gd,&gm,NULL);
x=0;
y=r;
pk=1-r;

while(x<=y)
{

if(pk<0)
{
x++;
pk=pk+2*x+1;
}
else
{
x++;
y--;
pk=pk+2*(x-y)+1;
}
delay(50);
putpixel(xc+x,yc+y,1); //4th
putpixel(xc+y,yc+x,2); //4th
putpixel(xc-y,yc+x,3); //3rd
putpixel(xc-x,yc+y,4); //3rd
CGPIT/IT/SEM-5/Computer Graphics
14
Enr No.:201403100810022

putpixel(xc-x,yc-y,5); //2nd
putpixel(xc-y,yc-x,6); //2nd
putpixel(xc+y,yc-x,7); //1st
putpixel(xc+x,yc-y,8); //1st

}
putpixel(r+xc,0+yc,3);
putpixel(-r+xc,0+yc,3);
putpixel(0+xc,r+yc,3);
putpixel(0+xc,-r+yc,3);
getch();
closegraph();
}

2.Midpoint Ellipse
#include<stdio.h>
#include<graphics.h>

void mellipse(int xc,int yc,int rx,int ry)


{
int gm=DETECT,gd;
int x, y, p;

initgraph(&gm,&gd,NULL);
x=0;
y=ry;
p=(ry*ry)-(rx*rx*ry)+((rx*rx)/4);
while((2*x*ry*ry)<(2*y*rx*rx))
{
putpixel(xc+x,yc-y,BLUE);
putpixel(xc-x,yc+y,BLUE);
putpixel(xc+x,yc+y,BLUE);
putpixel(xc-x,yc-y,BLUE);

if(p<0)
{
x=x+1;
p=p+(2*ry*ry*x)+(ry*ry);
}
else
{
x=x+1;
y=y-1;
p=p+(2*ry*ry*x+ry*ry)-(2*rx*rx*y);
}
delay(50);
CGPIT/IT/SEM-5/Computer Graphics
15
Enr No.:201403100810022

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

while(y>=0)
{
putpixel(xc+x,yc-y,RED);
putpixel(xc-x,yc+y,RED);
putpixel(xc+x,yc+y,RED);
putpixel(xc-x,yc-y,RED);

if(p>0)
{
y=y-1;
p=p-(2*rx*rx*y)+(rx*rx);

}
else
{
y=y-1;
x=x+1;
p=p+(2*ry*ry*x)-(2*rx*rx*y)-(rx*rx);
}
delay(50);
}
getch();
closegraph();
}

void main()
{
int xc,yc,rx,ry;
printf("Enter Xc=");
scanf("%d",&xc);
printf("Enter Yc=");
scanf("%d",&yc);
printf("Enter Rx=");
scanf("%d",&rx);
printf("Enter Ry=");
scanf("%d",&ry);
mellipse(xc,yc,rx,ry);
getch();
}

CGPIT/IT/SEM-5/Computer Graphics
16
Enr No.:201403100810022

Output:

CGPIT/IT/SEM-5/Computer Graphics
17
Enr No.:201403100810022

Practical:6

Aim: Implement Polygon filling Algorithms:


- Boundary fill
- Flood fill Algorithm

Code:
1.Boundary fill
#include<stdio.h>
#include<graphics.h>

void boundaryfill(int x,int y,int boundary,int fill)


{
int currentpixel;
currentpixel=getpixel(x,y);
// printf("%d",&currentpixel);
if((currentpixel!=boundary) && (currentpixel!=fill))
{
// setcolor(fill);
putpixel(x,y,fill);

boundaryfill(x+1,y,boundary,fill);
boundaryfill(x-1,y,boundary,fill);
boundaryfill(x,y+1,boundary,fill);
boundaryfill(x,y-1,boundary,fill);
}
}

void main()
{
int gd=DETECT,gm;
int x,y;
printf("Enter pixel values: ");
scanf("%d %d",&x,&y);
initgraph(&gd,&gm,NULL);

setcolor(5);
rectangle(100,100,400,400);
boundaryfill(x,y,5,7);
getch();
closegraph();
}

CGPIT/IT/SEM-5/Computer Graphics
18
Enr No.:201403100810022

Output:

2.Flood fill
#include<stdio.h>
#include<graphics.h>

void boundaryfill(int x,int y,int boundary,int fill)


{
int currentpixel;

currentpixel=getpixel(x,y);
if((currentpixel!=boundary) && (currentpixel!=fill))
{
putpixel(x,y,fill);

boundaryfill(x+1,y,boundary,fill);
boundaryfill(x-1,y,boundary,fill);
boundaryfill(x,y+1,boundary,fill);
boundaryfill(x,y-1,boundary,fill);
}
}

void flfill(int x,int y,int fillcolor,int oldcolor)


{
if(getpixel(x,y)== oldcolor)
{
putpixel(x,y,fillcolor);

flfill(x+1,y,fillcolor,oldcolor);
flfill(x-1,y,fillcolor,oldcolor);
flfill(x,y+1,fillcolor,oldcolor);
CGPIT/IT/SEM-5/Computer Graphics
19
Enr No.:201403100810022

flfill(x,y-1,fillcolor,oldcolor);
}
}
void main()
{
int gd=DETECT,gm;
int x,y,i;
printf("Enter pixel values: ");
scanf("%d %d",&x,&y);
initgraph(&gd,&gm,NULL);

setcolor(5);
rectangle(100,100,400,400);
boundaryfill(x,y,5,4);

flfill(x,y,6,5);
getch();
closegraph();
}

Output:

CGPIT/IT/SEM-5/Computer Graphics
20
Enr No.:201403100810022

Practical:7

Aim: Implement following 2D transformations for line and polygon using


switch case.

- Translation
- Rotations (about origin & pivot point)
- Scaling (about origin & fixed point)

Code:
#include <stdio.h>
#include <graphics.h>
#include<math.h>
void main()
{
int gm;
int gd=DETECT;
int x1,x2,x3,y1,y2,y3,nx1,nx2,nx3,ny1,ny2,ny3,c;
int sx,sy,xt,yt,r;
float t;
printf("\n\t Enter the points of triangle");
setcolor(1);
scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3);
initgraph(&gd,&gm,"NULL");
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
delay(1);
closegraph();
printf("\n 1.Transaction\n 2.Rotation\n 3.Scalling\n 4.exit");
printf("Enter your choice:");
scanf("%d",&c);
switch(c)
{
case 1:
printf("\n Enter the translation factor");
scanf("%d%d",&xt,&yt);
initgraph(&gd,&gm,"NULL");
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
nx1=x1+xt;
ny1=y1+yt;
nx2=x2+xt;
ny2=y2+yt;
nx3=x3+xt;
CGPIT/IT/SEM-5/Computer Graphics
21
Enr No.:201403100810022

ny3=y3+yt;
line(nx1,ny1,nx2,ny2);
line(nx2,ny2,nx3,ny3);
line(nx3,ny3,nx1,ny1);
delay(1);
getch();
closegraph();
case 2:
/* printf("\n Enter the angle of rotation");
scanf("%d",&r);
t=3.14*r/180;
nx1=abs(x1*cos(t)-y1*sin(t));
ny1=abs(x1*sin(t)+y1*cos(t));
nx2=abs(x2*cos(t)-y2*sin(t));
ny2=abs(x2*sin(t)+y2*cos(t));
nx3=abs(x3*cos(t)-y3*sin(t));
ny3=abs(x3*sin(t)+y3*cos(t));
line(nx1,ny1,nx2,ny2);
line(nx2,ny2,nx3,ny3);
line(nx3,ny3,nx1,ny1);
getch();*/
case 3:
printf("\n Enter the scalling factor");
scanf("%d%d",&sx,&sy);
initgraph(&gd,&gm,"NULL");
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
nx1=x1*sx;
ny1=y2*sy;
nx2=x2*sx;
ny2=y2*sy;
nx3=x3*sx;
ny3=y3*sy;
line(nx1,ny1,nx2,ny2);
line(nx2,ny2,nx3,ny3);
line(nx3,ny3,nx1,ny1);
delay(1);
getch();
closegraph();
case 4:
break;
default:
printf("Enter the correct choice");
}
}

CGPIT/IT/SEM-5/Computer Graphics
22
Enr No.:201403100810022

Output:

CGPIT/IT/SEM-5/Computer Graphics
23
Enr No.:201403100810022

Practical:8

Aim: Implement following 3D transformations for line and polygon using


switch case.

- Translation
- Rotations (about origin & pivot point)
- Scaling (about origin & fixed point)

Code:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void trans();
//void axis();
void scale();
void rotate();
intmaxx,maxy,midx,midy;
void main()
{
intch;
intgd=DETECT,gm;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"e:\\tc\\bgi");
printf("\n 1.Translation \n2.Scaling\n 3.Rotation \n 4.exit");
printf("enter your choice");
scanf("%d",&ch);
do
{
switch(ch)
{
case 1 : trans();
getch();
closegraph();
break;

case 2 : scale();
getch();
closegraph();
break;

case 3 : rotate();
getch();
closegraph();
break;
CGPIT/IT/SEM-5/Computer Graphics
24
Enr No.:201403100810022

case 4 : break;
}
printf("enter your choice");
scanf("%d",&ch);
} while(ch<4);
}
void trans()
{
int x,y,z,o,x1,x2,y1,y2;
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);
printf("After translation:");
bar3d(midx+x+50,midy-(y+100),midx+x+60,midy-(y+90),10,1);
}
void scale()
{
int x,y,z,o,x1,x2,y1,y2;
maxx=getmaxx();
maxy=getmaxy();
midx=maxx/2;
midy=maxy/2;
//axis();
bar3d(midx+50,midy-100,midx+60,midy-90,5,1);
printf("before translation\n");
printf("Enter scaling factors\n");
scanf("%d %d %d", &x,&y,&z);
printf("After scaling\n");
bar3d(midx+(x*50),midy-(y*100),midx+(x*60),midy-(y*90),5*z,1);
}
void rotate()
{
int x,y,z,o,x1,x2,y1,y2;
maxx=getmaxx();
maxy=getmaxy();
midx=maxx/2;
midy=maxy/2;
//axis();
bar3d(midx+50,midy-100,midx+60,midy-90,5,1);
printf("Enter rotating 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);
CGPIT/IT/SEM-5/Computer Graphics
25
Enr No.:201403100810022

y2=60*sin(o*3.14/180)+90*cos(o*3.14/180);
printf("After rotation about x axis");
bar3d(midx+50,midy-x1,midx+60,midy-x2,5,1);
//axis();
printf("After rotation about yaxis");
bar3d(midx+x1,midy-100,midx+x2,midy-90,5,1);
}

Output:-

CGPIT/IT/SEM-5/Computer Graphics
26
Enr No.:201403100810022

Practical:9
Aim: Implement Cohen-Sutherland Line Clipping Algorithm.

Code:

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

#define xmin 200


#define xmax 400
#define ymin 150
#define ymax 300
#include<dos.h>
void main()
{
int i,a[4]={0,0,0,0},b[4]={0,0,0,0},x1,x2,y1,y2;
int gd=DETECT,gm;
float m;
initgraph(&gd,&gm,"C:\\turboc3\\bgi");
cleardevice();
rectangle(xmin,ymin,xmax,ymax);
outtextxy(xmin-20,ymin-10,"(200,150)");
outtextxy(xmax,ymin-10,"(400,150)");
outtextxy(xmax,ymax+10,"(400,300)");
outtextxy(xmin-20,ymax+10,"(200,300)");
printf("\nenter the value of x1: ");
scanf("%d",&x1);
printf("\nenter the value of y1: ");
scanf("%d",&y1);
printf("\nenter the value of x2: ");
scanf("%d",&x2);
printf("\nenter the value of y2: ");
scanf("%d",&y2);
line(x1,y1,x2,y2);
getch();

if(x1<=xmin)
a[0]=1;
if(x1>=xmax)
a[1]=1;
if(y1>=ymax)
a[2]=1;
if(y1<=ymin)
a[3]=1;

CGPIT/IT/SEM-5/Computer Graphics
27
Enr No.:201403100810022

if(x2<=xmin)
b[0]=1;
if(x2>=xmax)
b[1]=1;
if(y2>=ymax)
b[2]=1;
if(y2<=ymin)
b[3]=1;
if((a[0]*b[0])==1 || (a[1]*b[1])==1 || (a[2]*b[2])==1 || (a[3]*b[3])==1){
cleardevice();
rectangle(xmin,ymin,xmax,ymax);
}
else if(a[0]==b[0] && a[1]==b[1] && a[2]==b[2] && a[3]==b[3]){
cleardevice();
rectangle(xmin,ymin,xmax,ymax);
line(x1,y1,x2,y2);
}
else{
if((x2-x1)!=0)
{
m=(float)(y2-y1)/(x2-x1);
if(x1<xmin)
{
y1=y1+(float)(m*(xmin-x1));
x1=xmin;
}
if(x1>xmax)
{
y1=y1+(float)(m*(xmax-x1));
x1=xmax;
}
if(y1>ymax)
{
x1=x1+(float)((ymax-y1)/m);
y1=ymax;
}
if(y1<ymin)
{
x1=x1+(float)((ymin-y1)/m);
y1=ymin;
}
if(x2<xmin)
{
y2=y2+(float)(m*(xmin-x2));
x2=xmin;
}
if(x2>xmax)
{
y2=y2+(float)(m*(xmax-x2));
x2=xmax;
CGPIT/IT/SEM-5/Computer Graphics
28
Enr No.:201403100810022

}
if(y2>ymax)
{
x2=x2+(float)((ymax-y2)/m);
y2=ymax;
}
if(y2<ymin)
{
x2=x2+(float)((ymin-y2)/m);
y2=ymin;
}
setcolor(4);
line(x1,y1,x2,y2);
}
else{
if(y1<ymin)
y1=ymin;
if(y1>ymax)
y1=ymax;
if(y2<ymin)
y2=ymin;
if(y2>ymax)
y2=ymax;
setcolor(4);
line(x1,y1,x2,y2);
}
}
getch();
closegraph();
}

Output:

CGPIT/IT/SEM-5/Computer Graphics
29
Enr No.:201403100810022

Practical:10
Aim: Implement Liang - Barsky Line Clipping Algorithm.

Code:

#include <graphics.h>
#include <dos.h>
#include <conio.h>
#include <stdlib.h>

void main()
{
int gd, gm ;
int x1 , y1 , x2 , y2 ;
int wxmin,wymin,wxmax, wymax ;
float u1 = 0.0,u2 = 1.0 ;
int p1 , q1 , p2 , q2 , p3 , q3 , p4 ,q4 ;
float r1 , r2 , r3 , r4 ;
int x11 , y11 , x22 , y22 ;
//clrscr();

printf("Enter the windows left xmin , top boundry ymin\n");


scanf("%d%d",&wxmin,&wymin);
printf("Enter the windows right xmax ,bottom boundry ymax\n");
scanf("%d%d",&wxmax,&wymax);
printf("Enter line x1 , y1 co-ordinate\n");
scanf("%d%d",&x1,&y1);
printf("Enter line x2 , y2 co-ordinate\n");
scanf("%d%d",&x2,&y2);
printf("liang barsky express these 4 inequalities using lpk<=qpk\n");

p1 = -(x2 - x1 ); q1 = x1 - wxmin ;
p2 = ( x2 - x1 ) ; q2 = wxmax - x1 ;
p3 = - ( y2 - y1 ) ; q3 = y1 - wymin ;
p4 = ( y2 - y1 ) ; q4 = wymax - y1 ;

printf("p1=0 line is parallel to left clipping\n");


printf("p2=0 line is parallel to right clipping\n");
printf("p3=0 line is parallel to bottom clipping\n");
printf("p4=0 line is parallel to top clipping\n");

if( ( ( p1 == 0.0 ) && ( q1 < 0.0 ) ) ||( ( p2 == 0.0 ) && ( q2 < 0.0 ) ) ||
( ( p3 == 0.0 ) && ( q3 < 0.0 ) ) ||( ( p4 == 0.0 ) && ( q4 < 0.0 ) ) )
{
printf("Line is rejected\n");

CGPIT/IT/SEM-5/Computer Graphics
30
Enr No.:201403100810022

getch();
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:\\turboc3\\bgi");
setcolor(RED);
rectangle(wxmin,wymax,wxmax,wymin);
setcolor(BLUE);
line(x1,y1,x2,y2);
getch();
setcolor(WHITE);
line(x1,y1,x2,y2);
getch();
}
else
{
if( p1 != 0.0 )
{
r1 =(float) q1 /p1 ;
if( p1 < 0 )
u1 = max(r1 , u1 );
}
else
u2 = min(r1 , u2 );
}

if( p2 != 0.0 )
{
r2 = (float ) q2 /p2 ;
if( p2 < 0 )
u1 = max(r2 , u1 );
else
u2 = min(r2 , u2 );
}
if( p3 != 0.0 )
{
r3 = (float )q3 /p3 ;
if( p3 < 0 )
u1 = max(r3 , u1 );
else
u2 = min(r3 , u2 );
}

if( p4 != 0.0 )
{
r4 = (float )q4 /p4 ;
if( p4 < 0 )
u1 = max(r4 , u1 );
else
u2 = min(r4 , u2 );
}
CGPIT/IT/SEM-5/Computer Graphics
31
Enr No.:201403100810022

if( u1 > u2 )
printf("line rejected\n");
else
{
x11 = x1 + u1 * ( x2 - x1 ) ;
y11 = y1 + u1 * ( y2 - y1 ) ;

x22 = x1 + u2 * ( x2 - x1 );
y22 = y1 + u2 * ( y2 - y1 );

printf("Original line cordinates\n");


printf("x1 = %d , y1 = %d, x2 = %d, y2 = %d\n",x1,y1,x2,y2);
printf("Windows coordinate are \n");
printf("wxmin = %d, wymin = %d,wxmax = %d , wymax = %d
",wxmin,wymin,wxmax,wymax);

printf("New coordinates are \n");


printf("x1 = %d, y1 = %d,x2 = %d , y2 = %d\n",x11,y11,x22,y22);

detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:\\turboc3\\bgi");
setcolor(2);
rectangle(wxmin,wymax,wxmax,wymin);
setcolor(1);
line(x1,y1,x2,y2);
getch();
setcolor(0);
line(x1,y1,x2,y2);
setcolor(3);
line(x11,y11,x22,y22);
getch();
}
}

Output:

CGPIT/IT/SEM-5/Computer Graphics
32
Enr No.:201403100810022

CGPIT/IT/SEM-5/Computer Graphics
33
Enr No.:201403100810022

Practical:11
Aim: Implement Sutherland-Hodgmen Polygon Clipping Algorithm.

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

#define xmax 400


#define xmin 200
#define ymax 300
#define ymin 200

void main()
{
int gdriver=DETECT,gmode;
int n,i,p[15],j=-1,pt1=1,pt2=1,count=0,k;
int q[15],x,y,x1,y1,x2,y2;
float m;
clrscr();

initgraph(&gdriver,&gmode,"C:\\turboc3\\bgi");
cleardevice();

rectangle(xmin,ymin,xmax,ymax);
outtextxy(xmin-20,ymin-10,"(200,200)");
outtextxy(xmin-10,ymax+10,"(200,300)");
outtextxy(xmax,ymin-10,"(400,200)");
outtextxy(xmax,ymax+10,"(400,300)");

printf("\nEnter no. of vertices: ");


scanf("%d",&n);
CGPIT/IT/SEM-5/Computer Graphics
34
Enr No.:201403100810022

for(i=0;i<(n*2);i++)
{
printf("\nEnter p[%d]: ",i);
scanf("%d",&p[i]);
}
p[i]=p[0];
p[i+1]=p[1];
drawpoly(n+1,p);
//left
k=0;
for(i=0;i<n;i++)
{
x1=p[k];
y1=p[++k];
x2=p[++k];
y2=p[++k];
if(x1<xmin)
pt1=0;
if(x2<xmin)
pt2=0;
if(pt1==1 && pt2==1)
{
q[++j]=x2;
q[++j]=y2;
count++;
}
else if((pt1==0 && pt2==1) || (pt1==1 && pt2==0))
{
m=(float)((y2-y1)/(x2-x1));
y=y1+(m*(xmin-x1));
x=xmin;

q[++j]=x;
q[++j]=y;
count++;

if(pt1==0 && pt2==1)


{
q[++j]=x2;
q[++j]=y2;
count++;
}
}
k=k-1;
pt1=pt2=1;
}
q[++j]=q[0];
q[++j]=q[1];

//right
CGPIT/IT/SEM-5/Computer Graphics
35
Enr No.:201403100810022

for(i=0;i<((count*2)+2);i++)
{
p[i]=q[i];
}
k=0;
j=-1;
n=count;
count=0;

for(i=0;i<n;i++)
{
x1=p[k];
y1=p[++k];
x2=p[++k];
y2=p[++k];
if(x1>xmax)
pt1=0;
if(x2>xmax)
pt2=0;
if(pt1==1 && pt2==1)
{
q[++j]=x2;
q[++j]=y2;
count++;
}
else if((pt1==0 && pt2==1) || (pt1==1 && pt2==0))
{
m=(float)((y2-y1)/(x2-x1));
y=y1+(m*(xmax-x1));
x=xmax;
q[++j]=x;
q[++j]=y;
count++;

if(pt1==0 && pt2==1)


{
q[++j]=x2;
q[++j]=y2;
count++;
}
}
k=k-1;
pt1=pt2=1;
}
q[++j]=q[0];
q[++j]=q[1];

//bottom
for(i=0;i<((count*2)+2);i++)
{
CGPIT/IT/SEM-5/Computer Graphics
36
Enr No.:201403100810022

p[i]=q[i];
}
k=0;
j=-1;
n=count;
count=0;

for(i=0;i<n;i++)
{
x1=p[k];
y1=p[++k];
x2=p[++k];
y2=p[++k];
if(y1>ymax)
pt1=0;
if(y2>ymax)
pt2=0;
if(pt1==1 && pt2==1){
q[++j]=x2;
q[++j]=y2;
count++;
}
else if((pt1==0 && pt2==1) || (pt1==1 && pt2==0))
{
if((x2-x1)==0)
{
x=x1;
y=ymax;
}
else
{
m=(float)((y2-y1)/(x2-x1));
x=x1+((ymax-y1)/m);
y=ymax;
}
q[++j]=x;
q[++j]=y;
count++;

if(pt1==0 && pt2==1)


{
q[++j]=x2;
q[++j]=y2;
count++;
}
}
k=k-1;
pt1=pt2=1;
}
q[++j]=q[0];
CGPIT/IT/SEM-5/Computer Graphics
37
Enr No.:201403100810022

q[++j]=q[1];

//top
for(i=0;i<((count*2)+2);i++)
{
p[i]=q[i];
}
k=0;
j=-1;
n=count;
count=0;
for(i=0;i<n;i++)
{
x1=p[k];
y1=p[++k];
x2=p[++k];
y2=p[++k];
if(y1<ymin)
pt1=0;
if(y2<ymin)
pt2=0;
if(pt1==1 && pt2==1)
{
q[++j]=x2;
q[++j]=y2;
count++;
}
else if((pt1==0 && pt2==1) || (pt1==1 && pt2==0))
{
if((x2-x1)==0)
{
x=x1;
y=ymin;
}
else
{
m=(float)((y2-y1)/(x2-x1));
x=x1+((ymin-y1)/m);
y=ymin;
}
q[++j]=x;
q[++j]=y;
count++;

if(pt1==0 && pt2==1)


{
q[++j]=x2;
q[++j]=y2;
count++;
}
CGPIT/IT/SEM-5/Computer Graphics
38
Enr No.:201403100810022

}
k=k-1;
pt1=pt2=1;
}
q[++j]=q[0];
q[++j]=q[1];

getch();
cleardevice();

rectangle(xmin,ymin,xmax,ymax);
setcolor(5);
drawpoly(count+1,q);
getch();
closegraph();
}

Output:

CGPIT/IT/SEM-5/Computer Graphics
39
Enr No.:201403100810022

CGPIT/IT/SEM-5/Computer Graphics
40

You might also like