You are on page 1of 105

Prof P.D.

Kad

1
2
3
 So we can figure out how to draw lines and
circles
 How do we go about drawing polygons?
 We use an incremental algorithm known as
the scan-line algorithm

4
 Vertex = point in space (2D or 3D)
◦ + extra information in most cases
 Polygon = ordered list of vertices
◦ Each vertex connected with the next in the list
◦ Last is connected with the first

5
6
 Different types of Polygons
 Convex
 Concave
 Non-simple : self-intersecting

Convex Concave Self-intersecting

7
convex concave
all interior angles are at least one angle is
<1800 >1800

8
9
10
11
12
13
14
 1. Polygon drawing primitives..
(polygon is saved as single unit)

 2 .Trapezoid primitive
◦ store them as series of trapezoids
◦ Trapezoids formed by two scan line and two line
segments.

 3. Line and point approach.

15
16
OP X Y

5 1 0

2 4 0

2 5 4

2 3 8

2 0 5

2 1 0
17
18
 Why ?

 Want to fill in (color) only pixels inside a polygon

 What is “inside” of a polygon ?

 Inside Test Methods


1. Even –odd method
2. Winding no. method

19
1.Even –odd Method
 Find point outside the polygon
 Choose x smaller than min x of polygon.
 Count how many intersection with boundary line.
Odd : inside
Even : Outside

 If point of intersection is a vertex where 2 lines meet, check


whether other 2 ends of those lines lie
 On same side ->count even(2) intersections
 On opposite sides -> count as single intersection

20
Case :1 Case:2

P Q
Odd
P Q
P even Q
P Q

 Count number of intersections


with polygon edges
◦ If N is odd, point is inside
◦ If N is even, point is outside

21
 2.Winding number method

1. Find point outside the polygon

2. Direction of each line it crosses is


considered.
3. -1 -> bottom to up
4. 1 ->top to down

22
 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.

+1 -1

23
24
▪ A.Seed Fill Approaches
▪ 2 algorithms:
a) Boundary Fill
b) Flood Fill
▪ works at the pixel level
▪ suitable for interactive painting applications
▪ B. Scanline : Fill Approaches
▪ works at the polygon level
▪ better performance

25
 These algorithms assume that at least one pixel
interior to a polygon or region is known
 Regions maybe boundary or interior defined

Interior-defined region
Boundary-defined region
26
▪ 4-connected region: From a given pixel, the
region that you can get to by a series of 4 way
moves (N, S, E and W)
▪ 8-connected region: From a given pixel, the
region that you can get to by a series of 8 way
moves (N, S, E, W, NE, NW, SE, and SW)

4-connected 8-connected 27
 Boundary-defined region
 Start at a point inside a region
 Paint the interior outward to the edge
 The edge must be specified in a single color
 Fill the 4-connected or 8-connected region
 4-connected fill is faster, but can have problems:

28
29
void BoundaryFill(int x, int y, Fillcolor , Boundarycolor )
{
int current;
current = ReadPixel(x, y);
if(current != Boundarycolor && current != Fillcolor)
{
putpixel(x,y, Fillcolor);
BoundaryFill(x+1, y, Fillcolor, Boundarycolor r);
BoundaryFill(x-1, y, Fillcolor , Boundarycolor);
BoundaryFill(x, y+1, Fillcolor, Boundarycolor );
BoundaryFill(x, y-1, Fillcolor, Boundarycolor);
}
}
30
 Interior-defined region
 Used when an area defined with multiple color
boundaries
 Start at a point inside a region
 Replace a specified interior color (old color) with fill color
 Fill the 4-connected or 8-connected region until all interior
points being replaced

31
void FloodFill(int x, int y, newcolor, oldColor)
{
if(ReadPixel(x, y) == oldColor)
{
putpixel(x,y, newcolor);
FloodFill(x+1, y, newcolor, oldColor);
FloodFill(x-1, y, newcolor, oldColor);
FloodFill(x, y+1, newcolor, oldColor);
FloodFill(x, y-1, newcolor, oldColor);
}
}

