0% found this document useful (0 votes)
37 views16 pages

Unit 4 Notes CGR

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)
37 views16 pages

Unit 4 Notes CGR

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/ 16

Unit 4

Topics and Sub-topics

1. Windowing concepts
2. Line clipping: Cohen Sutherland clipping algorithm, Mid-point
Subdivision Line clipping algorithm
3. Polygon clipping: Sutherland -Hodgeman Polygon clipping algorithm.

Windowing concept

The method of selecting and enlarging a portion of a drawing is called


windowing. The area chosen for this display is called a window. The window is
selected by world-coordinate.

Sometimes we are interested in some portion of the object and not in full
object. So we will decide on an imaginary box. This box will enclose desired or
interested area of the object. Such an imaginary box is called a window.

Clipping:

When we have to display a large portion of the picture, then not only scaling &
translation is necessary, the visible part of picture is also identified. This
process is not easy. Certain parts of the image are inside, while others are
partially inside. The lines or elements which are partially visible will be
omitted.

For deciding the visible and invisible portion, a particular process called
clipping is used. Clipping determines each element into the visible and
invisible portion. Visible portion is selected. An invisible portion is
discarded.

Types of Lines:

Lines are of three types:

1. Visible: A line or lines entirely inside the window is considered visible


2. Invisible: A line entirely outside the window is considered invisible
3. Clipped: A line partially inside the window and partially outside is
clipped. For clipping point of intersection of a line with the window is
determined.

1
• Window –It is the area on the world coordinate selected for display.
• ViewPort –It is the area on the device coordinate where graphics is to
be displayed.

Cohen Sutherland line clipping algorithm


Description:- In this algorithm, we are given 9 regions on the screen. Out of
which one region is of the window and the rest 8 regions are around it given by 4

2
digit binary. The division of the regions are based on (x_max, y_max) and (x_min,
y_min).
The central part is the viewing region or window, all the lines which lie within
this region are completely visible. A region code is always assigned to the
endpoints of the given line.
To check whether the line is visible or not.

Region Codes

Formula to check binary digits:- TBRL which can be defined as top, bottom,
right, and left accordingly.

TBRL Rule

Algorithm
Steps
1) Assign the region codes to both endpoints.
2) Perform OR operation on both of these endpoints.
3) if OR = 0000,
then it is completely visible (inside the window).
else

3
Perform AND operation on both these endpoints.
i) if AND ≠ 0000,
then the line is invisible and not inside the window. Also,
it can’t be considered for clipping.
ii) else
AND = 0000, the line is partially inside the window
and considered for clipping.
4) After confirming that the line is partially inside the window, then we find
the intersection with the boundary of the window. By using the following
formula:-
Slope:- m= (y2-y1)/(x2-x1)
a) If the line passes through top or the line intersects with the top boundary of
the window.
x = x + (y_wmax – y)/m
y = y_wmax
b) If the line passes through the bottom or the line intersects with the bottom
boundary of the window.
x = x + (y_wmin – y)/m
y = y_wmin
c) If the line passes through the left region or the line intersects with the left
boundary of the window.
y = y+ (x_wmin – x)*m
x = x_wmin
d) If the line passes through the right region or the line intersects with the
right boundary of the window.
y = y + (x_wmax -x)*m
x = x_wmax
5) Now, overwrite the endpoints with a new one and update it.
6) Repeat the 4th step till your line doesn’t get completely clipped
Given a set of lines and a rectangular area of interest, the task is to remove lines
that are outside the area of interest and clip the lines which are partially inside
the area.
Input : Rectangular area of interest (Defined by
below four values which are coordinates of
bottom left and top right)
x_min = 4, y_min = 4, x_max = 10, y_max = 8

A set of lines (Defined by two corner coordinates)


line 1 : x1 = 5, y1 = 5, x2 = 7, y2 = 7
Line 2 : x1 = 7, y1 = 9, x2 = 11, y2 = 4

4
Line 2 : x1 = 1, y1 = 5, x2 = 4, y2 = 1

Output : Line 1 : Accepted from (5, 5) to (7, 7)


Line 2 : Accepted from (7.8, 8) to (10, 5.25)
Line 3 : Rejected
Cohen-Sutherland algorithm divides a two-dimensional space into 9 regions and
then efficiently determines the lines and portions of lines that are inside the
given rectangular area.
The algorithm can be outlines as follows:-
Nine regions are created, eight "outside" regions and one
"inside" region.

For a given line extreme point (x, y), we can quickly


find its region's four bit code. Four bit code can
be computed by comparing x and y with four values
(x_min, x_max, y_min and y_max).

If x is less than x_min then bit number 1 is set.


If x is greater than x_max th

en bit number 2 is
set.
If y is less than y_min then bit number 3 is set.
If y is greater than y_max then bit number 4 is set

There are three possible cases for any given line.

5
1. Completely inside the given rectangle : Bitwise OR of region of two
end points of line is 0 (Both points are inside the rectangle)
2. Completely outside the given rectangle : Both endpoints share at
least one outside region which implies that the line does not cross the
visible region. (bitwise AND of endpoints != 0).
3. Partially inside the window : Both endpoints are in different regions.
In this case, the algorithm finds one of the two points that is outside the
rectangular region. The intersection of the line from outside point and
rectangular window becomes new corner point and the algorithm
repeats

