SlideShare a Scribd company logo
1 of 21
Download to read offline
Computer Graphics:
7-Polygon Rasterization,
Clipping
Prof. Dr. Charles A. Wüthrich,
Fakultät Medien, Medieninformatik
Bauhaus-Universität Weimar
caw AT medien.uni-weimar.de
Filling polygons (and drawing them)
• In general, except if we are dealing with wireframes, we would
want to draw a filled polygon on our screen.
• The advantage is clear: the polygon acquires thickness and can
be use to render surfaces
• The simplest way one would do that is to draw the polygon border
and then fill the region delimited by the polygon
• In fact, this is the start point for the real algorithm, the scanline
algorithm
• The scanline algorithm combines the advantages of filling
algorithms and of line tracing at the borders in a complex but very
fast way
• As input one takes an ordered list of points representing the
polygon
Scanline algorithm
• The basic idea is very simple:
– A polygon can be filled one
scanline at a time, from top to
bottom
– Order therefore polygon corners
according to their highest y
coordinate
– Order each horizonal line
according to the x coordinate of
the edge intersections
– Fill between pairs of edges, stop
drawing until the next edge, and
then restart filling again till the
next one
– once finished the edges at
current line, restart at next y
value
– Of course, one can also draw
upwards
Scanline algorithm
• Notice that the number of
edges remains constant
between starting and ending
points in the horizontal bands.
• Notice also that segments
have only a limited contiguous
range where they are active
• Notice that while proceeding
downwards, borders can use
a mirrored DDA to be drawn
• In this way, one can draw line
borders and fill between
them, after having ordered
the border intersections with
the current line WRT current
coordinate
Scanline algorithm
• Polygon drawing starts at the
bottom.
• Out of the edges list the ones with
lowest starting point are chosen.
• These will remain part of the „active
edge“ list until their end is met
• When they end, they are removed
and replaced by new starting edges
• This until there is no edge left among
the active edge
• At each value of the y variable, the
edge rasterization is computed, and
edges are ordered by growing x
• Colour is then filled between sorted
pairs of edge rasterizations.
Triangle rasterization
• Modern graphics cards
accept only triangles at the
rasterization step
• Polygons with more edges
are simply triangularized
• Obviously, the rasterization of
a triangle is much easier
• This because a triangle is
convex, and therefore a
horizontal line has just the left
and the right hand borders
• Filling is then done between
the left side and the right side
Clipping: motivation
• Often in 2D we have drawings
that are bigger than a screen
• To save drawing complexity,
it is good to be able to cut the
drawings so that only screen
objects are drawn
• Also, one needs to protect
other (invisible) regions while
working on a complex
drawing
• The question is how is this
done
• Problem: Given a segment in
the plane, clip it to a
rectangular segment
Line clipping
• Let B be the screen, and let
P1P2 be the endpoints of the
segment to be drawn
• There are four possible
cases available:
a) Whole line is visible"
P1,P2∈B
b) Line is partially visible"
P1∈B, P2∈B, P1P2
intersects screen borders
c) Line partially visible"
P1, P2∉B, but P1P2
intersects screen borders
d) Line not visible"
P1, P2∉B
P1
P2
P2
P2
P2
P1
P1
P1
a
b
c
d
Line clipping Algorithm
IF (P1,P2∈B) /* a */
DrawLine(P1,P2)
ELSE IF /* b */
(((P1∈B)AND NOT(P2∈B)) OR
((P2∈B)AND NOT(P1∈B)))
compute I=(P1P2∩borders)
IF(P1ÎB)
Drawline(I,P1)
ELSE
DrawLine(I,P2)
ELSE /* c,d */
compute I1,I2=
(P1P2∩ borders)
IF I1,I2 exist
Drawline (I1,I2)
END
P1
P2
P2
P2
P2
P1
P1
P1
a
b
c
d
Examples: Cohen-Sutherland algo.
Code points according to
characteristics:
Bit 0=1 if xP<xmin else 0
Bit 1=1 if xP>xmax else 0
Bit 2=1 if yP<ymin else 0
Bit 3=1 if yP>ymax else 0
Use bitwise operations:
code(P1) AND code(P2)!= 0
trivial case, line not
on screen
code(P1) OR code(P2) == 0
trivial case, line
on screen
ELSE
- compute line-borders intersection
(one at time) and set their code
as above
- redo clipping with shortened line
Note: before new intersection, at least
one endpoint is outside WRT the
border you clipped against, thus
one subseg is trivially out (all
left or right or up or down of
screen)
P1
P2
P2
P2
P1
P1
code=1001 code=1000
P2
P1
code=1010
code=0001 code=0010
code=0110
code=0100
code=0101
1 2 3
4
code=0000
5
6
7 8 9
Algorithm Examples
P1
P2
Q2 R2
R1
Q1
1001
1000
P2
0001
0100
0101
1 2 3
4
0000
5 6
7 8 9
1010
0010
0110
R4
R3
Q3
P3
P4
Algorithm examples
P1P2: P1=0001, P2=1000
P1 AND P2= 0000
P1 OR P2=1001
Subdivide against left,
Pick P2, find P4
new line P2P4
P2P4: P2=1000, P4=1000
P2 AND P4: 1000 outside!
Draw nothing
Q1Q2: Q1=0100, Q2=0000
Q1 AND Q2:0000
Q1 OR Q2: 0100
Subdivide, Pick Q2, find Q3
new line Q2Q3
Q2Q3: Q2=0000, Q3=0000
Q2 AND Q3: 0000
Q1 OR Q3: 0000 inside!
Draw Q3Q2
Q3Q2: Q3=0100
~
R1R2: R1=0100, R2=0010
R1 AND R2= 0000
R1 OR R2= 0110
Subdivide, Pick R1, find R4
new line R1R4
R1=0100, R4=0000
R1 AND R4= 0000
R1 OR R4= 0100
Subdivide, Pick R4, find R3
new line R3R4
R3=0000 R4=0000
R3 AND R4=0000
draw R3R4
P1
P2
Q2 R2
R1
Q1
1001
1000
P2
0001
0100
0101
1 2 3
4
0000
5 6
7 8 9
1010
0010
0110
R4
R3
Q3
P3
P4
Clipping polygons
• The task is similar, but it is more complicated to achieve
• Polygon clipping may result into disjunct polys
xmin xmax
ymax
ymin
xmin xmax
ymax
ymin
Sutherland Hodgeman Algorithm
• Clearly, drawing polygons is a
more complicated issue
• Idea: one could follow the
polygon border, and switch to
following the border when the
polygon leaves the screen until it
re-enters it
• This means creating a new
polygon, which is trimmed to the
screen
• While following an edge, four
cases are possible:
ymin
xmin xmax
ymax
P1
P2
P1
P2
P1
P2
P1
P2
P1
1
2
3
4
Sutherland-Hodgeman Algorithm
• The algorithm works considering polygons
as lists of edges
• Input is a list L of polygon edges
• Output wil be a new list L´ of polygon
edges
• The polygon is clipped against ALL screen
borders one at a time
FOR all screen borders DO:
FOR all lines in polygons
DO:
FOR all points P in L DO
Compute intersection I
of line with current
border
IF (case 1):
Do Nothing
IF (case 2):
Add (I,Succ(P))to L´
IF (case 3):
Add (I) to L´
IF (case 4):
Add (succ(P)) to L´
END
END
END
P1
P2
P1
P2 P2
1
Example
ymin
xmin xmax
V2
V3
V4
V5
V1
I1
I2
Example
ymin
xmin xmax
V2
V3
V4
V5
V1
I1
I2
• Left border
Input: {V1,V2,V3 ,V4 ,V5}
Output: {I1,V2,V3 ,V4 ,Í2}
• Top Border
Input: {I1,V2,V3 ,V4 ,I2}
Output: {I1,I3,I4 ,V3,V4 ,I2}
ymin
xmin xmax
V2
V3
V4
V5
V1
I1
I2
I3 I4
Clipping in 3D
• Remember the near and far
clipping planes of the view
frustum?
• How do I clip a polygon
against them?
Viewpoint
Clipping in 3D
• Remember the near and far
clipping planes of the view
frustum?
• How do I clip a polygon
against them?
• As a matter of fact, it is not so
different!
• The problem can be reduced
to the same as in 2D, with a
few differences Viewpoint
Clipping in 3D
• Let us consider a the far plane
and a polygon
• Substitute the coordinates of the
vertices of the triangle into the
plane equation:
– Front: <0
– Back: >0
– Plane: =0
• So we can follow the vertices
exactly like in Cohen-Sutherland
to clip against the plane
• A similar method can be applied
for an arbitrary plane
• For the frustum planes one can
do clipping one plane at a time,
like in 2D (except they are 6 now)
<0
>0
+++ Ende - The end - Finis - Fin - Fine +++ Ende - The end - Finis - Fin - Fine +++
End