32
 Recursive seed-fill algorithm may not fill regions
correctly if some interior pixels are already
displayed in the fill color.
 This occurs because the algorithm checks next pixels
both for boundary color and for fill color.
 Encountering a pixel with the fill color can cause a
recursive branch to terminate, leaving other interior
pixels unfilled.
 This procedure requires considerable stacking of
neighboring points, more efficient methods are
generally employed.

33
Xk+1,yk+1 Scan Line yk +1

Scan Line yk

Xk , yk

34
10 Scan Line

0
2 4 6 8 10 12 14 16
35
36
 The basic scan-line algorithm is as follows:
1. Find the intersections of the scan line with all
edges of the polygon
2. Sort the intersections by increasing x coordinate
3. Fill in all pixels between pairs of intersections that
lie interior to the polygon

37
 Edge endpoint is duplicated.
 In other words, when a scan line intersects an edge endpoint,
it intersects two edges.
 Two cases:
◦ Case A: edges are monotonically increasing or decreasing
◦ Case B: edges reverse direction at endpoint
 In Case A, we should consider this as only ONE edge
intersection
 In Case B, we should consider this as TWO edge intersections

Scan-line Scan-line

Case A Case B 38
B C

1
0 D

E
F 39
B C

D
0
1

E
40
 Exploit coherence: pixels that are nearby each other tend to share
common attributes (color, illumination, normal vectors, texture, etc.).
◦ Span coherence: Pixels in the same scan line tend to be similar.
◦ Scan-line coherence: Pixels in adjacent scan line tend to be similar.

41
 Suppose that slope of the edge is
m = Dy/Dx
 Let xk be the x intercept of the current scan line,
and xk+1 be the x intercept of the next scan line,
then
xk+1 = xk + Dx/Dy

42
The scan line conversion algorithm works as follows
i. Find Intersection at each scanline with all edges
ii. Sort intersections in ascending order of x
iii. Fill Each pair of intersections.
Special cases to be handled:
i. Horizontal edges should be excluded
ii. For vertices lying on scanlines,
i. count twice if both edges are on same side of scanline
ii. Count once if both edges are on opposite side of scanline
• Coherence between scanlines tells us that
- Edges that intersect scanline y are likely to intersect y + 1
- X changes predictably from scanline y to y + 1

(Xk + 1, Yk + 1) Scan Line yk + 1

(Xk , Yk )
Scan Line yk

43
 Edge table:
◦ all edges sorted by their ymin coordinates.
◦ keep a separate bucket for each scanline
◦ within each bucket, edges are sorted by increasing x of
the ymin endpoint
◦ Only nonhorizontal edges are stored in edge table

44
Where xmin is X
intercept value (at
lower vertex)

-4/2
45
 A list of edges active for current scanline,
sorted in increasing x EdgeTable

y= 9

y=8

46
 A list of edges active for current scanline,
sorted in increasing x in EdgeTable

47
Windowing & Clipping

48
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

49
PointclipClipping
rectangle y = ymax (xmax, ymax)

x = xmin x = xmax

(xmin, ymin) y = ymin

For a point (x,y) to be inside the clip rectangle:


xmin  x  xmax
50
ymin  y  ymax
Line Clipping

clip
rectangle

Cases for clipping lines


51
Line Clipping

B B

A A

clip
rectangle

Cases for clipping lines


52
Line Clipping
D

D' D'

C C
B B

A A

clip
rectangle

Cases for clipping lines


53
Line Clipping
F
D

D' D'

C C
B B
E

A A

clip
rectangle

Cases for clipping lines


54
Line Clipping
F
D

D' D'

C C
B H B
E
H' H'
A A
G' G'

clip G
rectangle

Cases for clipping lines


55
Line Clipping
F
D

D' D'

C C
B H B
E
H' J H'
A A
G' G'

J'
clip G
rectangle I'

Cases for clipping lines


