CG - Unit Ii - Polygon, Windowing and Clipping

Download as pdf or txt
Download as pdf or txt
You are on page 1of 93

Unit II

Polygon Windowing and Clipping


• Polygon and Clipping
• Introduction to polygon
• Types: convex, concave and complex.
• Representation of polygon
• Inside test
• Polygon filling algorithms – seed fill, scan line fill and filling
with patterns.
• Windowing and clipping: viewing transformations, 2-D
clipping: Cohen –Sutherland algorithm.
• Polygon clipping: Sutherland Hodgeman algorithm,
generalized clipping
Introduction to polygon
• A polyline is a chain of connected line
segment.
• It is specified by the vertices P0, P1,P2 .. and so
on
• The first vertices is called the initial or starting
point and the last vertex is called the final or
terminal point.
• When starting point and terminal point is same
then it is called polygon.
P1 P3

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

Example of Concave Polygon


Complex Polygon
• A complex polygon is neither convex nor
concave.

Example of Complex Polygon


Polygon Representation
• There are three approaches to represent
polygons according to the graphics system:
– Polygon drawing primitives approach
– Trapezoid primitive approach
– Line and point approach
Polygon Representation
• Polygon drawing primitives approach:

• Using drawing primitive directly draw the polygon image

• This polygon is considered as a single unit.


Polygon Representation
• Trapezoid primitive:
• The trapezoid are drawn by stepping down the line
segment with two vector generator’s and for each scan
line filling all the pixels between them.
• In such system, polygon can be represented as a series of
trapezoid.
• But some devices do not provide any polygon support,
best to represent a polygon as a unit.
Polygon Representation
• Line and point approach:
• This method uses display file structure format.
• Display file is used to store the command necessary for
drawing the line segment.
• It is used in vector display system.
Polygon Representation
• Line and point approach:
• Display file contain series of commands.
• Each display file command contains two fields, an
operation code and operand.
• It uses three separate arrays to store opcode and operand.
Inside Test
• There are two approaches to find the
point is inside the polygon or not:

– Even-Odd Rule Algorithm.


– Winding Number Algorithm
Inside Test
• Even-Odd Rule Algorithm:
• Construct a line from the point
• Count how many intersections of the line segment
with the edges of the polygon.
• If there are odd number of intersections then the point
is inside.
• If there are even number of intersections then the
point outside.
• This algorithm is sometimes also known as the
crossing number algorithm or the even-odd rule
algorithm.
Inside Test
• Even-Odd Rule Algorithm:
• Even number - point outside.
• Odd number - point inside.
Inside Test
• Even-Odd Rule Algorithm:
• If the intersection point is vertex of the polygon??
• Look at the other end points of the two line
segments which meet at this vertex.
• If these points lie on the same side of the
constructed line, then the it is counted as an even
number of vertex.
odd x
Winding no. Method
• Every side has given a no. called winding no.
• Total of this winding no. is called net winding.
• If net winding is zero then point is outside otherwise it is
inside.

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

1) Flood fill:-algorithm that fill interior defined


regions are called flood fill

2) Boundary fill :-those that fill boundary-


