You are on page 1of 14

Terna Engineering College

Computer Engineering Department


Program: Sem IV

Course: Computer Graphics

Faculty:
LAB Manual

PART A
(PART A : TO BE REFFERED BY STUDENTS)

Experiment No.06
A.1 Aim:
Implement Cohen Sutherland Line Clipping Algorithm.

A.2 Prerequisite:
1. C Language.
2. Geometric Concepts.
3. Concept of 2D basic Transformations.
4. Clipping Concepts.

A.3 Outcome:
After successful completion of this experiment students will be able to,

Apply the transformations and clipping algorithms on graphical objects.

A.4 Theory:
Line Clipping

The concept of line clipping is same as point clipping. In line clipping, we will cut the
portion of line which is outside of window and keep only the portion that is inside the
window.

Cohen-Sutherland Line Clippings


This algorithm uses the clipping window as shown in the following figure. The
minimum coordinate for the clipping region is (XWmin,YWmin)(XWmin,YWmin) and
the maximum coordinate for the clipping region is (XWmax,YWmax)(XWmax,YWmax).

We will use 4-bits to divide the entire region. These 4 bits represent the Top, Bottom,
Right, and Left of the region as shown in the following figure. Here, the TOP and LEFT
bit is set to 1 because it is the TOP-LEFT corner.

There are 3 possibilities for the line −

1. Line can be completely inside the window (This line should be accepted).
2. Line can be completely outside of the window (This line will be completely
removed from the region).
3. Line can be partially inside the window (We will find intersection point and
draw only that portion of line that is inside region).

Algorithm
Step 1 − Assign a region code for each endpoints.

Step 2 − If both endpoints have a region code 0000 then accept this line.

Step 3 − Else, perform the logical ANDoperation for both region codes.

Step 3.1 − If the result is not 0000, then reject the line.

Step 3.2 − Else you need clipping.

Step 3.2.1 − Choose an endpoint of the line that is outside the window.

Step 3.2.2 − Find the intersection point at the window boundary (base on
region code).

Step 3.2.3 − Replace endpoint with the intersection point and update the
region code.

Step 3.2.4 − Repeat step 2 until we find a clipped line either trivially
accepted or trivially rejected.

Step 4 − Repeat step 1 for other lines.

Illustration of Line Clipping


Before Clipping

1. Consider the line segment AD.

Point A has an outcode of 0000 and point D has an outcode of 1001. The logical
AND of these outcodes is zero; therefore, the line cannot be trivally rejected.
Also, the logical OR of the outcodes is not zero; therefore, the line cannot be
trivally accepted. The algorithm then chooses D as the outside point (its outcode
contains 1's). By our testing order, we first use the top edge to clip AD at B. The
algorithm then recomputes B's outcode as 0000. With the next iteration of the
algorithm, AB is tested and is trivially accepted and displayed.

2. Consider the line segment EI

Point E has an outcode of 0100, while point I's outcode is 1010. The results of the
trivial tests show that the line can neither be trivally rejected or accepted. Point E
is determined to be an outside point, so the algorithm clips the line against the
bottom edge of the window. Now line EI has been clipped to be line FI. Line FI is
tested and cannot be trivially accepted or rejected. Point F has an outcode of
0000, so the algorithm chooses point I as an outside point since its outcode
is1010. The line FI is clipped against the window's top edge, yielding a new line
FH. Line FH cannot be trivally accepted or rejected. Since H's outcode is 0010, the
next iteration of the algorthm clips against the window's right edge, yielding line
FG. The next iteration of the algorithm tests FG, and it is trivially accepted and
display.
After Clipping
After clipping the segments AD and EI, the result is that only the line segment
AB and FG can be seen in the window.

A.5 Procedure:
Algortihm:

1. Read two End points of the line say p1(x1,y1) and p2(x2,y2)

2. Read two corners(Left-Top, Right-Bottom) of the windw,say(WX1,WY1 and


WX2,WY2);
3. Assign the region codes for two endpoints, P1 and P2 using following stapes:

Initialize code with bits 0000

Set Bit1-if(x<WX1)

Set Bit2-if(x>WX2)

Set Bit1-if(y<WY2)

Set Bit1-if(x>Y1)

4. Check for visibility of Line P1,P2

A) If region codes for both endpoints P1 and P2 are Zero then the line is completely
visible. Hence draw the line and go step 9.

