0% found this document useful (0 votes)
192 views35 pages

Graphics Output Primitives Guide

The document discusses graphics output primitives and coordinate reference frames used in computer graphics. It describes the basic geometric primitives of points and lines. It then summarizes common algorithms for drawing lines on a raster display, including the digital differential analyzer (DDA) algorithm and Bresenham's line algorithm. Bresenham's algorithm uses integer calculations to efficiently determine the pixel positions closest to the actual line path.

Uploaded by

Sương Trần
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
192 views35 pages

Graphics Output Primitives Guide

The document discusses graphics output primitives and coordinate reference frames used in computer graphics. It describes the basic geometric primitives of points and lines. It then summarizes common algorithms for drawing lines on a raster display, including the digital differential analyzer (DDA) algorithm and Bresenham's line algorithm. Bresenham's algorithm uses integer calculations to efficiently determine the pixel positions closest to the actual line path.

Uploaded by

Sương Trần
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Computer Graphics

Chapter 3

Graphics Output Primitives

Nguyen Huu Cuong, PhD.


• The functions in a graphics package that we use to describe the various picture
components are called the graphics output primitives.
• The output primitives describing the geometry of objects are typically referred
to as geometric primitives.
• Point positions and straight-line segments are the simplest geometric primitives.
Additional geometric primitives include circles and other conic sections, quadric
surfaces, spline curves and surfaces, and polygon color areas.
• After the geometry of a picture has been specified within a selected coordinate
reference frame, the output primitives are projected to a 2D plane,
corresponding to the display area of an output device, and scan converted into
integer pixel positions within the frame buffer.
3.1 Coordinate Reference Frames
• To describe a picture, we first decide upon a convenient Cartesian coordinate
system, called the world-coordinate reference frame, which could be either 2D
or 3D.
• Then we describe the objects in our picture by giving their geometric
specifications in terms of position in world coordinates.
• Objects are then displayed by passing the scene information to the viewing
routines, which identify visible surfaces and ultimately map the objects to
positions on the video monitor.
• The scan-conversion process stores information about the scene, such as color
values, at the appropriate locations in the frame buffer, and the objects in the
scene are displayed on the output device.
3.1 Coordinate Reference Frames
 Screen coordinates
• Locations on a video monitor are referenced in integer screen coordinates,
which correspond to the pixel positions in the frame buffer
• Pixel coordinate values give the scan line number (y) and the column number (x
along a scan line).
• Scan lines are referenced from 0, at the top of the screen, to ymax at the bottom
of the screen.
• Pixel positions along each scan line are numbered from 0 to xmax, left to right.
• With software commands, we can set up any convenient reference frame for
screen positions, or we could use noninteger Cartesian values for a picture
description.
• Scan-line algorithms for the graphics primitives use the defining coordinate
descriptions to determine the locations of pixel that are to be displayed.
3.1 Coordinate Reference Frames
• Once pixel positions have been identified for an object, the appropriate color
values must be stored in the frame buffer.
setPixel(x, y);
• This procedure stores the current color setting into the frame buffer at integer
position (x, y), relative to the selected position of the screen-coordinate origin.
getPixel(x, y, color);
• In this function, parameter color receives an integer value corresponding to the
combined RGB bit codes stored for the specified pixel at position (x, y).

• In some case, screen-coordinate information is need for 3D scenes. The screen


coordinates are stored as 3D values, where the third dimension references the
depth of object position relative to a viewing position.
• For a 2D scene, all depth values are 0.
3.1 Coordinate Reference Frames
 Absolute and relative coordinate specifications
• The absolute coordinate values which are specified the actual positions within
the coordinate system in use.
• Some graphics package also allow positions to be specified using relative
coordinates. We can specify a coordinate position as an offset from the last
position that was referenced (called the current position).
3.2 Specifying a 2D world coordinate reference frame
in OpenGL
• gluOrtho2D function used to set up any 2D Cartesian reference frame. The
arguments are the four values defining the x and y coordinate limits for the
picture we want to display.
• This function specifies an orthogonal project, we need to be sure that the
coordinate values are places
in the OpenGL projection
matrix.

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(xmin, xmax, ymin, ymax);
3.3 OpenGL point functions
• To specify the geometry of a point, we simply give a coordinate position in the
world reference frame. Then this coordinate position, along with other
geometric descriptions is passed to the viewing routines.
• The default color for primitives is white and the default point size is equal to the
size of one screen pixel.
glVertex* ();
• The * indicates that suffix codes. The suffix codes are used to identify the spatial
dimension, the numerical data type to be used for the coordinate values.
• glVertex function must be placed between a glBegin function and glEnd
function.
glBegin(GL_POINTS);
glVertex* ();
glEnd();
3.3 OpenGL point functions

