Complex Objects, Such As Trees and Terrain or Furniture and Wals, Positioned at
Complex Objects, Such As Trees and Terrain or Furniture and Wals, Positioned at
3-1
POINTS AND LINES
fur
Point plotting is accomplished by converting a single coordinate position
nished by an application program into appropriate operations for the output de.
the electron beam is turned on to i
vice in 115e. With aCRT monitor, for example,
ie
luminate the screen phosphor at the selected location. How the electron beam
random-scan (vector) svste
positioned depends on the display technology. Ali_t,
stores point-plotting instructions in the display and coordinate valves
these instructions are converted to deflection voltages that position the electron
beam at the screen locations to be plotted during each refresh cycle. For a blacl
and-white raster system, on the other hand, a point is plotted by setting the bit
value corresponding to a specified screen position within the frame bufer to 1
Then, as the electron beam sweeps across each horizontal scan line, it emit
burst of elevtrons plots a
rame bufter. With an RGBpoint) whenever a value of 1is encountered in the Section -1
ns for the intensities that system, the frame buffer is loaded with the color Points and iines
are to be displayed at the
Line drawing is
accomplished by calculating screen pixel positions.
the line path between two
specified endpoint internmediate
positions. An outputpositions along
diected fill in these positions between the
to device is then
as a vector pen plotter or a endpoints. For analog devices, such
random-scan
smoothly from one endpoint to the other. Linearlydisplay, a straight line can be drawn
varying horizontal and verti
cal deflection voltages are
generated that are
changes in the x and ydirections to produce the proportional to the required
Digital devices display a straight line segmentsmooth line.
between the two endpoints. Discrete coordinate positions by plotting discrete points
cakulated from the equation of the line. For a raster videoalong the line path are
display, the line color
(intensity) is then loaded into the frame buffer at the corresponding pixel coordi
nates. Reading from the frame buffer, the video controller then
"plots"
pixels. Screen locations are referenced with integer values, so plotted the screen
may only approximate actual line positions between two specified positions
endpoints.
computed line position of (10.48, 20.51), for example, would be converted to pixelA
position (10, 21). This rounding of coordinate values to integers causes lines to be
displayed with a stairstep appearance ("the jaggies"), as represented in Fig 3-1.
The characteristic stairstep shape of raster lines is particularly noticeable on sys
terns with low resolution, and we can improve their appearance somewhat by
displaying them on high-resolution systems. More effective techniques for
smoothing raster lines are based on adjusting pixel intensities along the line
paths.
For the raster-graphics device-level algorithms discussed in this chapter, ob
ject positions are specified directly in integer device coordinates. For the time
being, we will assume that pixel positions are referenced according to scan-line
number and column number (pixel position across a scan line). This addressing
scheme is illustrated in Fig. 3-2. Scan lines are numbered consecutively from o
starting at the bottom of the screen; and pixel columns are numbered from 0.le#
to right across each scan line. In Section 3-10, we consider alternative pixel ad
dressing schemes.
To load a specified color into the frame buffer at a position
corresponding
to column ralong scan line y, we will assume we have available a low-level nr
cedure of the form
setPixel (*, y)
Figure 3-1
Stairstep effect (jaggies) produced
when a line is generated as a series
of pixel positions.
Scan
Line
Number
Figure 3-2
0 1 2 3 4 5 Pixel Column Pixel positions referenced by scan
Number line number and column number.
getPixel (x, y)
3-2
LINE-DRAWING ALGORITHMS
m= 2 y
(3-2)
b=y1- m
x1
(3-3)
Algorithms for displaying straight lines
the calculations given in Eqs. 3-2 and 3-3. are based on the line
For any given xinterval Ar along a equation 3-1 and
y interval Ay from Eq. 3-2 as line, we can
compute the
een endpoint Ay = m Ax corresponding
) and (z, yal. Similarly, we can obtain the xinterval Ax (3-4)
corresponding to
Ar = y
a
specified Ay as
These equations form the basis for (3-5)
determining deflection voltages in
analog de
ViKes. For lines with
small slope magnitudes
then set horizontal delection vollage and m<1, Ar can be set proportional to a Section 3-2
have proportional Ay as
to the
corresponding vertical deflection is Line DrawIg AigIithes
magnitudes |m
tion voltage with the 1, Ay can calculated from Eq. 3-4. For lines whose slopes
be set Proportional to asmall vertical deflec-
tional t Av corresponding horizontal
calculated from Eq. 3-5/For lines withdeflection
zontal and vertical voltage set prophori-
skope mis generated deflections m= 1, Ar = Ay and the
voltages are equa). In each case, a smooth line with
On raster between
systenms,
the
lines arespecified endýoints.
zontal and
vertical plotted with pixels, and step sizes in the hori-
must "sample" alinedirections are
constrained by pixel separations. That is, we
line at each sampled at discrete positions and determine the nearest pixel to the
lustrated in Fig. 34,position. This scan-conversion process for
for a near horizontal line with discrete straight ines i5 1
along the xaxis. sample positions
DDA Algorithm
The digital differential analyzer (DDA) is a
calculat1ng either Ay or ArAsing Eq. 3-4 scan-conversion
or Eq. 3-5. We
line algorithm based on Figure 3-4
sample the line at unit in- Straight line segment with
tervals in one coordinate ánd determine
line path for the other coordinate. corresponding integer values nearest the fivesampling positions alon
the r axis between z and I
Consider first a Jine with positive slope,jas shown in Fig, 3-3.Ifthe slope is
less than or equal to 1)we sample at unit *ntervals (Ax = 1) ànd compute each
successive yvalue as
= yk t m (3-6)
Subscriptk takes integer values starting from 1, for the first point, and increases
by 1 until the final endpoint is reached Since mcan be any real number between
0and Tyfhe calculated y values mustbe rounded to the nearest integer.
Fgt lines with apositive slope greater than 1,we reverse the roles of x and
y. That is, we sample at unit y intervals (Ay = 1) and calculate each succeeding r
value as
(3-7)
The abeve e,ns a
Equations 3-6 and 3-7 are based on the assumption that lines are to be
processed frÍm the left endpoint to the right endpoint (Fig. 3-3). Ifthis processing Jty9
is reversed, so that the starting endpoint is at the right, then either we have
Ar = -1and
Yk+1y m (3-S)
have Ay =-1 with
or (when the slope is greater than 1)we
1 1 39)
Ishe abeve eons
Euations 3-6 through 3-9 can also be used to calculate pixel positions aloms
absolute value of the slope is less than iand
aline with negative slope. If the set
is at the left, we
Ar = 1 and calculate y values
start endpoint withjEq. 3-6.
Chapter 3 we set r=-1 and
the same slope),
When the start endpoint is at the right (forwhen
Primitives obtain the absolute value of a
negative
Eg. 3-8. Similarly,
POShonsfrom
= -l and Ea. 3-9 or we use Ay = 1'and q. 3-7.
T5eater than 1, we use Ay following procedure, which accepts as
This algorithm is summarizedintheHorizontal and vertical differences be
t e two endpoint pixel positions. and dy. The differ
ne endpoint positions are assigned to parameters dx steps. Start
e ith the greater magniude determines the value of paramneter
8With pixel position (r.. u). we determine the offset needed at each step to
eiterate the next pixel position along the line path. We loop through thus process
steps times. If the magnitude of dxis greater than the magnitude of dy and xa
osnan xb, the values of the increments in the xand y directions are Iand m,
respectively. If the greater change is in the x direction, but xa is greater than xb,
then the decrements -1and -n are used to generate each new point on the line.
OnerwiSe, we usea unit increment (or decrement) in the y direction and an x in
crement (or decrement)of 1/m.
#include "device.h"
#define ROUND(a) ((int) (a+0.5))
void lineDDA (int xa, int ya, int xb, int
yb)
int dx = xb - xa, dy = yb - ya,
float xIncrement, steps, k;
y(nCrement, x = xa, y = ya;
if (abs (dx) > abs (dy))
steps = abs (dx);
else steps = abs (dy);
xIncrement = dx / (float) steps;
yIncrement = dy / (float ) steps;
setPixel (ROUND (x) , ROUND (y) );
for (k=0; k<steps; k++) {
X += xIncrement;
y += yIncrement;
set Pixel (ROUIND(:X), ROUND (Y))