0% found this document useful (0 votes)
19 views7 pages

14 23203a0061pdf

Uploaded by

devilblood496
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views7 pages

14 23203a0061pdf

Uploaded by

devilblood496
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

DEPARTMENT OF COMPUTER

ENGINEERING

Subject: Computer Graphics Subject Code: 313001


Semester: 03 Course: CO3KA
Laboratory No: V120 Name of Subject Teacher: Mr. Pradeep Shirke
Name of Student: Chintan Kalpesh Gupte Roll ID: 23203A0061

Experiment No 14
Title of Experiment Write a C program for Sutherland Hodgeman Polygon Clipping.

PROGRAM:-
#include<conio.h>

#include<stdio.h>

#include<graphics.h>

#include<math.h>

void bytecode();

void sutherland();

int a[4],b[4]; float

m,xnew,ynew;

float xl = 100, yl = 100, xh = 300, yh = 300,xa =350,ya = 150,xb = 250, yb = 150; void

main()

int gd = DETECT,gm;

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

setcolor(5);
line(xa,ya,xb,yb); setcolor(12);

rectangle(xl,yl,xh,yh); //0001 x<xl 0101 x<xl y<yl 1010 x>xh y>yh

m = (yb-ya)/(xb-xa); //0010 x>xh 0110 x>xh y<yl

bytecode();

sutherland(); getch();

void bytecode()

if(xa < xl) //10<100

a[3] = 1; //1 left else

a[3] = 0;

if(xa>xh) //10>300

a[2] = 1; //0 right else

a[2] = 0;

if(ya < yl) //200<100

a[1] = 1; //0 bottom else

a[1] = 0;

if (ya > yh) //200>300

a[0] = 1; //0 top TBRL 0001 else

a[0] = 0;

if(xb < xl) //250<100

b[3] = 1; //0

else

b[3] = 0;

if(xb>xh) //250>300

b[2] = 1; //0 else

b[2] = 0;

if(yb < yl) // 150<100


b[1] = 1; //0

else b[1] = 0;

if (yb > yh) // 150> 300

b[0] = 1; //0 TBRL 0000

else b[0] = 0;

void sutherland()

printf("press a key to continue");

getch();

if(a[0] == 0 && a[1] == 0 && a[2] == 0 && a[3] == 0 && b[0] == 0 && b[1] == 0
&& b[2] == 0 && b[3] == 0 )

printf("no clipping");

line(xa,ya,xb,yb);

else if(a[0]&&b[0] || a[1]&&b[1] || a[2]&&b[2] || a[3]&&b[3])

clrscr();

printf("line discarded");

rectangle(xl,yl,xh,yh);

else

if(a[3] == 1 && b[3]==0)

ynew = (m * (xl-xa)) + ya;

setcolor(12);
rectangle(xl,yl,xh,yh);

setcolor(0);

line(xa,ya,xb,yb); setcolor(15);

line(xl,ynew,xb,yb);

else if(a[2] == 1 && b[2] == 0)

ynew = (m * (xh-xa)) + ya;

setcolor(12);

rectangle(xl,yl,xh,yh);

setcolor(0);

line(xa,ya,xb,yb); setcolor(15);

line(xh,ynew,xb,yb);

else if(a[1] == 1 && b[1] == 0)

xnew = xa + (yl-ya)/m;

setcolor(0);

line(xa,ya,xb,yb); setcolor(15);

line(xnew,yh,xb,yb);

else if(a[0] == 1 && b[0] == 0)

xnew = xa + (yh-ya)/m;

setcolor(0);

line(xa,ya,xb,yb); setcolor(15);

line(xnew,yh,xb,yb);

}
}

OUTPUT:-

EXERCISE:-
1. Clip the following polygon using Sutherland Hodgeman Polygon Clipping
algorithm.

The Sutherland-Hodgman algorithm is used to clip polygons against a rectangular


clipping window. Here's a concise summary of the process:
1. Define Clipping Window: Identify the coordinates of the clipping window corners,
labeled C1,C2,C3,C4C_1, C_2, C_3, C_4C1,C2,C3,C4 for left, top, right, and bottom
edges respectively.
2. Iterate Over Clipping Edges: Clip the polygon against each edge of the clipping
window in the following order:
o Left Edge C1 to C2 o Top Edge C2 to C3 o Right Edge C3 to C4 o
Bottom Edge C4 to C1
3. Vertex Processing:
o For each edge of the polygon, check each vertex to determine if it is inside or
outside the clipping edge.
o Apply these rules:
 Both Inside: Add the second point to the output list.
 First Outside, Second Inside: Add the intersection point and the second
point to the output list.
 First Inside, Second Outside: Add only the intersection point to the
output list.
 Both Outside: No points are added.
4. Repeat: Use the output from the previous edge as the input for the next edge until all
clipping edges have been processed.

PRACTICAL RELATED QUESTION:-

1. If the first vertex is outside the clipping window and second point is inside the
clipping window, then write which points are added to output vertex list.

When clipping a line segment where the first vertex is outside the clipping window and the
second vertex is inside, the output vertex list will contain the following points:

 Intersection Point: This is the point where the line segment intersects with the
boundary of the clipping window.

 Second Vertex (Inside Point): This is the second endpoint of the line segment that
is inside the clipping window.

In essence, the output will consist of these two points, allowing for the clipped line segment
to be properly represented within the bounds of the clipping window.

2. Write the procedure to clip polygon using Sutherland Hodgeman Polygon


Clipping algorithm

Sutherland-Hodgman Polygon Clipping Algorithm Procedure:-


1. Initialize:
 Set the input polygon as the current polygon.
 Prepare an empty list for output vertices.
2. Loop through Clipping Edges:
▪ For each edge of the clipping window, do the following:

3. Process Each Edge:


 For each edge of the current polygon:
 Determine the position of each vertex (inside or outside the clipping
edge).
 Apply the following rules:
 Both Inside: Add the second vertex to the output list.
 First Inside, Second Outside: Add the intersection point and the second
vertex.
 First Outside, Second Inside: Add the intersection point.
 Both Outside: Add nothing.

4. Update Current Polygon:


▪ Set the output list as the new current polygon for the next clipping edge.
5. Repeat:
▪ Repeat for all clipping edges.
6. Output:
▪ The final output list contains the clipped polygon vertices.

Marks Obtained Dated signature of


Teacher
Process Product Total (25)
Related (10) Related (15)

You might also like