glBegin(GL_POINTS);
glVertex2i(50, 100);
glVertex2i(75, 150);
glVertex2i(100, 200);
glEnd();
3.3 OpenGL point functions

int point1[] = { 50, 100 };


int point2[] = { 75, 150 };
int point3[] = { 100, 200 };

glBegin(GL_POINTS);
glVertex2iv(point1);
glVertex2iv(point2);
glVertex2iv(point3);
glEnd();

glBegin(GL_POINTS);
glVertex3f(-78.05, 909.72, 14.60);
glVertex3f(261.91, -5200.67, 188.33);
glEnd();
3.4 OpenGL line functions

glBegin(GL_LINES); glBegin(GL_LINE_STRIP); glBegin(GL_LINE_LOOP);


glVertex2iv(p1); glVertex2iv(p1); glVertex2iv(p1);
glVertex2iv(p2); glVertex2iv(p2); glVertex2iv(p2);
glVertex2iv(p3); glVertex2iv(p3); glVertex2iv(p3);
glVertex2iv(p4); glVertex2iv(p4); glVertex2iv(p4);
glVertex2iv(p5); glVertex2iv(p5); glVertex2iv(p5);
glEnd(); glEnd(); glEnd();
3.5 Line-drawing algorithms
• A straight line segment in a scene is defined by the coordinate positions for the
endpoints of the segment.
• To display the line on a raster monitor, the graphics system must first project the
endpoints to integer screen coordinates and determine the nearest pixel
positions along the line path between the two endpoints. The line color is
loaded into the frame buffer at the corresponding pixel coordinates. Reading
from the frame buffer, the video controller plots the screen pixels.
• This process digitizes the line into a set of discrete integer position that, in
general, only approximates the actual line path.
3.5 Line-drawing algorithms
 Line equations
• The Cartesian slope-intercept equation for straight line

• For any given x interval x along a line, we can compute the corresponding y
interval
• Similarly,
• On raster systems, lines are plotted with pixels, and step sizes in the horizontal
and vertical directions are constrained by pixel separations.
3.5 Line-drawing algorithms
 DDA algorithm
• The digital differential analyzer (DDA) is a scan-conversion line algorithm based
on calculating either y or x.
• A line with positive slope, if the slope is less than or equal to 1, we sample at
unit x intervals (x = 1) and ; k takes integer values starting from
0, for the first point, and increase by 1 until the final endpoint is reached.
• For lines with a positive slope greater than 1, we reverse the roles of x and y. We
sample at unit y intervals (y = 1) and
• If the process is from the right to left endpoints then we have x = -1 and
; or y = -1 and
• Similar calculations are carried out using these equations to determine pixel
positions along a line with negative slope.
• The DDA algorithm is a faster method for calculating pixel positions than one
that directly line equation.
3.5 Line-drawing algorithms
 Bresenham’s line algorithm
• An accurate and efficient raster line generating algorithm, developed by
Bresenham, that use only incremental integer calculations. In addition, this
algorithm can be adapted to display circles and other curves.
• Consider the scan conversion process for lines with positive slope less than 1.0.
Starting from the left endpoint (x0, y0), we step to each column (x position) and
plot the pixel whose scan-line y value is closest to the line path.
• Assuming we have determined that the pixel at (xk, yk) is to be displayed, we
next need to decide which pixel to plot in column . Our choices
are the pixels at positions (xk + 1, yk) and (xk +1, yk +1)
3.5 Line-drawing algorithms
• At sampling position xk + 1, the y coordinate is calculated as

• Then
and
• To determine which of the two pixels is closest to the line path, we set up

• A decision parameter pk for the kth step can be obtained by

• The sign of pk is same as the sign of


.
• If the pixel at yk is closer to the line path than
the pixel at yk + 1 (that is, ),
then pk is negative. In that case, we plot the
lower pixel; otherwise we plot the upper pixel.
3.5 Line-drawing algorithms
• Coordinate changes along the line occur in unit step in either the x or y
directions.
• At step k + 1, the decision parameter is

