You are on page 1of 6

learn with shamim

Home Algorithms >


Algorithms
Learn C# COHEN-SUTHERLAND Algorithm
Windows Phone App
Development
Random Topics
Problem and its Solution Hello everyone, today we will talk about one algorithm that is used in computer graphics for
line clipping. So first we have to know what is clipping and why it is necessary. The dictionary
meaning of the word clipping is “a small piece trimmed from something”. To understand this
clearly we will introduce some coordinate systems that is used in computer graphics-

1. Screen coordinates: It is used for addressing the screen.

2. World coordinates: This is user defined or application specific coordinates.

3. Window: it is a rectangular region of the world which is visible.

4. Viewport: it is rectangular region of the screen that is used to display window.

Now that we understand these coordinate systems clipping can be thought of that process to
select a specific rectangular region in a world that is showed on the screen. We only can see
the clipped part of an object in our monitor. So we have to define an algorithm that can
identify which part of the object is to be shown and which to be rejected. Not every object in
the world coordinate system have to be shown on the screen, so the computers don’t need to
process that object at all. Thus we have to come up with an algorithm that can be applied in
computer to identify these object.

We can understand the coordinate systems discussed above by the following examples-
In the above figure we can see a world with one object. It is not necessary that all time the
whole object in the world will appear in the screen. Sometimes we need to trim a part of the
object to show in the screen and this trimming process is known as clipping. The following
figure shows this with example-

Now that we understand what is clipping we will discuss one very basic algorithm to identify
which part of an object needs to draw on the window. Before going to the algorithm first we
will discuss the basic idea behind the algorithm.

So our job is to identify whether one object lies on the window or not. Three possibilities
here-

The object completely lies on the window.

The object partially lies on the window.

The object is completely outside of the window.

Now if the object is a line we can take decision about the above three cases by finding the
start and ending position of the line and comparing it with the four edges of the window. To
understand this consider the following figure-

In the above figure we can see that the line AB is completely on the window and GH is
completely outside of the window. On the other hand CD line is partially on the window. Our
goal is to find an algorithm that will find that we don’t need to compute for GH line at all and
we will draw the whole AB line and find the E and F point that is the intersecting point of line
CD with the edges of the window and computer will draw the EF line on the window. So our
output will become like the following-
Today we will discuss “COHEN-SUTHERLAND” line clippings algorithm for doing this
clipping. This algorithm 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-

So to define the top-left corner of the region the 4 bit code should be 1001 because the
sequence is top, bottom, right and left. So we have to put one in the first and last bit to define
the top-left corner.
Once the codes for each endpoint of a line are determined, the logical AND opera on of the codes
determines if the line is completely outside of the window. If the logical AND of the endpoint codes
is not zero, the line can be trivially rejected. For example, if an endpoint had a code of 1001 while the
other endpoint had a code of 1010, the logical AND would be 1000 which indicates the line segment
lies outside of the window. On the other hand, if the endpoints had codes of 1001 and 0110, the
logical AND would be 0000, and the line could not be trivially rejected.

The logical OR of the endpoint codes determines if the line is completely inside the window. If the
logical OR is zero, the line can be trivially accepted. For example, if the endpoint codes are 0000 and
0000, the logical OR is 0000 - the line can be trivially accepted. If the endpoint codes are 0000 and
0110, the logical OR is 0110 and the line cannot be trivially accepted.

The formal algorithm can be written as follows-

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 AND operation 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.

Hope this will help to understand the basic of the algorithm.

Sign in | Recent Site Activity | Report Abuse | Print Page | Powered By Google Sites

You might also like