56
Cohen-Sutherland Algorithm
 Idea: eliminate as many cases as possible without computing
intersections
 Start with four lines that determine the sides of the clipping
window

y = ymax

x = xmin x = xmax

y = ymin
57
The Cases
 Case 1: both endpoints of line segment inside all four lines
 Draw (accept) line segment as is

y = ymax

x = xmin x = xmax

y = ymin

 Case 2: both endpoints outside all lines and on same side


of a line
58
 Discard (reject) the line segment
The Cases
 Case 3: One endpoint inside, one outside
 Must do at least one intersection
 Case 4: Both outside
 May have part inside
 Must do at least one intersection

y = ymax

x = xmin x = xmax

59
Defining Outcodes
 For each endpoint, define an outcode

(abrl)
b3b2b1b0

 Outcodes divide space into 9 regions


 Computation of outcodes requires at most 4 subtractions

b3 = 1 if y > ymax, 0 otherwise


b2 = 1 if y < ymin, 0 otherwise
b1 = 1 if x > xmax, 0 otherwise
60 b0 = 1 if x < xmin, 0 otherwise
Cohen-Sutherland: World Division

 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 0000
0001 0010
abovebelow right left Window

Region Code Legend 0101 0100 0110


61
62
Cohen-Sutherland Algorithm

1001 1000 1010


(Xmax,ymax)

bit 0 : x  xmin bit 2 : y  ymin


0001 0000 0010
bit 1 : x  xmax bit 3 : y  ymax
(Xmin,ymin)

0101 0100 0110


clip
rectangle Region outcodes
63
Using Outcodes
 Consider the 5 cases below
 Case 1:
 AB: outcode(A) = outcode(B) = 0
 Accept line segment

64
Using Outcodes
 Case 2:
 EF: outcode(E) logically ANDed with outcode(F) (bitwise) 
0
 If answer of AND operation is non zero means line is
outside the window and hence discard those lines.
 Both outcodes have a 1 bit in the same place
 Line segment is outside of corresponding side of clipping
window
 reject

65
Using Outcodes
 Case 3:
 CD: outcode (C) = 0, outcode(D)  0
 lines partially inside the window : Lines for which the AND operation of the
end coordinates is ZERO .These are identified as partially visible lines.
 Compute intersection
 Location of 1 in outcode(D) determines which edge to intersect
with.
 Note if there were a segment from A to a point in a region with
2 ones in outcode, we might have to do two interesections

66
Using Outcodes
 Case 4:
 GH and IJ: same outcodes, neither zero but logical AND
yields zero
 Shorten line segment by intersecting with one of sides of
window
 Compute outcode of intersection (new endpoint of
shortened line segment)
 Reexecute algorithm

67
Cohen-Sutherland Line Clipping
 Outcode tests and line-edge intersects are quite fast (how
fast?)
 But some lines require multiple iterations:
 Clip top
 Clip left
 Clip bottom
 Clip right

68
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. Asssign the region codes to the end co-or of P1.
1. Set Bit 1 – if (x<Wx1)
2. Set Bit 2 – if (x >Wx2)
3. Set Bit 3 – if (y<Wy1)
4. Set Bit 4 – if (y>Wy2)

4. Apply visibility check on the line.

69
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.

70
Problems
1.Following are coordinates of clipping window : Lower Left
Corner (20,20) and Upper Right Corner (200,200). A line has
end coordinates as (0,10) and (250,15). The given line segment
is ___________.
Use Cohen – Sutherland Outcode Algorithm.

Answer: Completely Invisible

72
Problems
2.Clip a line starting from (-13, 5) and ending at (17, 11)
against the window having lower left corner at (-8, -4) and
upper right corner at ( 12,8). What will be the end points of
clipped line?
Use Cohen – Sutherland Outcode Algorithm.

3. According to Cohen-Sutherland algorithm, a line is


completely outside the window if ............

4. Is 0011 valid region code / outcode used in Line Clipping