More Related Content

Similar to 7-Clipping-16 (1).pdf

Comparison of Various Line Clipping Algorithm for Improvement
Comparison of Various Line Clipping Algorithm for ImprovementComparison of Various Line Clipping Algorithm for Improvement
Comparison of Various Line Clipping Algorithm for ImprovementIJMER
 
Liang- Barsky Algorithm, Polygon clipping & pipeline clipping of polygons
Liang- Barsky Algorithm, Polygon clipping & pipeline clipping of polygonsLiang- Barsky Algorithm, Polygon clipping & pipeline clipping of polygons
Liang- Barsky Algorithm, Polygon clipping & pipeline clipping of polygonsLahiru Danushka
 
Clipping computer graphics
Clipping  computer graphicsClipping  computer graphics
Clipping computer graphicsShaishavShah8
 
Sutherlands Cohen and Hodgeman algorithms
Sutherlands Cohen and Hodgeman algorithmsSutherlands Cohen and Hodgeman algorithms
Sutherlands Cohen and Hodgeman algorithmsRohit Jain
 
Polygon clipping with sutherland hodgeman algorithm and scan line fill algorithm
Polygon clipping with sutherland hodgeman algorithm and scan line fill algorithmPolygon clipping with sutherland hodgeman algorithm and scan line fill algorithm
Polygon clipping with sutherland hodgeman algorithm and scan line fill algorithmMani Kanth
 
