Professional Documents
Culture Documents
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
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.
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 ?
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
20
Case :1 Case:2
P Q
Odd
P Q
P even Q
P Q
21
2.Winding number method
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 , 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
49
PointclipClipping
rectangle y = ymax (xmax, ymax)
x = xmin x = xmax
clip
rectangle
B B
A A
clip
rectangle
D' D'
C C
B B
A A
clip
rectangle
D' D'
C C
B B
E
A A
clip
rectangle
D' D'
C C
B H B
E
H' H'
A A
G' G'
clip G
rectangle
D' D'
C C
B H B
E
H' J H'
A A
G' G'
J'
clip G
rectangle I'
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
y = ymax
x = xmin x = xmax
59
Defining Outcodes
For each endpoint, define an outcode
(abrl)
b3b2b1b0
3 2 1 0 0000
0001 0010
abovebelow right left Window
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)
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.
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.
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.
(50,40) (80,40)
P1
(50,10) (80,10)
74
Example
(50,40) (80,40)
I1(10,y1)
P1
(50,10) (80,10)
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:
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.
84
Sutherland-Hodgeman Clipping
Edge from S to P takes one of four cases:
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
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
V3’
Top clipping
V1’
V1
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.
98
Viewing Transformation
99
Definition: World Window
W.B
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
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
45
250 x 250
Viewport in Pixels.
Device coords
106