Files 3 2021 February NotesHubDocument 1613295767

Download as pdf or txt
Download as pdf or txt
You are on page 1of 75

COMPUTER GRAPHICS AND MULTIMEDIA

PRACTICAL FILE

SUBMITTED BY:
NAME :ARYAMAN
ROLL NO : 017 20902719
BRANCH :CSE 2ND YEAR

ARYAMAN 017202902719 CSE 2ND YR 1


1.AIM: To execute various fundamental Graphics Functions.

THOERY:

1.closegraph()

Function which closes the graphics mode, deallocates all memory allocated by graphics system and
restores the screen to the mode it was in before you called initgraph.

Syntax :

void closegraph();

ex.

int main()

{ int gd = DETECT, gm;


initgraph(&gd, &gm, "");
outtext("Press any key to close"
" the graphics mode !!");
getch();
closegraph();
return 0;
}
2.Initgraph()

Initgraph initializes the graphics system by loading a graphics driver from disk (or validating a
registered driver), and putting the system into graphics mode. To start the graphics system, first call
the initgraph function. initgraph loads the graphics driver and puts the system into graphics mode.

Syntax :

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


ex.

int main()

{ int gd = DETECT, gm;


initgraph(&gd, &gm, "");
outtext("Press any key to close"
" the graphics mode !!");
getch();
closegraph();
return 0;
}

3.cleardevice()

Function which clears the screen in graphics mode and sets the current position to (0,0). Clearing the
screen consists of filling the screen with current background color.

ARYAMAN 017202902719 CSE 2ND YR 2


Syntax :

void cleardevice();
ex.
// cleardevice function
cleardevice();

4.putpixel()

function which plots a pixel at location (x, y) of specified color.

Syntax :

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


where, (x, y) is the location at which pixel is to be put , and color specifies the color of the pixel.
ex.
putpixel(30, 40, RED);

5.getpixel()

function which returns the color of pixel present at location (x, y).

Syntax :

int getpixel(int x, int y);


ex.

int color;
color = getpixel(0, 0);

6.getmaxx()
function which returns the maximum X coordinate for current graphics mode and driver.

Syntax :

int getmaxx();
ex.
sprintf(arr, "Maximum X coordinate for current "
"graphics mode And driver = %d", getmaxx());
7.getmaxy()

function which returns the maximum Y coordinate for current graphics mode and driver.

Syntax :

int getmaxy();
ex.
sprintf(arr, "Maximum Y coordinate for current "

ARYAMAN 017202902719 CSE 2ND YR 3


"graphics mode And driver = %d", getmaxx());

8.line()

function is used to draw a line from a point(x1,y1) to point(x2,y2) i.e. (x1,y1) and (x2,y2) are end
points of the line.

Syntax :

void line(int x1, int y1, int x2, int y2);

9.lineto()

Function which draws a line from current position to the point(x,y).

Syntax :

lineto(int x, int y);where,


(x, y) are the coordinates upto which the
line will be drawn from previous point.

ex. // lineto function

lineto(250, 100);

10.gotoxy()

places cursor at a desired location on screen i.e., we can change cursor position at a specific position

Syntax :

void gotoxy(int x, int y);


where (x, y) is the position where we want to place the cursor.

Ex.int x, y;

x = 10;
y = 10;

gotoxy(x, y);

11.circle()
Function which draws a circle with center at (x, y) and given radius.

Syntax :

circle(x, y, radius); where,(x, y) is center of the circle.'radius' is the Radius of the circle.

ARYAMAN 017202902719 CSE 2ND YR 4


ex.

arc(x, y, start_angle, end_angle, radius);

12.arc()
function which draws an arc with center at (x, y) and given radius. start_angle is the starting point of
angle and end_angle is the ending point of the angle. The value of the angle can vary from 0 to 360
degree.

Syntax :
void arc(int x, int y, int start_angle,
int end_angle, int radius);
where,(x, y) is the center of the arc.start_angle is the starting angle and end_angle is the ending
angle.'radius' is the Radius of the arc
ex.

arc(x, y, start_angle, end_angle, radius);

13.pieslice()

Draws and fills a pie slice with center at (x, y) and given radius r. The slice travels from s_angle to
e_angle which are starting and ending angles for the pie slice. The angles for pie‐slice are given in
degrees and are measured counterclockwise.

Syntax :

void pieslice(int x, int y, int s_angle, int e_angle, int r)


where,(x, y) is center of the circle.r is the radius of the circle.s_angle and e_angle are the starting and
ending angles respectively.

ex.
pieslice(300, 300, 0, 120, 150);

14.ellipse()
In this function x, y is the location of the ellipse. x_radius and y_radius decide the radius of form x and
y.start_angle is the starting point of angle and end_angle is the ending point of angle. The value of
angle can vary from 0 to 360 degree.
Syntax :

void ellipse(int x, int y, int start_angle, int end_angle, int x_radius, int y_radius)

ARYAMAN 017202902719 CSE 2ND YR 5


ex. int start_angle = 0,end_angle= 360,x_rad = 100,y_rad = 50;
ellipse(x, y, start_angle,

end_angle, x_rad, y_rad);

15.sector()

Function which draws and fills an elliptical pie slice with (x, y) as center, (s_angle, e_angle) as
starting and ending angle and (x_radius, y_radius) as x and y radius of sector.

Syntax :
void sector(int x, int y, int s_angle, int e_angle, int x_radius, int y_radius);where,(x, y) is center of the
sector.(s_angle, e_angle) are starting
and ending angles.(x_radius, y_radius) are x and y radius of sector.
ex.

sector(200, 200, 0, 150, 50, 65);

16.rectangle()