Windowing clipping
Windowing   clippingWindowing   clipping
Windowing clippingShweta Shah
 
Lecture 2d point,curve,text,line clipping
Lecture   2d point,curve,text,line clippingLecture   2d point,curve,text,line clipping
Lecture 2d point,curve,text,line clippingavelraj
 
Clipping in Computer Graphics
Clipping in Computer Graphics Clipping in Computer Graphics
Clipping in Computer Graphics Barani Tharan
 
Lecture1616_16827_2D Clipping.ppt
Lecture1616_16827_2D Clipping.pptLecture1616_16827_2D Clipping.ppt
Lecture1616_16827_2D Clipping.pptGaganvirKaur
 
Unit2- line clipping.pptx
Unit2- line clipping.pptxUnit2- line clipping.pptx
Unit2- line clipping.pptxRYZEN14
 

Similar to 7-Clipping-16 (1).pdf (20)

Comparison of Various Line Clipping Algorithm for Improvement
Comparison of Various Line Clipping Algorithm for ImprovementComparison of Various Line Clipping Algorithm for Improvement
Comparison of Various Line Clipping Algorithm for Improvement
 
Liang- Barsky Algorithm, Polygon clipping & pipeline clipping of polygons
Liang- Barsky Algorithm, Polygon clipping & pipeline clipping of polygonsLiang- Barsky Algorithm, Polygon clipping & pipeline clipping of polygons
Liang- Barsky Algorithm, Polygon clipping & pipeline clipping of polygons
 
Clipping computer graphics
Clipping  computer graphicsClipping  computer graphics
Clipping computer graphics
 
Clipping
ClippingClipping
Clipping
 
Sutherlands Cohen and Hodgeman algorithms
Sutherlands Cohen and Hodgeman algorithmsSutherlands Cohen and Hodgeman algorithms
Sutherlands Cohen and Hodgeman algorithms
 
Polygon clipping with sutherland hodgeman algorithm and scan line fill algorithm
Polygon clipping with sutherland hodgeman algorithm and scan line fill algorithmPolygon clipping with sutherland hodgeman algorithm and scan line fill algorithm
Polygon clipping with sutherland hodgeman algorithm and scan line fill algorithm
 
Polygon filling
Polygon fillingPolygon filling
Polygon filling
 
Windowing clipping
Windowing   clippingWindowing   clipping
Windowing clipping
 
Clipping
ClippingClipping
Clipping
 
Clipping
ClippingClipping
Clipping
 
ohu.pptx
ohu.pptxohu.pptx
ohu.pptx
 
UNIT2.pptx
UNIT2.pptxUNIT2.pptx
UNIT2.pptx
 
Computer graphics
Computer graphicsComputer graphics
Computer graphics
 
line clipping
line clipping line clipping
line clipping
 
Lecture 2d point,curve,text,line clipping
Lecture   2d point,curve,text,line clippingLecture   2d point,curve,text,line clipping
Lecture 2d point,curve,text,line clipping
 
ibuib.pptx
ibuib.pptxibuib.pptx
ibuib.pptx
 
Clipping in Computer Graphics
Clipping in Computer Graphics Clipping in Computer Graphics
Clipping in Computer Graphics
 
Clipping
ClippingClipping
Clipping
 