• We have
• But , so that

where is either 0 or 1, depending on the sign of parameter pk.


• This recursive calculation of decision parameter is performed at each integer x
position, starting at the left coordinate endpoint of the line. The first parameter
p0, at the starting pixel position (x0, y0) is
3.5 Line-drawing algorithms
 Bresenham’s line drawing algorithm for |m| < 1.0
1. Input the two line endpoints and store the left endpoint in (x0, y0).
2. Set the color for frame-buffer position (x0, y0); i.e. plot the first point.
3. Calculate the constants , , , and , and obtain the starting
value for the decision parameter as

4. At each xk along the line, starting at k = 0, perform the following test.


If pk < 0, the next point to plot is and

Otherwise, the next point to plot is and

5. Perform step 4 times.


3.5 Line-drawing algorithms
3.5 Line-drawing algorithms
 Displaying polylines
• Implementation of a polyline procedure is accomplished by invoking a line
drawing routine n – 1 times to display the lines connecting the n endpoints.

 Parallel line algorithms


• Using parallel processing, we can calculate multiple pixel positions along a line
path simultaneously by partitioning the computations among the various
processors available.
3.6 Setting frame-buffer values
• A final stage in the implementation procedures for line segments and other
objects is to set the frame-buffer color values.
• Since scan-conversion algorithms generate pixel positions at successive unit
intervals, incremental operations can also be used to access the frame buffer
efficiently at each step of the scan-conversion process
3.6 Setting frame-buffer values
• Suppose the frame buffer array is addressed in row-major order and that pixel
positions are labeled from (0, 0) at the lower-left screen corner to (xmax, ymax) at
the top-right corner.
• For a bilevel system (bit/pixel), the frame-buffer bit address for pixel position
(x, y) is calculated as

• Moving across a scan line, we can calculate the frame-buffer address for the
pixel at (x + 1, y) as

• Stepping diagonally up to the next scan line form (x, y), we get to the frame-
buffer address of (x + 1, y + 1) with
3.7 OpenGL curve functions
• The OpenGL core library contains functions for displaying Bézier splines, which
are polynomials that are defined with a discrete point set.
• The OpenGL Utility (GLU) has routines for 3D quadric, such as spheres and
cylinders, as well as routines for producing rational B-splines, which are a
general class of splines that include the simpler Bézier curves.
• Using rational B-splines, we can display circles, ellipses, and other 2D quadrics.
• There are routines in the OpenGL Utility Toolkit (GLUT) that we can use to
display some 3D quadrics, such as spheres and cones, and some other shapes.
• Another method we can use to generate a display of a simple curve is to
approximate it using a polyline. We just need to locate a set of points along the
curve path and connect the points with straight-line segments. The more line
sections we include in the polyline, the smoother the appearance of the curve.
3.8 Circle-generating algorithms
 Properties of circles
• A circle is defined as the set of points that are all at a given distance r from a center
position (xc, yc). For any circle point (x, y), this distance relationship is expressed by

• The position of points on a circle circumference by stepping along the axis in unit
steps from xc – r to xc + r and the corresponding y values at each position as

• This is not the best method. The problem is that


it involves considerable computation at each step;
moreover, the spacing between plotted pixel
position is not uniform
3.8 Circle-generating algorithms
• Another way to eliminate the unequal spacing is to calculate points along the circle
boundary using polar coordinates r and .
• Expressing the circle equation in parametric polar from yields the pair of equations