B) If region codes for both endpoints P1 and P2 are not Zero and Logical ANDing
of them is also nonzero then the line is completely invisible. Hence reject the line and
go step 9.

C) If region codes for two endpoints do not satisfy the condition in 4(A) and 4(B)
then the line is partially visible.

5. Determine the interesting edge of the clipping window by inspecting the region
codes of two end points.

6. Divide the line segments considering intersection points.

7. Reject the line segment if any one end point of it appears outsides the clipping
window.

8. Draw the remaining line segments.

9. Stop.
PART B
(PART B : TO BE COMPLETED BY STUDENTS)

(Students must submit the soft copy as per following segments within two hours of the practical.
The soft copy must be uploaded on the Blackboard or emailed to the concerned lab in charge
faculties at the end of the practical in case the there is no Black board access available)

Roll No. C28 Name: Prathmesh Krishna Gaikwad


Class : SE Batch : B-1
Date of Experiment: 29/10/2021 Date of Submission: 29/10/2021
Grade :
B.1 Document created by the student:
(Write the answers to the questions given in section 5.1 during the 2 hours of practical in the lab here)

B.3 Observations and learning:

Program-
OUTPUT-
Getting Input from User-
Before Clipping the Line -

After Clipping the Line -


B.4 Conclusion:
Ans - Cohen Sutherland Line Clipping Algorithm is implement in C.

B.5 Question of Curiosity


Q.1] Implement Cohen Sutherland Line Clipping Algorithm.

Ans –

Step1:Calculate positions of both endpoints of the line

Step2:Perform OR operation on both of these end-points

Step3:If the OR operation gives 0000


Then
line is considered to be visible
else
Perform AND operation on both endpoints
If And ≠ 0000
then the line is invisible
else
And=0000
Line is considered the clipped case.

Step4:If a line is clipped case, find an intersection with boundaries of the window
m=(y2-y1 )(x2-x1)

(a) If bit 1 is "1" line intersects with left boundary of rectangle window
y3=y1+m(x-X1)
where X = Xwmin
where Xwminis the minimum value of X co-ordinate of window

(b) If bit 2 is "1" line intersect with right boundary


y3=y1+m(X-X1)
where X = Xwmax
where X more is maximum value of X co-ordinate of the window

(c) If bit 3 is "1" line intersects with bottom boundary


X3=X1+(y-y1)/m
where y = ywmin
ywmin is the minimum value of Y co-ordinate of the window

(d) If bit 4 is "1" line intersects with the top boundary


X3=X1+(y-y1)/m
where y = ywmax
ywmax is the maximum value of Y co-ordinate of the window

Q.2] What are the three categories of line.

Ans-

1. Visible
2. Not Visible
3. Clipping Case

1. Visible: If a line lies within the window, i.e., both endpoints of the line lies within the window.
A line is visible and will be displayed as it is.

2. Not Visible: If a line lies outside the window it will be invisible and rejected. Such lines will
not display. If any one of the following inequalities is satisfied, then the line is considered
invisible. Let A (x1,y2) and B (x2,y2) are endpoints of line.

xmin,xmax are coordinates of the window.

3. Clipping Case: If the line is neither visible case nor invisible case. It is considered to be
clipped case. First of all, the category of a line is found based on nine regions given below. All
nine regions are assigned codes. Each code is of 4 bits. If both endpoints of the line have end bits
zero, then the line is considered to be visible.

The center area is having the code, 0000, i.e., region 5 is considered a rectangle window.

Q3.] What are the advantages and disadvantages of Cohen Sutherland Line Clipping
Algorithm.

Ans –

Advantages:

1. It calculates end-points very quickly and rejects and accepts lines quickly.
2. It can clip pictures much large than screen size.

Disadvantages:

1. Clipping window region can be rectangular in shape only and no other polygonal shaped
window is allowed.
2. Edges of rectangular shaped clipping window has to be parallel to the x-axis and y-axis.
3. If end points of line segment lies in the extreme limits i.e., one at R.H.S other at L.H.S.,
and on one the at top and other at the bottom (diagonally) then, even if the line doesn’t
pass through the clipping region, it will have logical intersection of 0000 implying that
line segment will be clipped but infect it is not so.

************************

You might also like