Você está na página 1de 42

cg-circle.

ppt 1/2/2013 4:47:31 PM 1


Outline
Circle drawing using midpoint algorithm
Ellipse drawing
Other curve examples
Line width, line style, and pen styles
Discrete line issues

Review 2.5.1 and 8.1.1; Advanced topics: Chapters 15 and 16
cg-circle.ppt 1/2/2013 4:47:35 PM 2
2D Circles
Circle characterization

Curve with constant curvature




Given the circle center (x
c
, y
c
) and radius r,
how to represent it?

cg-circle.ppt 1/2/2013 4:47:41 PM 3
2D Circles
Given the circle center (x
c
, y
c
) and radius r, how to
represent it?
2D implicit circle


If , (x, y) is inside the circle
If , (x, y) is outside the circle
The circle also has a parametric form
0 ) ( ) ( ) , (
2 2 2
= + = r y y x x y x f
c c
0 ) , ( < y x f
0 ) , ( > y x f
(

+
+
=
(

|
|
sin
cos
r y
r x
y
x
c
c
c
c
y y
x x
y x m

= ) , (
cg-circle.ppt 1/2/2013 4:47:44 PM 4
Discrete 2D Circles
The problems for discrete circles are the same
as for discrete lines as we can only draw at
finite number of pixels
cg-circle.ppt 1/2/2013 4:47:49 PM 5
Midpoint Circle Drawing
Here we consider the case where all the
parameters (x
c
, y
c
, and r) are all integers
To simplify the discussion, we assume the center is
at (0, 0)
We simply need to shift all the pixels by the given center
if it is not at (0, 0)
Eight point symmetry of a circle
cg-circle.ppt 1/2/2013 4:48:03 PM 6
Eight-point Symmetry of a Circle
) , ( y x ) , ( y x
) , ( y x
) , ( y x
) , ( x y ) , ( x y
) , ( x y ) , ( x y
If (x, y) is on the circle, then (y, x), (x, -y),
and so on will also be on the circle
cg-circle.ppt 1/2/2013 4:48:08 PM 7
Exploiting the Symmetry
We will
generate
2
nd
Octant
cg-circle.ppt 1/2/2013 4:48:11 PM 8
Characteristics of 2
nd
Octant
The slope of the 2
nd
octant is between 0 and -1
Note that the slope is given by x / y
Since x changes faster than y, there will be only
two choices if we increase x by 1
cg-circle.ppt 1/2/2013 4:48:25 PM 9
Midpoint Circle Drawing
E
SE
M

ideal
curve
cg-circle.ppt 1/2/2013 4:48:27 PM 10
Midpoint Circle Drawing
E
SE
M

ideal
curve
cg-circle.ppt 1/2/2013 4:48:27 PM 11
Midpoint Circle Drawing Algorithm
Suppose the current pixel is (x, y)
The midpoint for the next pixel is (x+1, y-0.5)
2 2 2
2 2 2
4
5
2
) 5 . 0 ( ) 1 (
) 5 . 0 , 1 (
r y y x x
r y x
y x f d
+ + + =
+ + =
+ =
cg-circle.ppt 1/2/2013 4:48:28 PM 12
Midpoint Circle Drawing Algorithm
Suppose the current pixel is (x, y)
If , choose E
If , choose SE
If , choose either SE or E
0 ) 5 . 0 , 1 ( < + y x f
0 ) 5 . 0 , 1 ( > + y x f
0 ) 5 . 0 , 1 ( = + y x f
cg-circle.ppt 1/2/2013 4:48:29 PM 13
Midpoint Circle Drawing Algorithm
cg-circle.ppt 1/2/2013 4:48:30 PM 14
Incremental Midpoint Circle Drawing Algorithm
Similar to line drawing, there are two cases
If we choose E

E
SE
M

ideal
curve
2 2 2
) 5 . 0 ( ) 1 (
) 5 . 0 , 1 (
r y x
y x f d
+ + =
+ =
3 2 ) 5 . 0 , 2 ( + + = + = x d y x f d
next
cg-circle.ppt 1/2/2013 4:48:32 PM 15
Incremental Midpoint Circle Drawing Algorithm
Similar to line drawing, there are two cases
If we choose SE
E
SE
M

ideal
curve
2 2 2
) 5 . 0 ( ) 1 (
) 5 . 0 , 1 (
r y x
y x f d
+ + =
+ =
2 2 3 2 ) 5 . 1 , 2 ( + + + = + = y x d y x f d
next
cg-circle.ppt 1/2/2013 4:48:32 PM 16
Incremental Midpoint Circle Drawing Algorithm
cg-circle.ppt 1/2/2013 4:48:33 PM 17
Incremental Midpoint Circle Drawing
Can we make the algorithm using integers only?
Under the assumptions the center and radius are
integers
cg-circle.ppt 1/2/2013 4:48:34 PM 18
Integer Only Incremental Midpoint Circle Drawing
cg-circle.ppt 1/2/2013 4:48:34 PM 19
Midpoint Curve Drawing
We can apply the midpoint idea to other curves
also
We need to divide the curves into regions with
|slope| s 1 and |slope| > 1
If |slope| s 1
We change x pixel by pixel
The binary choice can be E/NE or E/SE if we increase x
If |slope| > 1
We change y pixel by pixel
The binary choice can be N/NE or N/NW if we increase y
cg-circle.ppt 1/2/2013 4:48:44 PM 20
Midpoint Curve Drawing
Example
How do we draw
10 x 10 - where ,
8
1
2
s s = x y
cg-circle.ppt 1/2/2013 4:48:48 PM 21
Midpoint Curve Drawing
Example
How do we draw
Since it is symmetric with respect to y, we only
need to do for
If (x, y) is on the curve, then (-x, y) will be on the
curve too
Two regions: and

10 x 10 - where ,
8
1
2
s s = x y
10 x 0 s s
4 0 s s x
10 4 s < x
cg-circle.ppt 1/2/2013 4:48:54 PM week02-3-circle.ppt 22
Midpoint Ellipse Drawing
Given an ellipse

Its parametric curve is given by
It has four-point symmetry
1
2 2
=
|
.
|

\
|

+
|
.
|

\
|

b
y y
a
x x
c c
(

+
+
=
(

|
|
sin
cos
b y
a x
y
x
c
c
) (
) (
2
2
c
c
y y
x x
a
b
dx
dy

=
cg-circle.ppt 1/2/2013 4:48:58 PM week02-3-circle.ppt 23
Midpoint Ellipse Drawing
Given an ellipse

Its parametric curve is given by
1
2 2
=
|
.
|

\
|

+
|
.
|

\
|

b
y y
a
x x
c c
(

+
+
=
(

|
|
sin
cos
b y
a x
y
x
c
c
) (
) (
2
2
c
c
y y
x x
a
b
dx
dy

=
cg-circle.ppt 1/2/2013 4:49:03 PM 24
Midpoint Ellipse Drawing
To simplify the notations, we assume all the
parameters are integers
We further assume the center is at (0, 0)

If , (x, y) is inside the ellipse
If , (x, y) is outside the ellipse

2 2 2 2 2 2
) , ( b a y a x b y x f + =
0 ) , ( < y x f
0 ) , ( > y x f
cg-circle.ppt 1/2/2013 4:49:17 PM 25
Midpoint Ellipse Drawing
Region 1
The slope is between -1 and 0 and
The binary choice is E and SE
Very similar to the circle drawing
y a x b
2 2
s
E
SE
M

ideal
curve
E
SE
M

ideal
curve
cg-circle.ppt 1/2/2013 4:49:19 PM 26
Midpoint Ellipse Drawing
Region 2
The slope is < -1 and
The binary choice is N and NW
Similar to region 1 but with x and y switched
y a x b
2 2
>
cg-circle.ppt 1/2/2013 4:49:19 PM 27
Line Attributes
So far we know all how to draw single-pixel
line segments, circles, ellipses, and other
curves using midpoint algorithms
A line or curve segment can have three attributes
Color We can interpolate the colors smoothly
Width How to draw lines with a specified width?
Style How to draw solid lines, dashed lines and
dotted lines?
We can use a pixel mask to indicate which pixels to draw
along the path
cg-circle.ppt 1/2/2013 4:49:24 PM 28
Lines with Specified Width
We can draw lines with multiple pixels by drawing
each location multiple times vertically (|slope|s1) or
horizontally (|slope|>1)
cg-circle.ppt 1/2/2013 4:49:47 PM 29
Lines with Specified Width
Line caps



For polylines
cg-circle.ppt 1/2/2013 4:50:55 PM 30
Pen and Brush Options
For each pixel location, we can also draw a chosen
pattern given as pen/brush patterns
How to avoid double coloring, i.e., drawing a pixel more
than once?
cg-circle.ppt 1/2/2013 4:51:04 PM
Line Drawing in openGL
Points, lines, and polylines are drawn using
glBegin and glEnd
You can specify what to be drawn by passing one
of the predefined primitives
Then define the vertices using glVertex*
You can change attributes of points and lines
Specify a color at a vertex using glColor*
For points, you can specify the size using glPointSize
For lines you can specify line width using glLineWidth

cg-circle.ppt 1/2/2013 4:51:10 PM
Primitives in openGL
cg-circle.ppt 1/2/2013 4:51:22 PM
Line Patterns in openGL
You can also specify a line pattern using glLineStipple





You can also implement antialiasing in openGL
Using glEnable with GL_POINT_SMOOTH or GL_LINE_SMOOTH
Or you can use multisampling (glEnable with GL_MULTISAMPLE)
See Chapter 6 of the red book
cg-circle.ppt 1/2/2013 4:51:23 PM
Antialiased Example in openGL
cg-circle.ppt 1/2/2013 4:51:24 PM 35
Summary of Midpoint Algorithm
Midpoint algorithm can be used to draw
discrete lines and curves by reducing the
decision at each pixel location to binary
choices
We covered line segments, circles, ellipses, and
other curves
cg-circle.ppt 1/2/2013 4:51:26 PM 36
Criteria at Beginning
Good lines
No gaps in adjacent pixels
Pixels close to ideal line
Consistent choices
Smooth looking
Even brightness in all orientations
Same line from (x
0
, y
0
) to (x
1
, y
1
) and from (x
1
,
y
1
) to (x
0
, y
0
)
cg-circle.ppt 1/2/2013 4:51:31 PM week02-3-circle.ppt 37
Antialiasing
The line segments given by midpoint algorithm are
fairly jaggy
This is because we sample the continuous plane at pixel
locations resulting in aliasing
There are in general two antialiasing techniques
Prefiltering (also called area sampling)
Postfiltering (also called supersampling)
cg-circle.ppt 1/2/2013 4:51:50 PM week02-3-circle.ppt 38
Prefiltering
Also called area sampling
We view each pixel is as the square centered at it
We compute the area covered by each line
The color is computed as the weighted average between the
foreground and background using the area
cg-circle.ppt 1/2/2013 4:52:02 PM 39
Postfiltering
Also called
supersampling
The idea is to first
sample the screen (plane)
at a higher resolution and
use the (weighted)
average color of related
pixels as the final color
cg-circle.ppt 1/2/2013 4:53:06 PM 40
Antialiasing Examples
cg-circle.ppt 1/2/2013 4:53:08 PM 41
Antialiasing Examples
cg-circle.ppt 1/2/2013 4:53:34 PM 42
Intensity Variation
Another problem is that the lines at different
orientations have different average brightness
per unit distance
We can adjust the brightness so that the average
brightness per unit distance to be more similar

Você também pode gostar