• Using a fixed angular step size, a circle is plotted with equally space points along
the circumference.
• To reduce calculations, we can use a large angular separation between points along
the circumference and connect points with straight-line segments to approximate
the circular path.
• For a more continuous boundary on a raster display, we can set the angular step
size at . This plots pixel positions that are approximately one unit part.
• Although polar coordinates provide equal point spacing, the trigonometric
calculations are still time consuming.
3.8 Circle-generating algorithms
• We can reduce computations by considering the symmetry of circles.
• If we determine the curve positions in the first quadrant, we can generate the circle
section in the second quadrant. And the circle sections in the third and fourth
quadrants can be obtained from sections in first and second quadrants.
• We can takes this one step further, there is symmetry between octants. Circle
sections in adjacent octants within one quadrant are symmetric with respect to the
45o line diving the two octants.
• Taking advantage of the circle symmetry in this way, we can generate all pixel
positions around a circle by calculating only
the points within the sector from x = 0 to x = y.
• The slope of the curve in this octant has a
magnitude less than or equal to 1.0. At x = 0,
the circle slope is 0, and at x = y, the slope
is -1.0
3.8 Circle-generating algorithms
• Bresenham’s line algorithm for raster displays is adapted to circle generation by
setting up decision parameters for finding the closest pixel to the circumference at
each sampling step.
• The circle equation is nonlinear, so that square root evaluations would be required
to compute pixel distances from a circular path.
• Bresenham’s circle algorithm avoids these square-root calculations by comparing
the squares of the pixel separation distances.
3.8 Circle-generating algorithms
 Midpoint circle algorithm
• We sample at unit intervals and determine the closest pixel position to specified
circle path at each step.
• For a given radius r and screen center position (xc, yc), we can first set up algorithm
to calculate pixel positions around a circle path centered at the coordinate origin
(0, 0)
• Then each calculated position (x, y) is moved to its proper screen position by adding
xc to x and yc to y.
• Along the circle section from x = 0 to x = y, the slope of curve varies from 0 to -1.
Therefore, we can take unit steps in the positive x direction over this octant and use
a decision parameter to determine which of the two possible pixel positions in any
column is vertically closer to the circle path.
• Positions in the other seven octants are then obtained by symmetry.
3.8 Circle-generating algorithms
• We define a circle function as
• The relative position of any point (x, y) can be determined by checking the sign of
the circle function:

• Thus, the circle function is the decision parameter in the midpoint algorithm
• The decision parameter is the circle function evaluated at the midpoint between
two pixels
• If pk < 0, this midpoint is inside the circle and the pixel on scan line yk is closer to
the circle boundary. Otherwise, the midpoint is outside or on the circle boundary,
and we select the pixel on scan line yk – 1.
3.8 Circle-generating algorithms
• A recursive expression for the next decision parameter

where yk+1 is either yk or yk – 1, depending on the sign of pk.

• The initial decision parameter is obtained by evaluating the circle function at the
start position

• If the radius r is specified as an integer, we can simply round p0 = 1 – r.


3.8 Circle-generating algorithms
 Midpoint circle algorithm
1. Input radius r and circle center (xc, yc), then set the coordinates for the first point
on the circumference of a circle centered on the origin as
2. Calculate the initial value of the decision parameter as
3. At each xk position, starting at k = 0, perform the following test. If pk < 0, the next
point along the circle centered on (0, 0) is (xk+1, yk) and

Otherwise, the nest point along the circle is (xk + 1, yk - 1) and

where and
4. Determine symmetry points in the other seven octants.
5. Move each calculated pixel position (x, y) onto the circular path centered at (xc, yc)
and plot the coordinate values: ,
6. Repeat steps 3 through 5 until .
3.8 Circle-generating algorithms
3.9 Ellipse-generating algorithms
 Properties of ellipses
• A precise definition of an ellipse can be given in terms of the distances from any
point on the ellipse to two fixed position, called the foci of the ellipse. The sum of
these two distances is the same value for all points on the ellipse.

• Expressing distances d1 and d2 in terms of the focal coordinates and


, we have

• The general ellipse equation in the form


where the coefficients A, B, C, D, E, and F are evaluated in terms of the focal
coordinates and the dimensions of the major and minor axes of the ellipse
3.9 Ellipse-generating algorithms
• An interactive method for specifying an ellipse in an arbitrary orientation is to input
the two foci and a point on the ellipse boundary.
• With these three coordinate positions, we can evaluate the constant.
• Then, the values for coefficients can be computed and used to generate pixels
along the ellipse path.
3.9 Ellipse-generating algorithms
• The ellipse equation can be written in terms of the ellipse center coordinates and
parameters rx and ry as

• Using polar coordinates r and , we can also describe the


ellipse in standard position with the parametric equations

• Angle , called the eccentric angle of the ellipse.


• If rx > ry, the radius of the bounding circle is r = rx.
Otherwise, the bounding circle has radius r = ry.
• As with the circle algorithm, symmetry considerations
can be used to reduce computations.

You might also like