Lecture1616_16827_2D Clipping.ppt
Lecture1616_16827_2D Clipping.pptLecture1616_16827_2D Clipping.ppt
Lecture1616_16827_2D Clipping.ppt
 
Unit2- line clipping.pptx
Unit2- line clipping.pptxUnit2- line clipping.pptx
Unit2- line clipping.pptx
 

Recently uploaded

MICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxMICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxabhijeetpadhi001
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxUnboundStockton
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 

Recently uploaded (20)

MICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxMICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptx
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docx
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 

7-Clipping-16 (1).pdf

  • 1. Computer Graphics: 7-Polygon Rasterization, Clipping Prof. Dr. Charles A. Wüthrich, Fakultät Medien, Medieninformatik Bauhaus-Universität Weimar caw AT medien.uni-weimar.de
  • 2. Filling polygons (and drawing them) • In general, except if we are dealing with wireframes, we would want to draw a filled polygon on our screen. • The advantage is clear: the polygon acquires thickness and can be use to render surfaces • The simplest way one would do that is to draw the polygon border and then fill the region delimited by the polygon • In fact, this is the start point for the real algorithm, the scanline algorithm • The scanline algorithm combines the advantages of filling algorithms and of line tracing at the borders in a complex but very fast way • As input one takes an ordered list of points representing the polygon
  • 3. Scanline algorithm • The basic idea is very simple: – A polygon can be filled one scanline at a time, from top to bottom – Order therefore polygon corners according to their highest y coordinate – Order each horizonal line according to the x coordinate of the edge intersections – Fill between pairs of edges, stop drawing until the next edge, and then restart filling again till the next one – once finished the edges at current line, restart at next y value – Of course, one can also draw upwards
  • 4. Scanline algorithm • Notice that the number of edges remains constant between starting and ending points in the horizontal bands. • Notice also that segments have only a limited contiguous range where they are active • Notice that while proceeding downwards, borders can use a mirrored DDA to be drawn • In this way, one can draw line borders and fill between them, after having ordered the border intersections with the current line WRT current coordinate
  • 5. Scanline algorithm • Polygon drawing starts at the bottom. • Out of the edges list the ones with lowest starting point are chosen. • These will remain part of the „active edge“ list until their end is met • When they end, they are removed and replaced by new starting edges • This until there is no edge left among the active edge • At each value of the y variable, the edge rasterization is computed, and edges are ordered by growing x • Colour is then filled between sorted pairs of edge rasterizations.
  • 6. Triangle rasterization • Modern graphics cards accept only triangles at the rasterization step • Polygons with more edges are simply triangularized • Obviously, the rasterization of a triangle is much easier • This because a triangle is convex, and therefore a horizontal line has just the left and the right hand borders • Filling is then done between the left side and the right side
  • 7. Clipping: motivation • Often in 2D we have drawings that are bigger than a screen • To save drawing complexity, it is good to be able to cut the drawings so that only screen objects are drawn • Also, one needs to protect other (invisible) regions while working on a complex drawing • The question is how is this done • Problem: Given a segment in the plane, clip it to a rectangular segment
  • 8. Line clipping • Let B be the screen, and let P1P2 be the endpoints of the segment to be drawn • There are four possible cases available: a) Whole line is visible" P1,P2∈B b) Line is partially visible" P1∈B, P2∈B, P1P2 intersects screen borders c) Line partially visible" P1, P2∉B, but P1P2 intersects screen borders d) Line not visible" P1, P2∉B P1 P2 P2 P2 P2 P1 P1 P1 a b c d
  • 9. Line clipping Algorithm IF (P1,P2∈B) /* a */ DrawLine(P1,P2) ELSE IF /* b */ (((P1∈B)AND NOT(P2∈B)) OR ((P2∈B)AND NOT(P1∈B))) compute I=(P1P2∩borders) IF(P1ÎB) Drawline(I,P1) ELSE DrawLine(I,P2) ELSE /* c,d */ compute I1,I2= (P1P2∩ borders) IF I1,I2 exist Drawline (I1,I2) END P1 P2 P2 P2 P2 P1 P1 P1 a b c d
  • 10. Examples: Cohen-Sutherland algo. Code points according to characteristics: Bit 0=1 if xP<xmin else 0 Bit 1=1 if xP>xmax else 0 Bit 2=1 if yP<ymin else 0 Bit 3=1 if yP>ymax else 0 Use bitwise operations: code(P1) AND code(P2)!= 0 trivial case, line not on screen code(P1) OR code(P2) == 0 trivial case, line on screen ELSE - compute line-borders intersection (one at time) and set their code as above - redo clipping with shortened line Note: before new intersection, at least one endpoint is outside WRT the border you clipped against, thus one subseg is trivially out (all left or right or up or down of screen) P1 P2 P2 P2 P1 P1 code=1001 code=1000 P2 P1 code=1010 code=0001 code=0010 code=0110 code=0100 code=0101 1 2 3 4 code=0000 5 6 7 8 9
  • 11. Algorithm Examples P1 P2 Q2 R2 R1 Q1 1001 1000 P2 0001 0100 0101 1 2 3 4 0000 5 6 7 8 9 1010 0010 0110 R4 R3 Q3 P3 P4
  • 12. Algorithm examples P1P2: P1=0001, P2=1000 P1 AND P2= 0000 P1 OR P2=1001 Subdivide against left, Pick P2, find P4 new line P2P4 P2P4: P2=1000, P4=1000 P2 AND P4: 1000 outside! Draw nothing Q1Q2: Q1=0100, Q2=0000 Q1 AND Q2:0000 Q1 OR Q2: 0100 Subdivide, Pick Q2, find Q3 new line Q2Q3 Q2Q3: Q2=0000, Q3=0000 Q2 AND Q3: 0000 Q1 OR Q3: 0000 inside! Draw Q3Q2 Q3Q2: Q3=0100 ~ R1R2: R1=0100, R2=0010 R1 AND R2= 0000 R1 OR R2= 0110 Subdivide, Pick R1, find R4 new line R1R4 R1=0100, R4=0000 R1 AND R4= 0000 R1 OR R4= 0100 Subdivide, Pick R4, find R3 new line R3R4 R3=0000 R4=0000 R3 AND R4=0000 draw R3R4 P1 P2 Q2 R2 R1 Q1 1001 1000 P2 0001 0100 0101 1 2 3 4 0000 5 6 7 8 9 1010 0010 0110 R4 R3 Q3 P3 P4
  • 13. Clipping polygons • The task is similar, but it is more complicated to achieve • Polygon clipping may result into disjunct polys xmin xmax ymax ymin xmin xmax ymax ymin
  • 14. Sutherland Hodgeman Algorithm • Clearly, drawing polygons is a more complicated issue • Idea: one could follow the polygon border, and switch to following the border when the polygon leaves the screen until it re-enters it • This means creating a new polygon, which is trimmed to the screen • While following an edge, four cases are possible: ymin xmin xmax ymax P1 P2 P1 P2 P1 P2 P1 P2 P1 1 2 3 4
  • 15. Sutherland-Hodgeman Algorithm • The algorithm works considering polygons as lists of edges • Input is a list L of polygon edges • Output wil be a new list L´ of polygon edges • The polygon is clipped against ALL screen borders one at a time FOR all screen borders DO: FOR all lines in polygons DO: FOR all points P in L DO Compute intersection I of line with current border IF (case 1): Do Nothing IF (case 2): Add (I,Succ(P))to L´ IF (case 3): Add (I) to L´ IF (case 4): Add (succ(P)) to L´ END END END P1 P2 P1 P2 P2 1
  • 17. Example ymin xmin xmax V2 V3 V4 V5 V1 I1 I2 • Left border Input: {V1,V2,V3 ,V4 ,V5} Output: {I1,V2,V3 ,V4 ,Í2} • Top Border Input: {I1,V2,V3 ,V4 ,I2} Output: {I1,I3,I4 ,V3,V4 ,I2} ymin xmin xmax V2 V3 V4 V5 V1 I1 I2 I3 I4
  • 18. Clipping in 3D • Remember the near and far clipping planes of the view frustum? • How do I clip a polygon against them? Viewpoint
  • 19. Clipping in 3D • Remember the near and far clipping planes of the view frustum? • How do I clip a polygon against them? • As a matter of fact, it is not so different! • The problem can be reduced to the same as in 2D, with a few differences Viewpoint
  • 20. Clipping in 3D • Let us consider a the far plane and a polygon • Substitute the coordinates of the vertices of the triangle into the plane equation: – Front: <0 – Back: >0 – Plane: =0 • So we can follow the vertices exactly like in Cohen-Sutherland to clip against the plane • A similar method can be applied for an arbitrary plane • For the frustum planes one can do clipping one plane at a time, like in 2D (except they are 6 now) <0 >0
  • 21. +++ Ende - The end - Finis - Fin - Fine +++ Ende - The end - Finis - Fin - Fine +++ End