It is used to draw a rectangle. Coordinates of left top and right bottom corner are required to draw
the rectangle. left specifies the X‐coordinate of top left corner, top specifies the Y‐coordinate of top
left corner, right specifies the X‐coordinate of right bottom corner, bottom specifies the Y‐coordinate
of right bottom corner.

Syntax :

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


ex.

rectangle(left, top, right, bottom);

17.drawpoly()

function which is used to draw polygons i.e. triangle, rectangle, pentagon, hexagon etc.

Syntax :

void drawpoly( int number, int *polypoints );


where,number indicates (n + 1) number of points where n is the number of vertices in a polygon.
polypoints points to a sequence of (n*2) integers.
ex.
int arr[] = {320, 150, 400, 250,
250, 350, 320, 150};
drawpoly(4, arr);

ARYAMAN 017202902719 CSE 2ND YR 6


Write a program to include all the functions and take snapshot (2 or more
to cover all functions).
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
int main()
{
int gd = DETECT, gm,color;
char arr1[10],arr2[10],arr3[10];
int poly[] = {300, 100, 275, 250,200, 270, 300, 100};
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
putpixel(30, 40, RED);
color = getpixel(30,40);
sprintf(arr1, "color of pixel at (30,40) = %d",color);
outtextxy(60,50,arr1);
sprintf(arr2, "Maximum X coordinate for current graphics mode And driver =
%d", getmaxx());
outtextxy(60,60,arr2);
sprintf(arr3, "Maximum Y coordinate for current graphics mode And driver =
%d", getmaxy());
outtextxy(60,70,arr3);
outtextxy(60,80,"LINE");
line(60, 90,100,90);
outtextxy(60,150,"LINETO");
lineto(250, 120);
printf("\t\t\n\n\n\n\n\nRECTANGLE");
rectangle(125,115,215,165);
printf("\t\t\t\n\n\n\n\n\n\nARC");
arc(120,200,180,0,30);
printf("\t\n\n\n\nCIRCLE");
circle(120,270,30);
printf("\t\n\n\n\nELLIPSE");
ellipse(120,350,0,360,30,20);
pieslice(400, 350, 0, 120, 150);
sector(500, 420, 0, 150, 50, 65);
drawpoly(4, poly);
getch();
closegraph();
return 0;
}

ARYAMAN 017202902719 CSE 2ND YR 7


ARYAMAN 017202902719 CSE 2ND YR 8
2.AIM: To execute various functions for Style and Colour settings.

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

int main()
{
int gd = DETECT, gm,color;
char a[10];
int poly[] = {190, 80, 210, 240,300, 270, 190, 80};
int x_circle = 350,y_circle = 350,radius=30,font=6,direction=0,fontsize=3;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
setbkcolor(BROWN);
setfillstyle(SOLID_FILL,RED);
circle(x_circle,y_circle,radius);
floodfill(x_circle,y_circle,WHITE);
settextstyle(font, direction, fontsize);
outtextxy(80,50,"Outtextxy running");
outtext("Outtext running");
setcolor(BLUE);
circle(250,400,radius);
fillellipse(450, 200, 30, 50);
delay(4000);
fillpoly(4,poly);
setlinestyle(DASHED_LINE, 0, 1);
line(100,100,175,175);
color=getcolor();
sprintf(a,"The value of the current drawing color is %d",color);
outtextxy(80,80,a);
getch();
closegraph();
return 0;
}

ARYAMAN 017202902719 CSE 2ND YR 9


ARYAMAN 017202902719 CSE 2ND YR
10
3.Aim: To draw hut, a boy walking in rain and a fish/car using graphics
functions.
// WRITE A PROGRAM TO SHOW MOVING FISH
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<dos.h>
#include<graphics.h>

void main()
{
int gd=DETECT,gm,k,l,count=0;
int x=10,y=200,x1=675,y1=380;
int stangle=35,endangle=140,radius=90;

initgraph(&gd,&gm,"c:\\TurboC3\\BGI");
setbkcolor(BLUE);
while(!kbhit())
{
cleardevice();

setbkcolor(BLUE);
if(x<640)
{
x+=5;
y+=1;
arc(x,y,stangle,endangle+35,radius);
arc(x,y‐110,190,323,radius+2);
circle(x+40,y‐60,5);
line(x‐90,y‐90,x‐90,y‐8);
}
else
{
x1‐=5;
y1‐=1;
arc(x1,y1,stangle‐30,endangle+4,radius);
arc(x1,y1‐110,217,350,radius+2);
circle(x1‐40,y1‐60,5);
line(x1+90,y1‐90,x1+90,y1‐10);
}
setcolor(YELLOW);
delay(90);
}
closegraph();
}

ARYAMAN 017202902719 CSE 2ND YR


11
// WRITE A PROGRAM TO SHOW RAINING AND A PERSON HOLDING AN UMBRELLA.
#include<stdio.h>
#include<graphics.h>
#include<dos.h>
#include<conio.h>
#include <stdlib.h>

#define ScreenWidth getmaxx()


#define ScreenHeight getmaxy()
#define GroundY ScreenHeight*0.75

int ldisp=0;

void DrawManAndUmbrella(int x,int ldisp)


{
//head
circle(x,GroundY‐90,10);
line(x,GroundY‐80,x,GroundY‐30);
//hand
line(x,GroundY‐70,x+10,GroundY‐60);
line(x,GroundY‐65,x+10,GroundY‐55);
line(x+10,GroundY‐60,x+20,GroundY‐70);
line(x+10,GroundY‐55,x+20,GroundY‐70);
//legs
line(x,GroundY‐30,x+ldisp,GroundY);
line(x,GroundY‐30,x‐ldisp,GroundY);
//umbrella
pieslice(x+20,GroundY‐120,0,180,40);
line(x+20,GroundY‐120,x+20,GroundY‐70);
}

void Rain(int x)
{
int i,rx,ry;
for(i=0;i<400;i++)
{
rx=rand() % ScreenWidth;
ry=rand() % ScreenHeight;
if(ry<GroundY‐4)
{
if(ry<GroundY‐120 || (ry>GroundY‐120 && (rx<x‐20 || rx>x+60)))
line(rx,ry,rx+0.5,ry+4);
}
}
}
void main()
{

ARYAMAN 017202902719 CSE 2ND YR


12
int gd=DETECT,gm,x=0;
//Change BGI directory according to yours
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
while(!kbhit())
{
//Draw Ground
line(0,GroundY,ScreenWidth,GroundY);
Rain(x);
ldisp=(ldisp+2)%20;
DrawManAndUmbrella(x,ldisp);
delay(75);
cleardevice();
x=(x+2)%ScreenWidth;
}
getch();
}

ARYAMAN 017202902719 CSE 2ND YR


13
ARYAMAN 017202902719 CSE 2ND YR
14
ARYAMAN 017202902719 CSE 2ND YR
15
4. AIM: Write a program to implement DDA Line drawing algorithms.

THEORY:

DDA Algorithm:

Step1: Start Algorithm

Step2: Declare x1,y1,x2,y2,dx,dy,x,y as integer variables.

Step3: Enter value of x1,y1,x2,y2.

Step4: Calculate dx = x2‐x1

Step5: Calculate dy = y2‐y1

Step6: If ABS (dx) > ABS (dy)


Then step = abs (dx)
Else

Step=abs(dy)

Step7: xinc=dx/step
yinc=dy/step
assign x = x1
assign y = y1

Step8: Set pixel (x, y)

Step9: x = x + xinc
y = y + yinc
Set pixels (Round (x), Round (y))

Step10: Repeat step 9 until x = x2

Step11: End Algorithm

ARYAMAN 017202902719 CSE 2ND YR


16
//Write Program which draw the line for both cases‐ Slope(m)>1 and
//Slope(m)<1.

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

void main()
{
int gd = DETECT ,gm, i;
float x, y,dx,dy,steps;
int x0, x1, y0, y1,x2,y2,x3,y3;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
setbkcolor(BLACK);
outtextxy(10,300,"Lines drawn using DDA Algorithm");
x0 = 50 , y0 = 150, x1 = 450, y1 = 250; //SLOPE < 1
x2= 125,y2=100,x3=175,y3=275; //SLOPE > 1
dx = (float)(x1 ‐ x0);
dy = (float)(y1 ‐ y0);
if(dx>=dy)
{
steps = dx;
}
else
{
steps = dy;
}
dx = dx/steps;
dy = dy/steps;
x = x0;
y = y0;
i = 1;
while(i<= steps)
{
putpixel(x, y, WHITE);
x += dx;
y += dy;
i=i+1;
}
dx = (float)(x3 ‐ x2);
dy = (float)(y3 ‐ y2);
if(dx>=dy)
{
steps = dx;
}
else
{

ARYAMAN 017202902719 CSE 2ND YR


17
steps = dy;
}
dx = dx/steps;
dy = dy/steps;
x = x2;
y = y2;
i = 1;
while(i<= steps)
{
putpixel(x, y, WHITE);
x += dx;
y += dy;
i=i+1;
}
getch();
closegraph();
}

/*Case 1: Take endpoints of a line such that both endpoints of have same
coordinates (x0=x1=y0=y1).

Case 2: Take endpoints (x0, y0, x1, y1) of a line such that X coordinates of
both endpoints are same (x0= x1) but different values of Y coordinates.

Case 3: Take endpoints (x0, y0, x1, y1) of a line such that Y coordinates of
both endpoints are same (y0= y1) but different values of X coordinates.

Case 4: Take endpoints (x0, y0, x1, y1) of a line such Slope of line is
Positive and greater than 1.

Case 5: Take endpoints (x0, y0, x1, y1) of a line such Slope of line is
Negative and greater than 1.

Case 6: Take endpoints (x0, y0, x1, y1) of a line such Slope of line is
Positive and less than 1.

Case 7: Take endpoints (x0, y0, x1, y1) of a line such Slope of line is
Negative and less than 1(m<1).*/

ARYAMAN 017202902719 CSE 2ND YR


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

void main()
{
int gd = DETECT ,gm, i;
float x, y,dx,dy,steps;
int x0, x1, y0, y1;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
setbkcolor(BLACK);
outtextxy(10,300,"Lines drawn using DDA Algorithm");
for(int j =0;j<6;j++){
clrscr();
printf("Enter x0,x1,y0,y1 for line:\n");
scanf("%d %d %d %d",&x0,&x1,&y0,&y1);
dx = (float)(x1 ‐ x0);
dy = (float)(y1 ‐ y0);
if(dx>=dy)
{
steps = dx;
}
else
{
steps = dy;
}
dx = dx/steps;
dy = dy/steps;
x = x0;
y = y0;
i = 1;
while(i<= steps)
{
putpixel(x, y,RED);
x += dx;
y += dy;
i=i+1;
}
getch();
}

closegraph();
}

ARYAMAN 017202902719 CSE 2ND YR


19
CASE 1:

ARYAMAN 017202902719 CSE 2ND YR


20
CASE 2:

CASE 3:

ARYAMAN 017202902719 CSE 2ND YR


21
CASE 4:

CASE 5:

ARYAMAN 017202902719 CSE 2ND YR


22
CASE 6:

CASE 7:

ARYAMAN 017202902719 CSE 2ND YR


23
5. AIM: Write a program to implement Bresenham’s Line drawing algorithms.
THOERY:
1. Write Algorithm which draw the line for both cases‐ Slope(m)>1 and
Slope(m)<1.

Ans:
 Step 1 : Except the two end points of Line from User.
 Step 2 : Calculate the slope(m) of the required Line.
 Step 3 : Identify the value of slope(m).
 Step 3.1 : If slope(m) is Less than 1 i.e: m < 1
* Step 3.1.1 : Calculate the constants dx, dy, 2dy, and (2dy – 2dx) and get the first value
for the decision parameter as ‐
* p0 = 2dy − dx
* Step 3.1.2 : At each Xk along the line, starting at k = 0, perform the following test −
* If pk < 0, the next point to plot is (xk + 1,yk) and
pk+1 = pk + 2dy
else
* plot (xk,yk + 1)
* pk+1 = pk + 2dy − 2dx
* Step 3.1.3 : Repeat step 4(dx ‐ 1) times.
 Step 3.2 : If slope(m) is greater than or equal to 1 i.e: m >= 1
* Step 3.2.1 : Calculate the constants dx, dy, 2dy, and (2dy – 2dx) and get the first value
for the decision parameter as ‐
* p0 = 2dx − dy
* Step 3.2.2 : At each Yk along the line, starting at k = 0, perform the following test −
* If pk < 0, the next point to plot is (xk,yk + 1) and
pk+1 = pk + 2dx
else
* plot (xk + 1,yk)
* pk+1 = pk + 2dx − 2dy
* Step 3.2.3 : Repeat step 4(dy ‐ 1) times.
 Step 3.3 : Exit.

ARYAMAN 017202902719 CSE 2ND YR


24
/* Write Program which draw the line for both cases‐ Slope(m)>1 and
Slope(m)<1.
3. Display text on the top of screen mentioning “Lines drawn using
Bresenham’s Algorithm”, using outtextxy function.*/

#include<stdio.h>
#include<graphics.h>
#include<conio.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;
}
getch();
}
int main()
{
int gdriver=DETECT, gmode, error, x0, y0, x1, y1;
initgraph(&gdriver, &gmode, "C:\\TURBOC3\\BGI");
outtextxy(350,30,"Lines drawn using Bresenham's Algo");
//FOR LINE OF SLOPE M<1
printf("Enter co‐ordinates of first point:");
scanf("%d %d", &x0, &y0);
printf("Enter co‐ordinates of second point: ");
scanf("%d %d", &x1, &y1);
drawline(x0, y0, x1, y1);
//FOR LINE OF SLOPE M>1

printf("Enter co‐ordinates of first point:");


scanf("%d %d", &x0, &y0);
printf("Enter co‐ordinates of second point: ");

ARYAMAN 017202902719 CSE 2ND YR


25
scanf("%d %d", &x1, &y1);
drawline(x0, y0, x1, y1);
return 0;

}
/*
Case 1: Take endpoints of a line such that both endpoints of have same
coordinates (x0=x1=y0=y1).

Case 2: Take endpoints (x0, y0, x1, y1) of a line such that X
coordinates of both endpoints are same (x0= x1) but different values of
Y coordinates.

Case 3: Take endpoints (x0, y0, x1, y1) of a line such that Y
coordinates of both endpoints are same (y0= y1) but different values of
X coordinates.

Case 4: Take endpoints (x0, y0, x1, y1) of a line such Slope of line is
Positive and greater than 1.

Case 5: Take endpoints (x0, y0, x1, y1) of a line such Slope of line is
Negative and greater than 1.

Case 6: Take endpoints (x0, y0, x1, y1) of a line such Slope of line is
Positive and less than 1.

Case 7: Take endpoints (x0, y0, x1, y1) of a line such Slope of line is
Negative and less than 1(m<1).*/