Pseudo Code:
Step 1 : Assign a region code for two endpoints of given line.
Step 2 : If both endpoints have a region code 0000
then given line is completely inside.
Step 3 : Else, perform the logical AND operation for both region
codes.
Step 3.1 : If the result is not 0000, then given line is
completely
outside.
Step 3.2 : Else line is partially inside.
Step 3.2.1 : Choose an endpoint of the line
that is outside the given rectangle.
Step 3.2.2 : Find the intersection point of the
rectangular boundary (based on region code).
Step 3.2.3 : Replace endpoint with the intersection point
and update the region code.

6
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

Cohen Sutherland Line Clipping Program

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

void main()
{
int a[4],b[4];
float m,xnew,ynew;
float xl=100,yl=100,xh=300,yh=300,xa=10,ya=200,xb=250,yb=150;
int gd = DETECT,gm;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
setcolor(5);
line(xa,ya,xb,yb);
setcolor(12);
rectangle(xl,yl,xh,yh);
m = (yb-ya)/(xb-xa);

if(xa < xl)


a[3] = 1;
else a[3] = 0;

if(xa>xh)
a[2] = 1;
else a[2] = 0;

if(ya < yl)


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

if (ya > yh)


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

if(xb < xl)


b[3] = 1;
else b[3] = 0;

if(xb>xh)
b[2] = 1;
else b[2] = 0;

if(yb < yl)


b[1] = 1;
else b[1] = 0;

if (yb > yh)


b[0] = 1;
else b[0] = 0;

printf("press a key to continue");

7
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(xl,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);
}
}
getch();
closegraph();
}

CohePr{[

8
Use the Cohen Sutherland algorithm to clip two lines P1(35,10)- P2(65,40) and P3(65,20)-
P4(95,10) against a window A(50,10), B(80,10), C(80,40) and D(50,40).

Mid Point Subdivision Line Clipping


Algorithm:
It is used for clipping line. The line is divided in two parts. Mid points of line is
obtained by dividing it in two short segments. Again division is done, by finding
midpoint. This process is continued until line of visible and invisible category is
obtained. Let (x i,yi) are midpoint

9
x5lie on point of intersection of boundary of window.

Advantage of midpoint subdivision Line Clipping:


It is suitable for machines in which multiplication and division operation is not
possible. Because it can be performed by introducing clipping divides in hardware.

Algorithm of midpoint subdivision Line Clipping:


Step1: Calculate the position of both endpoints of the line

Step2: Perform OR operation on both of these endpoints

Step3: If the OR operation gives 0000


then
Line is guaranteed to be visible
else
Perform AND operation on both endpoints.
If AND ≠ 0000
then the line is invisible
else

10
AND=1000
then the line is clipped case.

Step4: For the line to be clipped. Find midpoint


Xm=(x1+x2)/2
Ym=(y1+y2)/2
Xmis midpoint of X coordinate.
Ymis midpoint of Y coordinate.

Step5: Check each midpoint, whether it nearest to the boundary of a window or


not.

Step6: If the line is totally visible or totally rejected not found then repeat step 1
to 5.

Step7: Stop algorithm.

Example: Window size is (-3, 1) to (2, 6). A line AB is given having co-ordinates
of A (-4, 2) and B (-1, 7). Does this line visible. Find the visible portion of the line
using midpoint subdivision?

Solution:

Step1: Fix point A (-4, 2)

Step2: Find b"=mid of b'and b

11
So (-1, 5) is better than (2, 4)
Find b"&bb"(-1, 5) b (-1, 7)

So B""to B length of line will be clipped from upper side

Now considered left-hand side portion

A and B""are now endpoints

12
Find mid of A and BA (-4, 2) B ""(-1, 6)

13
Sutherland-Hodgeman Polygon Clipping Algorithm:-
A polygon can be clipped by processing its boundary as a whole against
each window edge. This is achieved by processing all polygon vertices
against each clip rectangle boundary in turn. beginning with the original
set of polygon vertices, we could first clip the polygon against the left
rectangle boundary to produce a new sequence of vertices. The new set of
vertices could then be successively passed to a right boundary clipper, a
top boundary clipper and a bottom boundary clipper, as shown in figure
(l). At each step a new set of polygon vertices is generated and passed to
the next window boundary clipper. This is the fundamental idea used in
the Sutherland - Hodgeman algorithm.

The output of the algorithm is a list of polygon vertices all of which are
on the visible side of a clipping plane. Such each edge of the polygon is
individually compared with the clipping plane. This is achieved by
processing two vertices of each edge of the polygon around the clipping
boundary or plane. This results in four possible relationships between

14
the edge and the clipping boundary or Plane. (See Fig. m).

1. If the first vertex of the edge is outside the window boundary and
the second vertex of the edge is inside then the intersection point
of the polygon edge with the window boundary and the second
vertex are added to the output vertex list (See Fig. m (a)).
2. If both vertices of the edge are inside the window boundary, only
the second vertex is added to the output vertex list. (See Fig. m
(b)).
3. If the first vertex of the edge is inside the window boundary and
the second vertex of the edge is outside, only the edge
intersection with the window boundary is added to the output
vertex list. (See Fig. m (c)).
4. If both vertices of the edge are outside the window boundary,
nothing is added to the output list. (See Fig. m (d)).

15
Sutherland-Hodgeman Polygon Clipping Algorithm:-

1. Read coordinates of all vertices of the Polygon.


2. Read coordinates of the dipping window
3. Consider the left edge of the window
4. Compare the vertices of each edge of the polygon, individually
with the clipping plane.
5. Save the resulting intersections and vertices in the new list of
vertices according to four possible relationships between the edge
and the clipping boundary.
6. Repeat the steps 4 and 5 for remaining edges or the clipping
window. Each time the resultant list of vertices is successively
passed to process the next edge of the clipping window.
7. Stop.

16

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy