CG - Unit Ii - Polygon, Windowing and Clipping
CG - Unit Ii - Polygon, Windowing and Clipping
CG - Unit Ii - Polygon, Windowing and Clipping
P2
P4
Po
Introduction to polygon
• The line segments which makes up the
polygon boundary are called sides or edges.
• The endpoints of polygon are called the
polygon vertices.
• Simplest polygon is a triangle
Examples of Polygons
Types of polygon
• We can divide polygon into three classes
1. Convex
2. Concave
3. Complex
Convex polygon
• A convex polygon is a simple polygon whose interior is a
convex set.
• Following are properties of a simple polygon
– Every internal angle is less than 180 degrees.
– Every line segment between two vertices remains inside or
on the boundary of the polygon.
Example of Convex Polygon
10/3/2024
Concave Polygon
• A concave polygon will always have an
interior angle greater than 180 degrees.
• It is possible to cut a concave polygon into a
set of convex polygons
x
+1 -1
x
Polygon Filling Algorithm
• Filling polygon means highlighting all the pixels which
lie inside the polygon.
• There are basic two approaches:
– Seed fill
– Scan line algorithm
Polygon Filling Algorithm
• Seed fill:
• Start from a given “seed”.
• A point which is inside the polygon, highlight
outward from this point until it reaches to the
boundary pixel.
• This approach is called as seed filling.
• The seed fill algorithm is further classified into
– Flood fill algorithm
– Boundary fill algorithm
SEED fill
• The seed fill algorithm is further classified as
Seed
Seed
Scan line
Seed fill algorithm
World Coordinates
Windowing
• When we display a scene only those objects
within a particular window are displayed
Window
wymax
wymin
wxmin wxmax
World Coordinates
Windowing
• Because drawing things to a display takes time
we clip everything outside the window
Window
wymax
wymin
wxmin wxmax
World Coordinates
Clipping
• Process to cut off the lines which are outside
the window so that only the lines within the
window will displayed is called “clipping”
• Line is inside the window, then display
• Line is outside do not display
• Line crosses the boundary determine the point
of intersection and draw only the portion
which lies inside.
Clipping
• Clipping refers to the removal of part of a
scene.
• Window : it is the selected area of the picture.
Usually it is rectangular in shape.
• Following are the graphics primitives that we
are going to study under clipping:
✓ point clipping
✓ line clipping
✓ polygon clipping
Point Clipping
• Easy - a point (x,y) is not clipped if:
• wxmin ≤ x ≤ wxmax AND wymin ≤ y ≤ wymax
• otherwise it is clipped
P4 Clipped
Clipped
Window P2
wymax
Clipped
P5
P1
P7 Points Within the Window
are Not Clipped
P9 P8
wymin
Clipped P10
wxmin wxmax
Line Clipping
• For the image below consider which lines and
points should be kept and which ones should
be clipped
P4
Window P2
wymax
P6
P3
P1
P7 P5
P9
P8
wymin
P10
wxmin wxmax
Line Clipping
• Harder - examine the end-points of each line to
see if they are in the window or not
Situation Solution Example
Window
wymax
P6 [0000]
P3 [0001]
P5 [0000] P12 [0010]
P7 [0001]
P9 [0000] P8 [0010]
wymin
P10 [0100]
P13 [0101] P14 [0110]
wxmin wxmax
Cohen-Sutherland: Lines In The Window
Lines completely contained within the window
boundaries have region code [0000] for both
end-points so are not clipped
P11 [1010]
P4 [1000]
Window
wymax
P6 [0000]
P3 [0001]
P5 [0000] P12 [0010]
P7 [0001]
P9 [0000] P8 [0010]
wymin
P10 [0100]
P13 [0101] P14 [0110]
wxmin wxmax
Cohen-Sutherland: Lines Outside The
Window
Any lines with a common set bit in the region
codes of both end-points can be clipped
– The AND operation can efficiently check this
P4 [1000]
P11 [1010] • Result of AND is non zero
line is outside the window.
Window
wymax
P6 [0000]
P3 [0001]
P5 [0000] P12 [0010]
P7 [0001]
P9 [0000] P8 [0010]
wymin
P10 [0100]
P13 [0101] P14 [0110]
wxmin wxmax
Cohen-Sutherland: Other Lines
• Lines for which the AND operation of the end
coordinates is ZERO . These are identified as
partially visible lines.
Cohen-Sutherland: Other Lines
• These lines are processed as follows:
– Find the intersection of those lines with the edges
of window.
– We can use the region codes to determine which
window boundaries should be considered for
intersection.eg: region code 1000 then line
intersects with TOP boundary. Find intersection
with that boundary only.
– Check other end of the line to get other
intersection.
– Draw line between calculated intersection.
Cohen-Sutherland Examples
• Consider the line P9 to P10 below
– Start at P10
Window
– From the region codes wymax
of the two end-points we
know the line doesn’t
P [0000]
cross the left or right wymin
9
P ’ [0000]
boundary 10
– Calculate the
P [0100]
10
wxmin
intersection of the line with the bottom boundarywxto max
Y2 − Y1
Y= (X − X1) + Y1
X2 − X1
Y = mX + b
Where
Y2 − Y1
m=
X2 − X1
b = Y1 - mX1
Y is called slope intercept form of the line
The Slope m is change in height/change in width for two points on the line
b is called intercept ,it is the height at which the line crosses the y Axis.
66
Calculating Line Intersections
Original Area Clip Left Clip Right Clip Top Clip Bottom
Sutherland-Hodgeman Clipping
p s
p s I
Four cases:
1. S inside plane and P inside plane
Save p in the output-List
2. S inside plane and P outside plane
Find intersection point i
Save i in the output-List
3. S outside plane and P outside plane
Save nothing
4. S outside plane and P inside plane
Find intersection point i
Save i in the output-List, followed by P
Algorithm
1. Read polygon vertices
2. Read window coordinates
3. For every edge of window do
4. Check every edge of the polygon to do 4 tests
1. Save the resultant vertices and the intersections in the output list.
2. The resultant set of vertices is then sent for checking against next
boundary.
Window 2
Window 3
Window 1
Viewing transformation
Clipping process
Viewing Transformation
• Picture is stored in the computer memory
using any convenient Cartesian coordinate
system, called as world coordinate
system(WCS).
• When picture is displayed on the display
device it is measured in physical device
coordinate system (PDCS).
Viewing Transformation
• Before displaying it is mapped from WCS to
PDCS.
• This mapping of coordinate is achieved with
the use of coordinate transformation known as
viewing transformation.
• This is performed by
– Normalization transformation
– Workstation transformation
Viewing Transformation
• Normalization transformation:
– Different display device may have different screen
size.
– The device independent units are called as
normalized device coordinate.
– In these unit, the screen measures 1 unit wide and
1 unit in length.
Viewing Transformation
• Normalization transformation:
– The interpreter uses a simple liner formula to convert
the normalized device coordinate to the actual device
coordinates.
– x = x n . Xw
– y = y n . YH
– Where x : Actual device x coordinate
• y : Actual device y coordinate
• xn : Normalized x coordinate
• yn : Normalized y coordinate
• XW : Width of actual screen in pixels
• YH : height of actual screen in pixel
Viewing Transformation
• Normalization transformation:
– The transformation which map the world
coordinate to the normalized device coordinates is
called as normalized transformation .
• Workstation transformation :
• The transformation which map the normalized
device coordinate to the physical device
coordinates is called as workstation transformation
.
Viewing Transformation
• The viewing transformation is the combination of
normalization transformation and workstation
transformation as shown in figure.
• It is given as V= N x W
Normalized Device
World Normalization Workstation
Coordinates transformation transformation Coordinates
Coordinates
(WC) (DC)
(NC)
Viewing Transformation
• To perform viewing transformation we select a
finite world coordinate area called as window.
Xv max − xv min
Sx =
xw max − xw min
Yv max − Yv min
Sy =
Yw max − Yw min
Viewing Transformation
• The workstation transformation is given as
– W= T • S • T-1
– W=
1 0 0 Sx 0 0 1 0 0
W = 0 1 0 • 0 Sy 0 • 0 1 0
− Xw min − yw min 1 0 0 1 Xw min yw min 1
Sx 0 0
W = 0 Sy 0
Xv min − Xw min• Sx Yv min − yw min• Sy 1