ARYAMAN 017202902719 CSE 2ND YR


26
#include<stdio.h>
#include<graphics.h>
#include<conio.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,RED);
y=y+1;
p=p+2*dy‐2*dx;
}
else
{
putpixel(x,y,RED);
p=p+2*dy;
}
x=x+1;
}
getch();
}
int main()
{
int gdriver=DETECT, gmode, error, x0, y0, x1, y1;
initgraph(&gdriver, &gmode, "C:\\TURBOC3\\BGI");

for(int i=0;i<6;i++){
clrscr();
printf("Enter co‐ordinates of first point(x0,y0):\n");
scanf("%d %d", &x0, &y0);
printf("Enter co‐ordinates of second point(x1,y1):\n");
scanf("%d %d", &x1, &y1);
drawline(x0, y0, x1, y1);

return 0;
}

ARYAMAN 017202902719 CSE 2ND YR


27
CASE 1:

ARYAMAN 017202902719 CSE 2ND YR


28
CASE 2:

CASE 3:

ARYAMAN 017202902719 CSE 2ND YR


29
CASE 4:

CASE 5:

ARYAMAN 017202902719 CSE 2ND YR


30
CASE 6:

CASE 7:

ARYAMAN 017202902719 CSE 2ND YR


31
6.Aim: Write a program to implement Bresenham’s Circle drawing algorithms.
Theory:
Converting a circle using Bresenham's algorithm works as follows: Points are generated from
90° to 45°, moves will be made only in the +x & ‐y directions as shown in fig:

The best approximation of the true circle will be described by those pixels in the raster that
falls the least distance from the true circle. We want to generate the points from

90° to 45°. Assume that the last scan‐converted pixel is P1 as shown in fig. Each new point
closest to the true circle can be found by taking either of two actions.

1. Move in the x‐direction one unit or


2. Move in the x‐ direction one unit & move in the negative y‐direction one unit.

ARYAMAN 017202902719 CSE 2ND YR


32
Let D (Si) is the distance from the origin to the true circle squared minus the distance to point
P3 squared. D (Ti) is the distance from the origin to the true circle squared minus the distance
to point P2 squared. Therefore, the following expressions arise.

D (Si)=(xi‐1+1)2+ yi‐12 ‐r2


D (Ti)=(xi‐1+1)2+(yi‐1 ‐1)2‐r2

Since D (Si) will always be +ve & D (Ti) will always be ‐ve, a decision variable d may be
defined as follows:

di=D (Si )+ D (Ti)

Therefore,
di=(xi‐1+1)2+ yi‐12 ‐r2+(xi‐1+1)2+(yi‐1 ‐1)2‐r2

From this equation, we can drive initial values of di as

If it is assumed that the circle is centered at the origin, then at the first step x = 0 & y = r.

Therefore,
di=(0+1)2+r2 ‐r2+(0+1)2+(r‐1)2‐r2
=1+1+r2‐2r+1‐r2
= 3 ‐ 2r

Thereafter, if d_i<0,then only x is incremented.

xi+1=xi+1 di+1=di+ 4xi+6

ARYAMAN 017202902719 CSE 2ND YR


33
& if di≥0,then x & y are incremented
xi+1=xi+1 yi+1 =yi+ 1
di+1=di+ 4 (xi‐yi)+10

Algorithm:
Step1: Start Algorithm

Step2: Declare p, q, x, y, r, d variables


p, q are coordinates of the center of the circle
r is the radius of the circle

Step3: Enter the value of r

Step4: Calculate d = 3 ‐ 2r

Step5: Initialize x=0


&nbsy= r

Step6: Check if the whole circle is scan converted


If x > = y
Stop

Step7: Plot eight points by using concepts of eight‐way symmetry. The center is at (p, q).
Current active pixel is (x, y).
putpixel (x+p, y+q)
putpixel (y+p, x+q)
putpixel (‐y+p, x+q)
putpixel (‐x+p, y+q)
putpixel (‐x+p, ‐y+q)
putpixel (‐y+p, ‐x+q)
putpixel (y+p, ‐x+q)
putpixel (x+p, ‐y‐q)

Step8: Find location of next pixels to be scanned


If d < 0
then d = d + 4x + 6
increment x = x + 1
If d ≥ 0
then d = d + 4 (x ‐ y) + 10
increment x = x + 1
decrement y = y ‐ 1

Step9: Go to step 6

Step10: Stop Algorithm

ARYAMAN 017202902719 CSE 2ND YR


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

void EightWaySymmetricPlot(int xc,int yc,int x,int y)


{
putpixel(x+xc,y+yc,RED);
putpixel(x+xc,‐y+yc,YELLOW);
putpixel(‐x+xc,‐y+yc,GREEN);
putpixel(‐x+xc,y+yc,YELLOW);
putpixel(y+xc,x+yc,12);
putpixel(y+xc,‐x+yc,14);
putpixel(‐y+xc,‐x+yc,15);
putpixel(‐y+xc,x+yc,6);
}

void BresenhamCircle(int xc,int yc,int r)


{
int x=0,y=r,d=3‐(2*r);
EightWaySymmetricPlot(xc,yc,x,y);

while(x<=y)
{
if(d<=0)
{
d=d+(4*x)+6;
}
else
{
d=d+(4*x)‐(4*y)+10;
y=y‐1;
}
x=x+1;
EightWaySymmetricPlot(xc,yc,x,y);
}
}

int main(void)
{
/* request auto detection */
int xc,yc,r,gdriver = DETECT, gmode, errorcode;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "C:\\TURBOC3\\BGI");

/* read result of initialization */


errorcode = graphresult();

ARYAMAN 017202902719 CSE 2ND YR


35
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
printf("Enter the values of xc and yc :");
scanf("%d%d",&xc,&yc);
printf("Enter the value of radius :");
scanf("%d",&r);
BresenhamCircle(xc,yc,r);

getch();
closegraph();
return 0;
}