algorithm?
Answers: 2> (-8,6) and (2,8)
73 3> The region codes of line endpoints have a '1' in same bit position
4> No
Example
Problem : Use cohen sutherland algorithm to clip two lines
p1(40,15)-p2(75,45) and p3(70,20)-p4(100,10) against a
window A(50,10),B(80,10),C(80,40),D(50,40)
P2

(50,40) (80,40)

P1
(50,10) (80,10)

74
Example

Solution : point outcode AND


p1 0001 0000 ( means line is partially visible)
p2 1000 P2

(50,40) (80,40)

I1(10,y1)

P1
(50,10) (80,10)

Outcode of P1(0001) indicates that line P1P2 has intersection


with left boundary. Let say I1(10,y1)
75
We need to compute y1 of I1
Example

y1= m(XL-x)+y = 6/7(50-40)+15 => 23.57

P2

(50,40) (80,40)
I2(1x1,40)

I1(10,y1)

P1
(80,10)
(50,10)

Outcode of P2(1000) indicates that line P1P2 has intersection with top boundary. Let say I2(X1,40). We
76 need to compute x1 of I2
Problems
3.One edge of the polygon has coordinates (10,20) and
(15,40). In scan line polygon filling at the ith step x-value of the
intersection point of the scan line ‘i’ and above mentioned edge
is 14. What will be the x-value of the intersection point of the
scan line in i+1 th step?

Answer: 14.2

77
Sutherland-Hodgman Polygon
Clipping Algorithm

78
Why Is Clipping Hard?
 What happens to a triangle during clipping?
 Possible outcomes:

Triangle→triangle Triangle→quad Triangle→Pentagon

◼ How many sides can a clipped triangle have?

79
Polygon Clipping

Example
80
Polygon Clipping

Example
81
Polygon Clipping

Example
82
Sutherland-Hodgman
Polygon 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
83
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.

84
Sutherland-Hodgeman Clipping
Edge from S to P takes one of four cases:

inside outside inside outside inside outside inside outside


p p s
s

p s
p s I

Save p Save I Save nothing Save I


and P
85
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

86
Algorithhm
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.

87
Example

V3

V2

V1

88 10/28/2020
solution

V3

V2’ V2’’,v2’, v3,v3’


V2 V2’’

V3’
Top clipping
V1’

V1

Left clipping Right Clipping Bottom clipping

V1, v2, v3
V2’’,v2’, v3,v3’
89 V1, V1’,v2’, v3 V1, V1’,v2’, v3
solution

V3

V2’

V2’’
V3’

90
Weiler-Atherton Polygon Clipping
 Problem in some Concave polygon
Using sutherland Hodgman algo.
 Extraneous line

91
Weiler-Atherton Polygon Clipping
 When the clipped polygons have two or more separate sections, then it is the concave
polygon handled by this algorithm. The vertex-processing procedures for window
boundaries are modified so that concave polygon is displayed.
 Let the clipping window be initially called clip polygon and the polygon to be clipped
the subject polygon. We start with an arbitrary vertex of the subject polygon and trace
around its border in the clockwise direction until an intersection with the clip polygon is
encountered:
 1. If the edge enters the clip polygon, record the intersection point and
continue to trace the subject polygon.
 2. If the edge leaves the clip polygon, record the intersection point and
make a right turn to follow the clip polygon in the same manner
 Whenever our path of traversal forms a sub-polygon we output the sub-polygon as part of
