Output Primitives - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Output Primitives

Description:

into rectangular grid of pixels. E.g: straight line segments or polygon colour area ... It uses only integer value, avoid multiplication and has a ... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 22
Provided by: Eza5
Category:

less

Transcript and Presenter's Notes

Title: Output Primitives


1
Output Primitives
  • 10/9/03
  • Chapter 10 in Hill (10.4)

2
Introduction
What is it?
A basic geometric-structure specification that
can produce an object or a scene which being
map into rectangular grid of pixels. E.g
straight line segments or polygon colour
area Each output primitives is specified with
input coordinates with specific information
(intensity , color).
3
Introduction
  • Point and straight line segments are the simplest
    geometric
  • component of pictures
  • 1. Point by converting single coordinate
  • position into output device through
  • application program.
  • 2. Line by calculating the intermediate
  • positions along two end-point.

4
Straight Line
  • (Recall what you have learned before)
  • Equation for straight line
  • . y mx b
  • . m slope
  • . m is the changes in y divided by changes of x
  • . m ?y / ?x
  • . b the point where the line intercepts of y
    axis.

5
Straight Line
  • Let say we want to find the equation for
    a line from (x1,y1)
  • to (x2,y2)
  • . Slope
  • . m ?y /?x
  • . ?y y2-y1
  • . ?x x2-x1
  • . m y2-y1/x2-x1
  •  
  • . y axis Intercept
  • . b y1 m.x1

6
Straight Line
  • The equation is used to
  • a.    Find the equation by two given points.
  • b.    Find the the b intercept by given equation.
  • c. Find out whether line is parallel or
    intercept at
  • which point.

7
OpenGL and output primitives
  • OpenGL provides built-in tools or functions
    to draw output
  • primitives. Even though this is done by
    OpenGL, it is
  • important for us to know the underlying
    fundamental that
  • is used to draw output primitives.
  • In this section, we will look at the
    algorithms used to draw
  • a straight line.

8
Line-Drawing Algorithm
1.   Digital differential analyzer (DDA)
algorithm 2. Bresenhams line-drawing
algorithm
9
DDA Algorithm
Sample line at unit intervals on one axis and
calculate the corresponding co-ordinate on the
other axis. E.g. Line from (1,1) to (12,8) x1
1 and y1 1 x2 12 and y2 8
Algorithm (for slope 0 lt m lt 1) ?y 8 1 7
?x 12 1 11 m ?y / ?x 7/11 0.64
Plot the first point (1,1) For each x
co-ordinate between x 2 and x 12 y
co-ordinate previous y co-ordinate m
plot point at (x,y)
10
C Implementation of DDA
define ROUND (a) ((int) (a0.5)) void
plot_point(int x, int y) void line_DDA(int
x1,int y1,int x2, int y2) int dx,
dy, steps, k float x_increment, y_increment,
x, y if (abs(dx) gt abs(dy))
steps abs(dx) else steps
abs(dy) x_increment dx /
(float) steps y_increment dy / (float)
steps plot_point(ROUND(x), ROUND(y))
for (k0 klt steps k ) x
x_increment y y_increment
plot_point(ROUND(x), ROUND(y))
11
DDA Algorithm
12
DDA Algorithm
0 1 2 3 4 5 6
7 8 9 10 11 12
13
Observations
Advantages . Faster than using y mx b .
Eliminates multiplication (mx) by using screen
characteristics, i.e. incrementing
pixel. Disadvantages . Floating point
arithmetic is slower than integer arithmetic. .
Rounding down take time (fairly expensive
operation). . Long lines can drift from the true
line due to floating point round-off errors.
14
Bresenhams Algorithm
  • Is a classic example of an incremental
    algorithm that
  • computes the location of each pixel along a
    line based on the
  • information about the previous pixel
  • It uses only integer value, avoid
    multiplication and has a
  • tight and efficient innermost loop that
    generates the proper
  • pixels

15
Bresenhams Algorithm
  • It has become the standard algorithm used in
    hardware and
  • Software rasterizers
  • Uses integer increment no rounding
  • Decision parameter p used to decide which of two
    pixels is
  • plotted (x1, y) or (x1, y1)

16
Implementation
1) Calculate ?x, ?y, 2?y,2?y-2?x   . ?x 11,
?y 7   . 2?y 14   . 2?y - 2?x -8  
2) Calculate initial p 2?y - ?x p 3
17
Implementation
  • Plot the first point (1,1) (line between (1,1)
    and (12, 8))
  • For each x coordinate between x 2 and x 12
  • If (p lt 0)
  • y previous y
  • p previous p 2?y
  • else
  • y previous y 1
  • p previous p 2?y - 2?x
  • plot point at(x,y)

18
C Implementation
Void line_bresenham(int x1, int y1, int x2, int
y2) Int dx,dy Int
two_dy 2 dy Int two_dy_dx 2
(dy-dx) Int p 2 dy dx Int
x, y, x_end   If (x1 gt x2)
x x2 y y1 x_end
x1 else x x1
y y1 x_end x2
plot_point(x,y) (cont.)
19
C Implementation
while (x lt x_end) x
if plt0 p two_dy
else p two_dy_dx
y
plot_point(x,y)
20
Bresenhams Algorithm
Advantages . Accurate and efficient
incremental integer calculations . Can be
adapted to draw circles and curves.
21
Properties of a drawn line
  • Point to take note when using algorithm to draw
    straight line
  • The line drawn should be as straight as possible
    and should
  • reliably pass through both of the given
    endpoints
  • The line should be smooth and have uniform
    brightness
  • along its length
  • 3) The line should be drawn so that it is
    repeatable
  • 4) The direction to draw the line from which
    endpoint is not
  • important
Write a Comment
User Comments (0)
About PowerShow.com