ARYAMAN 017202902719 CSE 2ND YR


36
7.Aim: Write a program to implement Mid‐Point Circle drawing algorithms

Theory:

It is based on the following function for testing the spatial relationship between the arbitrary point (x,
y) and a circle of radius r centered at the origin:

Now, consider the coordinates of the point halfway between pixel T and pixel S

This is called midpoint (xi+1,yi‐ ) and we use it to define a decision parameter:

Pi=f (xi+1,yi‐ ) = (xi+1)2+(yi‐ )2‐r2 ...............equation 2

If Pi is ‐ve ⟹midpoint is inside the circle and we choose pixel T

If Pi is+ve ⟹midpoint is outside the circle (or on the circle)and we choose pixel S.

The decision parameter for the next step is:

Pi+1=(xi+1+1)2+(yi+1‐ )2‐ r2............equation 3

ARYAMAN 017202902719 CSE 2ND YR


37
Since xi+1=xi+1, we have

If pixel T is choosen ⟹Pi<0

We have yi+1=yi

If pixel S is choosen ⟹Pi≥0

We have yi+1=yi‐1

We can continue to simplify this in n terms of (xi,yi) and get

Now, initial value of Pi (0,r)from equation 2

We can put ≅1
∴r is an integer
So, P1=1‐r