defined regions are called boundary fill or
edge fill
Flood fill
• In some cases it is required to fill an area that
is not defined within single color boundary.
• In such cases we can fill areas by replacing a
specified interior color instead of searching for
a boundary color
• This is called flood fill
• Start with seed point and examine the
neighboring pixel, here pixels are checked for
a specified interior color instead of boundary
color and they are replaced by new color
Flood fill
flood_fill(x, y, old_colour , new_colour)
If(getpixel(x, y)=old_colour) then
// getpixel function gives the colour of specified pixel
Putpixel(x,y,new_colour)
// putpixel function draws the pixel with specified colour
flood_fill(x+1,y,old_colour,new_colour)
flood_fill(x-1,y,old_colour,new_colour)
flood_fill(x,y+1,old_colour,new_colour)
flood_fill(x,y-1,old_colour,new_colour)
flood_fill(x+1,y+1,old_colour,new_colour)
flood_fill(x-1,y-1,old_colour,new_colour)
flood_fill(x+1,y-1,old_colour,new_colour)
flood_fill(x-1,y+1,old_colour,new_colour
Return
End;
#include<stdio.h>
#include<graphics.h>
void flood(int,int,int,int);
void main()
{
intgd=DETECT,gm;
initgraph(&gd,&gm,"C:/TURBOC3/bgi");
rectangle(50,50,250,250);
flood(55,55,10,0);
getch();
}
void flood(intx,inty,intfillColor, intdefaultColor)
{
if(getpixel(x,y)==defaultColor)
{
putpixel(x,y,fillColor);
flood(x+1,y,fillColor,defaultColor);
flood(x-1,y,fillColor,defaultColor);
flood(x,y+1,fillColor,defaultColor);
flood(x,y-1,fillColor,defaultColor);
}
}
Boundary fill
• Edges of the polygon are drawn
• Starting with some seed, any point inside the
polygon we examine the neighboring pixels to
check whether the boundary pixel is reached.
• If boundary pixel are not reached, pixel is
highlighted and the process is continued until
boundary pixels are reached.
• Boundary connected regions may be either 4-
connected or 8-connected.
4-connected 8-connected

If a region is 4 connected, If a region is 8 connected,


then every pixel moves in then every pixel moves in
only four direction: left, two horizontal, two
right, up and down vertical and four diagonal
directions
Boundary fill algorithm
• Bounary_fill(x, y, f_colour , b_colour)
• //f_colour→fill colour b_colour→boundary colour
• If(getpixel(x, y)!= b_colour && getpixel(x,y)!= f_colour) then
• // getpixel function gives the colour of specified pixel
• Putpixel(x,y,f_colour)
• // putpixel function draws the pixel with specified colour
• Boundary_fill(x+1,y,f_colour,b_colour)
• Boundary_fill(x,y+1,f_colour,b_colour)
• Boundary_fill(x-1,y,f_colour,b_colour)
• Boundary_fill(x,y-1,f_colour,b_colour)
• Return
• End;
Boundary fill algorithm

Seed

Partial filling resulted using 4-connected algorithm


Boundary fill algorithm

Seed

Filling resulted using 8-connected algorithm


Polygon Filling Algorithm
• Drawbacks of Seed Fill algorithm:-
• May not fill regions correctly if some interior pixels are
already displayed in the fill color.

• Encountering a pixel with the fill color can cause a


recursive branch to terminate, leaving other interior
pixels unfilled.
Polygon Filling Algorithm
Polygon
Filling
Algorithm

Scan line
Seed fill algorithm

Flood fill Boundary fill


SCAN LINE ALGORITHM
Consider only
these sides
scan line

Filling along scan lines Consider only the sides which


intersect the scan line
Polygon Filling Algorithm
• Scan-line algorithm:
• It involves horizontal scanning of the polygon
from its lowermost to its topmost vertex
• Identify which edges intersect the scan-line
• Finally drawing the interior horizontal lines with
the specified fill color.
Polygon Filling Algorithm
• Scan-line algorithm:
• What if the intersection point is vertex??
Polygon Filling Algorithm
• Scan-line algorithm:
• Consider scan line 1, the
other end point B and D lie
on the same side of the scan
line hence there are two
intersections resulting two
pairs: 1-2 and 3-4.
Polygon Filling Algorithm
• Scan-line algorithm:
• Consider scan line 2, the
other end point D and F lie
on the opposite side of the
scan line hence there is
single intersections resulting
two pairs: 1-2 and 3-4.
Polygon Filling Algorithm
• Scan-line algorithm:
• Consider scan line 3, the
other end point E and G lie
on the opposite side of the
scan line hence there are two
intersections resulting two
pairs: 1-2 and 3-4.
Polygon Filling Algorithm
• Scan-line algorithm:
• It is necessary to calculate x intersection points
for scan line with every polygon side.
• By using coherence property we can determine
the x intersection value on the lower scan line if
the x intersection value for current scan line is
known by using
1
xi + 1 = xi −
m
Polygon Filling Algorithm
• Scan-line algorithm:
• As we scan from top to bottom value of y
coordinates between the two scan line changes
by 1
yi + 1 = yi − 1
• It is not necessary to compute the x intersection
for scan line with every polygon side.
• The active edge table will be used to keep track
of the edges that are intersected by the current
scan-line.
• This should also contain ordered edges.
Scan-line algorithm:
1. Read n, number of vertices of polygon.
2. Read x and y coordinates of all vertices in
array x[n] and y[n].
3. Find ymin and ymax.
4. Store initial x value, y1, y2 and slope.
5. Sort the rows of array, edges [n][4] in
descending order of y1, descending order of y2
and ascending order of x
6. Set y = ymax
Scan-line algorithm:
7. Find active edges and update active edge list:
if (y > y2 and y < y1)
{ edge is active }
else
{ edge is not active }
8. Compute x intersect for all active edges
[initially x-intersect is x1] xi+1 = xi -1/ m
9. If x intersect is vertex apply vertex test to
check whether to consider one intersect or two
intersect. Store x-intersect in the array.
Scan-line algorithm:
10. Sort x-intersect array in the ascending order.
11.Extract pairs of intersects from the sorted x-
intersect array.
12.Pass pairs of x values to line drawing
algorithm.
13.Set y = y - 1
14.Repeat step 7 to 13 until y ≥ ymin
15.Stop
Unit II
Polygon Windowing and Clipping
• Polygon and Clipping
• Introduction to polygon
• Types: convex, concave and complex.
• Representation of polygon
• Inside test
• Polygon filling algorithms – seed fill, scan line fill and filling
with patterns.
• Windowing and clipping: viewing transformations, 2-D
clipping: Cohen –Sutherland algorithm.
• Polygon clipping: Sutherland Hodgeman algorithm,
generalized clipping
Windowing
• A scene is made up of a collection of objects
specified in world coordinates

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

Both end-points inside


Don’t clip
the window

One end-point inside the


Must clip
window, one outside

Both end-points outside


Don’t know!
the window
Cohen-Sutherland Clipping Algorithm
• An efficient line clipping algorithm
• The key advantage of the algorithm is that it
vastly reduces the number of line intersections
that must be calculated
Cohen-Sutherland: World Division

• World space is divided into regions based on


the window boundaries
– Each region has a unique four bit region code
– Region codes indicate the position of the regions
with respect to the window
1001 1000 1010
3 2 1 0

above below right left


0000
0001 0010
Window
Region Code Legend

0101 0100 0110


Cohen-Sutherland: Labelling
• Every end-point is labelled with the
appropriate region code
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 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

generate point P10’


– The line P9 to P10’ is completely inside the window so
is retained
Cohen-Sutherland Examples (cont…)

• Consider the line P3 to P4 below


– Start at P4 P ’ [1001]
4
4 P [1000]
Window
– From the region codes wy max

of the two end-points P [0001]


3

we know the line


crosses the left
boundary so calculate wymin
the intersection point to
generate P4’
– The line P3 to P4’ is completely outsidewxthe min
window sowxmax
is clipped
Cohen-Sutherland Examples (cont…)

• Consider the line P7 to P8 below


– Start at P7
Window
– From the two region wymax

codes of the two


P7’ [0000]
end-points we know P7 [0001] P8 [0010]
the line crosses the P8’ [0000]
wymin
left boundary so
calculate the
intersection point to wxmin wxmax
generate P7’
Cohen-Sutherland Examples (cont…)

• Consider the line P7’ to P8


– Start at P8 Window
wymax
– Calculate the
intersection with the P7’ [0000]
right boundary to P7 [0001]
P8’ [0000]
P8 [0010]

generate P8’ wymin

– P7’ to P8’ is inside


the window so is wxmin wxmax
retained
Equation of Line
Y − Y1 Y2 − Y1
=
X − X1 X2 − X1

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

• Intersection points with the window


boundaries are calculated using the line-
equation parameters
– Consider a line with the end-points (x1, y1) and (x2,
y2)
– The y-coordinate of an intersection with a vertical
window boundary can be calculated using:
y = y1 + m (xboundary - x1)
where xboundary can be set to either wxmin or wxmax
Calculating Line Intersections (cont…)

– The x-coordinate of an intersection with a


horizontal window boundary can be calculated
using:
x = x1 + (yboundary - y1) / m
where yboundary can be set to either wymin or wymax
– m is the slope of the line in question and can be
calculated as m = (y2 - y1) / (x2 - x1)
Algorithm
1. Read two endpoints of the line say P1 (x1,y1)
and p2(x2,y2)
2. Read two corners of the window (left –top ) and
(right-bottom) say (Wx1,Wy1) and (Wx2, Wy2)
3. Assign the region codes to the end co-or of P1.
1. Set Bit 1 – if (x<Wx1) ----Wx1- minimum x co-ordinate
2. Set Bit 2 – if (x >Wx2) ----Wx2 – maximum x co-ordinate
3. Set Bit 3 – if (y<Wy1) ----Wy1- minimum y co-ordinate
4. Set Bit 4 – if (y>Wy2) ----Wy2 – minimum y co-ordinate

4. Apply visibility check on the line.


Algorithm conti…

5. If line is partially visible then-


1. If region codes for the both end points are non-zero , find
intersection points p1’ and p2’ with boundary edges by
checking the region codes.
2. If region code for any one end point is non-zero then find
intersection point p1’ or p2’ with the boundary edge.

6. Divide the line segments considering the


intersection points.
7. Draw the line segment
8. Stop.
Area Clipping
• Similarly to lines, areas
must be clipped to a
window boundary
• Consideration must be
taken as to which
portions of the area must
be clipped
Sutherland-Hodgman Area Clipping
Algorithm
• A technique for clipping areas developed by
Sutherland & Hodgman.
• Put simply the polygon is clipped by comparing
it against each boundary in turn.

Original Area Clip Left Clip Right Clip Top Clip Bottom
Sutherland-Hodgeman Clipping

Input/output for algorithm:


• Input: list of polygon vertices in order.
• Output: list of clipped poygon vertices consisting of old vertices
(maybe) and new vertices (maybe)
Sutherland-Hodgeman Clipping

Sutherland-Hodgman does four tests on every


edge of the polygon:

– Inside – Inside ( I-I)


– Inside –Outside (I-O)
– Outside –Outside (O-O)
– Outside-Inside(O-I)

Output co-ordinate list is created by doing these


tests on every edge of poly.
Sutherland-Hodgeman Clipping

Edge from S to P takes one of four cases:

inside outside inside outside inside outside inside outside


p s
p
s

p s
p s I

Save p Save I Save nothing Save I


and P
Sutherland-Hodgeman Clipping

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.

5. Draw polygon using output-list.


6. Stop.
Generalized clipping
• We have used four clipping routines; one for each
boundary.
• But these routines are identical.

• They differ only in their tests for determining


whether a point is inside or outside the boundary.

• Can we make it general ??

• It is possible to write one single routine and pass


information about boundary as parameter. This
change allows to clip along any line(not just
horizontal or vertical boundaries).
• Such general routine can be then used to clip
along an arbitrary convex polygon.

Fig :A window with 6 clipping boundaries


Interior and Exterior window
• Clipping is the process of removing the
graphics parts either inside or outside the given
region.
• Interior clipping removes the parts outside
the given window.
• Exterior clipping removes the parts inside the
given window.
10/3/2024
Interior and Exterior window

Window 2

Window 3

Window 1

Clipping in multiwindow environment


clipping

Clip left Clip right

Clip bottom Clip top

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.

• An area on device to which a window is mapped is


called a viewport.

• The window defines what is to be viewed and the


viewport define where to display.
10/3/2024
Viewing Transformation
• The workstation transformation is given as
– W= T • S • T-1
– Where
 1 0 0  Sx 0 0  1 0 0
T =  0 1 0 S =  0 Sy 0 T −1 =  0 1 0
− Xw min − Yw min 1  0 0 1  Xw min yw min 1

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

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