Title: Computer Graphics
1Computer Graphics
2Polygons
- Generally represented as vertex and edge data
- Edge data can be explicitly or implicitly
represented in a data structure - Explicit - list of vertices (x,y) and a list of
edges (v1,v2) no order required - Implicit - list of vertices in either clockwise
or counter-clockwise order
3PolygonsExplicit data structure
- Edges
- 1, 4
- 4, 3
- 3, 2
- 2, 5
- 5, 1
V3
- Vertices
- 4, 3
- -2, -4
- -4, 4
- 0, 2
- 2, -4
V1
V4
V2
V5
4PolygonsImplicit data structure
V3
- Vertices
- 4, 3
- 0, 2
- -4, 4
- -2, -4
- 2, -4
V1
V2
V4
V5
5Filling shapes
- Shapes are generally filled by setting each
pixel, for a given scan-line, from xmin to xmax - Spans reflect spatial coherence - primitives
often do not change from pixel to pixel or from
scan line to scan line
6Filling shapes
Span coherence - all pixels on a span are set to
the same value
Edge coherence - all pixels on an edge are set to
the same value
7Filling polygons
- Scanline algorithm - How to ...
- Find the intersections between each scan lines
and all of the edges - Sort the intersections by Y, and then by X
- Set all pixels between pairs of intersections
that lie interior to the polygon
8Filling polygons
- In order to insure that pixels on shared edges
are not often set twice - Only choose pixels which are found inside a
polygon
9Filling polygons
- Horizontal edges
- Ignored as they are automatically drawn due to
the fact that there are included via other edges
10Filling polygons
- A vertex shared by two edges, where the dY does
not change direction from edge to edge, - Need to be addressed only once
11Filling polygons
- To accomplish this, one of the two edges needs to
be foreshortened before being added to the Edge
Table
12Edge table
y min
y max
A
x min
6
1/m
5
DA
4
D
3
BA
2
B
1
CD
CB
0
C
- For each edge, create an edge record consisting
of ymax, xmin (value for x corresponding to ymin)
and 1/m - Place the each edge record into the sublist
(bucket) corresponding to the edges ymin
13Active edge table
y max
x
1/m
- Set y to the smallest y coordinate with a list in
the ET - Initialize the AET to be empty
- Repeat until AET and ET are both empty
- Move edge records from ET to AET where yminy
- Draw spans using two edge records at a time
- Remove from AET all edges where ymax y
- Increment y
- Increment each x by 1/m
14Clipping
- Simultaneous equations
- Cohen-Sutherland Line-Clipping Algorithm
- Parametric Line-Clipping Algorithm
- Circles
- Sutherland-Hodgman Polygon-Clipping Algorithm
15Clipping
- Lines within the clipping rectangle are trivially
accepted - Lines outside the rectangle with respect to one
halfplane are trivially rejected
Accepted
Rejected
16Clipping
- Lines that are neither accepted nor rejected
- are clipped against the extended edges of the
clipping rectangle - thus creating new endpoints
Accepted
Rejected
17Simultaneous equations
- For a given line segment (x0,y0,x1,y1), each
point in the edge can be described by... - x x0 t (x1 - x0)
- y y0 t (y1 - y0)
- ...where 0 ? t ? 1
18Simultaneous equations
- First, the intersection of the extended clipping
rectangle edge and the extended line segment is
determined. - Then, this point (x,y) is placed into two pairs
of simultaneous equations to compute tedge and
tline - If tedge and tline and both in the range 0,1,
then this is an intersection we wish to clip to
19Cohen-SutherlandLine-Clipping Algorithm
- Uses a four-bit code for each of the nine regions
- 1 0 0 1top-bottom-left-right
- 1 outside0 inside
1001
1000
1010
0001
0000
0010
0101
0100
0110
20Cohen-SutherlandLine-Clipping Algorithm
if outCode.top 1 then clip against
top else if outCode.bottom 1 then clip
against bottom else if outCode.right 1
then clip against right else if outCode.left
1 then clip against left code endpoint
p0 if (accept) then draw P0 to P1
- accept ? false
- done ? false
- code endpoints P0 and P1
- while (not done)
- if (outCode0 0) and (outCode1 0) then
- accept ? true
- done ? true
- else if (outCode 0 and outCode1) ? 0 then
- done ? true
- else if outCode0 0 then
- swap points and codes
-
21ParametricLine-Clipping Algorithm
- Cyrus-Beck (1978)
- Somewhat different from Cohen-Sutherland
- First, pick a point PE somewhere on the edge
being tested
N? P(t) - PE lt 0
N? P(t) - PE 0
N? P(t) - PE gt 0
Ni
22ParametricLine-Clipping Algorithm
- Next, since the dot product of the edges normal
with P(t)-PE is zero when P(t) is on the edge - with some rearrangement and substitution we get
N? P(t) - PE lt 0
N? P(t) - PE 0
N? P(t) - PE gt 0
Ni
23ParametricLine-Clipping Algorithm
- If t is in the range 0,1
- then the edge intersects our line segment (P0,P1)
- And so, we clip against the edge using t to
compute the new endpoint
N? P(t) - PE lt 0
N? P(t) - PE 0
N? P(t) - PE gt 0
Ni
24Antialiasing
- Increasing resolution
- Area sampling