ARYAMAN 017202902719 CSE 2ND YR


38
Algorithm:

Step1: Put x =0, y =r in equation 2


We have p=1‐r

Step2: Repeat steps while x ≤ y


Plot (x, y)
If (p<0)
Then set p = p + 2x + 3
Else
p = p + 2(x‐y)+5
y =y ‐ 1 (end if)
x =x+1 (end loop)

Step3: End

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

class bresen
{
float x, y,a, b, r, p;
public:
void get ();
void cal ();
};
void main ()
{
bresen b;
b.get ();
b.cal ();
getch ();
}
Void bresen :: get ()
{
cout<<"ENTER CENTER AND RADIUS";
cout<< "ENTER (a, b)";
cin>>a>>b;
cout<<"ENTER r";

ARYAMAN 017202902719 CSE 2ND YR


39
cin>>r;
}
void bresen ::cal ()
{
/* request auto detection */
int gdriver = DETECT,gmode, errorcode;
int midx, midy, i;
/* initialize graphics and local variables */
initgraph (&gdriver, &gmode, " ");
/* read result of initialization */
errorcode = graphresult ();
if (errorcode ! = grOK) /*an error occurred */
{
printf("Graphics error: %s \n", grapherrormsg (errorcode);
printf ("Press any key to halt:");
getch ();
exit (1); /* terminate with an error code */
}
x=0;
y=r;
putpixel (a, b+r, RED);
putpixel (a, b‐r, RED);
putpixel (a‐r, b, RED);
putpixel (a+r, b, RED);
p=5/4)‐r;
while (x<=y)
{
If (p<0)
p+= (4*x)+6;
else
{
p+=(2*(x‐y))+5;
y‐‐;
}
x++;
putpixel (a+x, b+y, RED);
putpixel (a‐x, b+y, RED);
putpixel (a+x, b‐y, RED);
putpixel (a+x, b‐y, RED);
putpixel (a+x, b+y, RED);
putpixel (a+x, b‐y, RED);
putpixel (a‐x, b+y, RED);
putpixel (a‐x, b‐y, RED);
}
}