the overall result. We then continue to trace the rest of the original subject polygon from
a recorded intersection point that marks the beginning of a not-yet traced edge or portion
of an edge. The algorithm terminates when the entire border of the original subject
polygon has been traced exactly once.
92
Algorithm
1. First make a list of all intersection points namely i1, i2, i3, ...
2. Classify those intersection points as entering or exiting.
3. Now, make two lists, one for the clipping polygon, and the other for the clipped polygon.
4. Fill both the lists up in such a way that the intersection points lie between the correct
vertices of each of the polygon. That is the clipping polygon list is filled up with all the
vertices of the clipping polygon along with the intersecting points lying between the
corresponding vertices.
5. Now, start at the 'to be clipped' polygon's list.
6. Choose the first intersection point which has been labelled as an entering point. Follow the
points in the list (looping back to the top of the list, in case the list ends) and keep on
pushing them into a vector or something similar of the sorts. Keep on following the list until
an exiting intersection point is found.
7. Now switch the list to the 'polygon that is clipping' list, and find the exiting the intersection
that was previously encountered. Now keep on following the points in this list (similar to
how we followed the previous list) until the entering intersection point is found (the one
that was found in the previous 'to be clipped' polygon's list).
8. This vector now formed by pushing all the encountered points in the two lists, is now the
clipped polygon (one of the many clipped polygons if any of the clipping polygons is
concave).
9. Repeat this clipping procedure (i.e. from step 5) until all the entering intersection points
93 have been visited once.
1. Finding all the intersection points and grouping them
Here, let there be a polygon ABCD and another polygon VWXYZ. Let ABCD be the clipping
polygon and let VWXYZ be the clipped polygon.

So, we can find the intersection points using any method. For example, we can find the
intersecting points separately and then find for each intersecting point find if it is entering or
leaving,

94
2. Making and filling of two lists
Now, we make two lists. One for the clipping polygon and one for the clipped polygon.
Now this is how we fill it:

95
3. Running of the algorithm
We start at the clipped polygon’s list, i.e. VWXYZ.
Now, we find the first intersecting point that is entering. Hence we choose i1.
From here we begin the making of the list of vertices (or vector) to make a clipped sub-polygon.

According to the given example, i1 Y i2 is a clipped sub-polygon.


96 Similarly, we get:
i0 V i3 as another sub-polygon also.
97
Hence, we were able to get two sub-polygons as a result of this polygon clipping,
which involved a concave polygon, which resulted in:

Similarly, this clipping works for convex polygons.

98
Viewing Transformation

• Generally, an image is defined in a world coordinate


system, WC.
• Picture stored in computer memory using convenient
Cartesian cocordinate system referred to as WCS
• World Coordinate system: application-specific
• Example: drawing dimensions may be in meters, km,
feet, etc.
• But when picture is represented on display device,it is
measured in physical device coordinate system
corresponding to the display device.

99
Definition: World Window

•World Window: rectangular region of drawing (in world coordinates)


to be drawn (it specifies what to display)
•Defined by W.L, W.R, W.B, W.T
W.T

W.B

W.L W.R 100


Definition: Viewport

•Rectangular region in the screen used to display drawing(it specifies


where to display)
•Defined in screen coordinate system

V.T

V.B
V.L V.R

101
Clipping Window
ywmax
World Coordinates
Viewing world has its own
coordinates, which may be
a non-uniform scaling of
ywmin world coordinates.

xwmin xwmax

Viewport
yvmax
The clipping window is
mapped into a viewport.

yvmin

xvmin xvmax
Viewport Coordinates
102
View Window and Viewport
View/Clipping Window

Information outside
the viewport is
clipped away
Viewport

103
, Larry F. Hodges
VIEWING TRANSFORMATION

• Mapping of a part of a world coprdinates to device


coordinates is referred to as viewing transformation.
• 2D viewing transformation: Window-to viewport
transformation or Windowing transformation

104
2D viewing transformation pipeline

Construct World-
Modeling World Convert World-
Coordinate Scene
Coordinates Coordinates Coordinates to
From Modeling-
Viewing-
Coordinate
Coordinates
Transformations

Viewing Coordinates

Transform Viewing- Normalized Device


Coordinates Map Normalized- Coordinates
Coordinates to
Coordinates to
Normalized-
Device-Coordinates
Coordinates

Beginning with modelling coordinates, we go through a series of


coordinate transformations which result in device coordinates that
105
can be plotted
Viewing in 2D - Viewport
250

45

Window in world coordinates.

250 x 250
Viewport in Pixels.
Device coords

106

You might also like