Unit 4 Notes CGR
Unit 4 Notes CGR
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
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:
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.
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
4
Line 2 : x1 = 1, y1 = 5, x2 = 4, y2 = 1
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
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
#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>xh)
a[2] = 1;
else a[2] = 0;
if(xb>xh)
b[2] = 1;
else b[2] = 0;
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);
}
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).
9
x5lie on point of intersection of boundary of window.
10
AND=1000
then the line is clipped case.
Step6: If the line is totally visible or totally rejected not found then repeat step 1
to 5.
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:
11
So (-1, 5) is better than (2, 4)
Find b"&bb"(-1, 5) b (-1, 7)
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:-
16