ARYAMAN 017202902719 CSE 2ND YR


40
ARYAMAN 017202902719 CSE 2ND YR
41
8.Aim:Write a program to perform 2D Translation and Scaling on line, circle and polygon.

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

int x,y,r,midx,midy;
void axis();

void translation()
{
int tx,ty,xn1,yn1,xn2,yn2;
printf("\n Enter the translation:");
scanf("%d%d",&tx,&ty);
cleardevice();
outtextxy(400,100,"TRANSLATION");
axis();
xn1=x+tx;
yn1=y+ty;
circle(xn1,yn1,r);
getch();
}
void translateLine ( int P[][2], int T[])
{
setcolor (2);
line(P[0][0], P[0][1], P[1][0], P[1][1]);
P[0][0] = P[0][0] + T[0];
P[0][1] = P[0][1] + T[1];
P[1][0] = P[1][0] + T[0];
P[1][1] = P[1][1] + T[1];
setcolor(3);
line(P[0][0], P[0][1], P[1][0], P[1][1]);
closegraph();
}
void translateRectangle ( int P[][2], int T[])
{
setcolor (2);
rectangle (P[0][0], P[0][1], P[1][0], P[1][1]);
// calculating translated coordinates
P[0][0] = P[0][0] + T[0];
P[0][1] = P[0][1] + T[1];
P[1][0] = P[1][0] + T[0];
P[1][1] = P[1][1] + T[1];
rectangle (P[0][0], P[0][1], P[1][0], P[1][1]);
}

ARYAMAN 017202902719 CSE 2ND YR


42
void scaling()
{
float sc,r1;
printf("Enter the scaling factor");
scanf("%f",&sc);
cleardevice();
outtextxy(300,200,"SCALING");
r1=r*sc;
axis();
circle(midx,midy,r1);
getch();
}

void get()
{
printf("\n Enter the Raidus(r): ");
scanf("%d",&r);
outtextxy(200,100,"ORIGINAL OBJECT");
axis();
getch();
}

void axis()
{
midx=x= getmaxx() / 2;
midy=y= getmaxy() / 2;
line(0,midy,midx*2,midy);
line(midx,0,midx,midy*2);
circle(midx,midy,r);
}
void findNewCoordinate(int s[][2], int p[][1])
{
int temp[2][1] = { 0 };

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


for (int j = 0; j < 1; j++)
for (int k = 0; k < 2; k++)
temp[i][j] += (s[i][k] * p[k][j]);

p[0][0] = temp[0][0];
p[1][0] = temp[1][0];
}

// Scaling the Polygon


void scale(int x[], int y[], int sx, int sy)
{
// Triangle before Scaling
line(x[0], y[0], x[1], y[1]);

ARYAMAN 017202902719 CSE 2ND YR


43
line(x[1], y[1], x[2], y[2]);
line(x[2], y[2], x[0], y[0]);

// Initializing the Scaling Matrix.


int s[2][2] = { sx, 0, 0, sy };
int p[2][1];

// Scaling the triangle


for (int i = 0; i < 3; i++)
{
p[0][0] = x[i];
p[1][0] = y[i];

findNewCoordinate(s, p);

x[i] = p[0][0];
y[i] = p[1][0];
}

// Triangle after Scaling


line(x[0], y[0], x[1], y[1]);
line(x[1], y[1], x[2], y[2]);
line(x[2], y[2], x[0], y[0]);
}

void main()
{
int ch,gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
int P[2][2] = {5, 8, 12, 18}; // coordinates of point
int T[] = {2, 1}; // translation factor
int x[] = { 100, 200, 300 };
int y[] = { 200, 100, 200 };
int sx = 2, sy = 2;
scale(x, y, sx,sy);
translateRectangle (P, T);
translateLine (P, T);
get();
do
{
cleardevice();
outtextxy(10,10,"1)TRANSLATION");
outtextxy(10,20,"2)SCALING");
outtextxy(10,30,"6)EXIT");
outtextxy(10,40,"ENTER UR CHOICE:");
scanf("%d",&ch);
switch(ch)

ARYAMAN 017202902719 CSE 2ND YR


44
{
case 1:
translation();
break;
case 2:
scaling();
break;
case 3:
exit(0);
}
}while(ch<3);
}

Output:

ARYAMAN 017202902719 CSE 2ND YR


45
ARYAMAN 017202902719 CSE 2ND YR
46
9.AIM: Write a program to perform 2D Rotation on line, ellipse and polygon.
POLYGON
#include<stdio.h>
#include<graphics.h>
#include<math.h>
main()
{
intgd=0,gm,x1,y1,x2,y2,x3,y3;
double s,c, angle;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
setcolor(RED);
printf("Enter coordinates of triangle: ");
scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2, &x3, &y3);
setbkcolor(WHITE);
cleardevice();
line(x1,y1,x2,y2);
line(x2,y2, x3,y3);
line(x3, y3, x1, y1);
getch();
setbkcolor(BLACK);
printf("Enter rotation angle: ");
scanf("%lf", &angle);
setbkcolor(WHITE);
c = cos(angle *M_PI/180);
s = sin(angle *M_PI/180);
x1 = floor(x1 * c + y1 * s);
y1 = floor(‐x1 * s + y1 * c);
x2 = floor(x2 * c + y2 * s);
y2 = floor(‐x2 * s + y2 * c);
x3 = floor(x3 * c + y3 * s);
y3 = floor(‐x3 * s + y3 * c);
cleardevice();
line(x1, y1 ,x2, y2);
line(x2,y2, x3,y3);
line(x3, y3, x1, y1);
getch();
closegraph();
return 0;
}

ARYAMAN 017202902719 CSE 2ND YR


47
ARYAMAN 017202902719 CSE 2ND YR
48
LINE

#include<stdio.h>
#include<graphics.h>
#include<math.h>
int main()
{
intgd=0,gm,x1,y1,x2,y2;
double s,c, angle;
initgraph(&gd, &gm, "C:\\TC\\BGI");
setcolor(RED);
printf("Enter coordinates of line: ");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
cleardevice();
setbkcolor(WHITE);
line(x1,y1,x2,y2);
getch();
setbkcolor(BLACK);
printf("Enter rotation angle: ");
scanf("%lf", &angle);
setbkcolor(WHITE);
c = cos(angle *3.14/180);
s = sin(angle *3.14/180);
x1 = floor(x1 * c + y1 * s);
y1 = floor(‐x1 * s + y1 * c);
x2 = floor(x2 * c + y2 * s);
y2 = floor(‐x2 * s + y2 * c);
cleardevice();
line(x1, y1 ,x2, y2);
getch();
closegraph();
return 0;
}

ARYAMAN 017202902719 CSE 2ND YR


49
ARYAMAN 017202902719 CSE 2ND YR
50
ELLIPSE

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

int check(int,char);
void Initialize(void);void drawEllipse1(int color);
void drawEllipse1(int color);
void drawEllipse2(int color);
void main() {
int color, color1;
char ch;

Initialize();
ch = 'R';color1=1;color=1;
do {
if (ch == 'R' || ch == 'L') {
if (ch == 'L') {
if (color1 == 4) color = 1;
else color = ++color1;
}
drawEllipse1(color);
color1 = color;
color = check(color, ch);
drawEllipse2(color);
}
ch = toupper(getche());
} while (ch!='X');
closegraph();
}

void Initialize(void) {
int driver = DETECT,mode, errorcode;
initgraph(&driver,&mode, "C:\\TURBOC3\\BGI");
errorcode = graphresult();
if (errorcode != grOk) {
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("<Copy egavga.bgi to C:\\TURBOC3\\BGI");
getch();
exit(1);
}
}

void drawEllipse1(int color) {


setfillstyle(SOLID_FILL,color);

ARYAMAN 017202902719 CSE 2ND YR


51
setcolor(color);
fillellipse((getmaxx()/2),(getmaxy()/2), 25, 50);
}

void drawEllipse2(int color) {


setfillstyle(SOLID_FILL,color);
setcolor(color);
fillellipse(((getmaxx()/2)),(getmaxy() / 2),50,25);
}

int check(int color,char ch) {


if (ch == 'R') {
if (color < 4)
++color;
else if (color == 4)
color = 1;
}
if (ch == 'L') {
if (color == 4)
color = 1;
else color++;
}
return color;
}

ARYAMAN 017202902719 CSE 2ND YR


52
10 AIM:Write a program to perform 2D Shearing on line and square.

#include<iostream.h>
#include<graphics.h>
#include<math.h>
#include<conio.h>
#include<dos.h>

void mul(int mat[3][3],int vertex[10][3],int n);


void shear(int vertex[10][3],int n);
void init(int vertex[10][3],int n);

int main()
{
int i,x,y;
int vertex[10][3],n;
clrscr();
cout<<"\nEnter the no. of vertex : ";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter the points (x,y): ";
cin>>x>>y;
vertex[i][0]=x;
vertex[i][1]=y;
vertex[i][2]=1;
}
shear(vertex,n);

getch();
return 0;
}
void init(int vertex[10][3],int n)
{
int gd=DETECT,gm,i;
initgraph(&gd,&gm,"C:\\turboc3\\bgi");
setcolor(10);
line(0,240,640,240);
line(320,0,320,480);
setcolor(3);
line(450,20,490,20);
setcolor(15);
line(450,50,490,50);
setcolor(6);
outtextxy(500,20,"Original");
outtextxy(500,50,"Transformed");
setcolor(3);

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

ARYAMAN 017202902719 CSE 2ND YR


53
{
line(320+vertex[i][0],240‐vertex[i][1],320+vertex[i+1][0],240‐
vertex[i+1][1]);
}
line(320+vertex[n‐1][0],240‐vertex[n‐1][1],320+vertex[0][0],240‐
vertex[0][1]);

}
void mul(int mat[3][3],int vertex[10][3],int n)
{
int i,j,k;
int res[10][3];
for(i=0;i<n;i++)
{
for(j=0;j<3;j++)
{
res[i][j]=0;
for(k=0;k<3;k++)
{
res[i][j] = res[i][j] + vertex[i][k]*mat[k][j];
}
}
}
setcolor(15);
for(i=0;i<n‐1;i++)
{
line(320+res[i][0],240‐res[i][1],320+res[i+1][0],240‐res[i+1][1]);
}
line(320+res[n‐1][0],240‐res[n‐1][1],320+res[0][0],240‐res[0][1]);

}
void shear(int vertex[10][3],int n)
{
int opt;
int shear_array[3][3];
cout<<"\n1.x‐shear\n2.y‐shear\nYour Choice: ";
cin>>opt;
switch(opt)
{
case 1: int xsh;
cout<<"\nEnter the x shear : ";
cin>>xsh;
shear_array[0][0]=1;
shear_array[1][0]=xsh;
shear_array[2][0]=0;
shear_array[0][1]=0;
shear_array[1][1]=1;
shear_array[2][1]=0;

ARYAMAN 017202902719 CSE 2ND YR


54
shear_array[0][2]=0;
shear_array[1][2]=0;
shear_array[2][2]=1;
init(vertex,n);
mul(shear_array,vertex,n);
break;
case 2:int ysh;
cout<<"\nEnter the y shear : ";
cin>>ysh;
shear_array[0][0]=1;
shear_array[1][0]=0;
shear_array[2][0]=0;
shear_array[0][1]=ysh;
shear_array[1][1]=1;
shear_array[2][1]=0;
shear_array[0][2]=0;
shear_array[1][2]=0;
shear_array[2][2]=1;
init(vertex,n);
mul(shear_array,vertex,n);
break;
}
}

ARYAMAN 017202902719 CSE 2ND YR


55
ARYAMAN 017202902719 CSE 2ND YR
56
11.AIM:Write a program to perform 3D Translation and Scaling on an object.

#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);

ARYAMAN 017202902719 CSE 2ND YR


57
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();
}

ARYAMAN 017202902719 CSE 2ND YR


58
ARYAMAN 017202902719 CSE 2ND YR
59
12.AIM:Write a program to perform 3D Rotation an object.

#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);

ARYAMAN 017202902719 CSE 2ND YR


60
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();
}

ARYAMAN 017202902719 CSE 2ND YR


61
ARYAMAN 017202902719 CSE 2ND YR
62
ARYAMAN 017202902719 CSE 2ND YR
63
ARYAMAN 017202902719 CSE 2ND YR
64
13.AIM:Write a program to implement Cohen Sutherland line clipping algorithm.
THEORY:

This is one of the oldest and most popular line clipping algorithm. To speed up the process
this algorithm performs initial tests that reduce number of intersections that must be
calculated. It does so by using a 4 bit code called as region code or outcodes. These codes
identify location of the end point of line.

Each bit position indicates a direction, starting from the rightmost position of each bit
indicates left, right, bottom, top respectively.

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.

ARYAMAN 017202902719 CSE 2ND YR


65
#include<stdio.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;

printf("\nEnter x1 and y1\n");


scanf("%d %d",&p1.x,&p1.y);
printf("\nEnter x2 and y2\n");
scanf("%d %d",&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)
{

ARYAMAN 017202902719 CSE 2ND YR


66
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

ARYAMAN 017202902719 CSE 2ND YR


67
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;

ARYAMAN 017202902719 CSE 2ND YR


68
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);
}

ARYAMAN 017202902719 CSE 2ND YR


69
ARYAMAN 017202902719 CSE 2ND YR
70
ARYAMAN 017202902719 CSE 2ND YR
71
14.AIM:Write a program to implement Mid‐point line clipping algorithm.
THEORY:
1) Read two end points of line P1 (x1,y1) and P2 (x2,y2).
2) Read corners of window (Wx1, Wy1) and (Wx2, Wy2).
3) Assign region codes for P1 and P2. A region code is a 4 digit bit code which
indicates one of nine regions having the end point of line.
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)
4) Check visibility of line P1P2
a) If region codes for both end points P1 and P2 are 0 = > Line is completely visible.
Draw Line and Go to Step 3.
b) If region codes for both end points P1 and P2 are not 0 and logical ANDing of them
is also not zero = > Line is completely invisible. Reject line and Go to Step 3.
c) If a) and b) both are not satisfied, line is partially visible.
5) Divide partially visible line segments to equal parts and repeat steps 3 through 5
for both sub divided line segments until you get completely visible and completely
invisible line segments.
6) Stop.

#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:\\tc\\bgi”);
cleardevice();
printf(“ENTER END‐POINT 1 (x,y): “);
scanf(“%d%d”,&p1.x,&p1.y);
printf(“\nENTER END‐POINT 2 (x,y): “);
scanf(“%d%d”,&p2.x,&p2.y);
cleardevice();
drawwindow();

ARYAMAN 017202902719 CSE 2ND YR


72
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)

ARYAMAN 017202902719 CSE 2ND YR


73
{
PT ptemp;
if(p.y<=100) ptemp.code[0]=’1′; else ptemp.code[0]=’0′; if(p.y>=400)
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)
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);
}
// Output:
// ENTER END‐POINT 1 (x,y):100 200
// ENTER END‐POINT 2 (x,y):300 380

ARYAMAN 017202902719 CSE 2ND YR


74
ARYAMAN 017202902719 CSE 2ND YR
75

You might also like