Upload
trannguyet
View
225
Download
4
Embed Size (px)
Citation preview
Output Primitives Lecture: 3
Dr. Ayman Elshenawy Elsefy Page | 1
Lecture 3
Output Primitives
Assuming we have a raster display, a picture is completely specified by:
- A set of intensities for the pixel positions in the display.
- A set of complex objects, such as trees and terrain or furniture and walls, positioned at
specified coordinate locations within the scene. Shapes and colors of the objects can be
described internally with pixel arrays or with sets of basic geometric structures, such as
straight line segments and polygon color areas figure (4-1).
Figure (4-1): Image is set of intensities of pixels or a set of complex objects
The scene is then displayed either by:
- Loading the pixel arrays into the frame buffer.
- Scan converting the basic geometric-structure specifications into pixel patterns.
Typically, graphics programming packages provide functions to describe a scene in
terms of these basic geometric structures, referred to as output primitives, and to group sets of
output primitives into more complex structures.
Each output primitive is specified with input coordinate data and other information about
the way that objects is to be displayed.
Points, straight line segments, circles, other conic sections, quadric surfaces, spline curves
and surfaces, polygon color areas, and character strings are the most geometric components of
pictures. We begin our discussion of picture-generation procedures by examining device-level
algorithms for displaying two dimensional output primitives.
Output Primitives Lecture: 3
Dr. Ayman Elshenawy Elsefy Page | 2
Point
Point plotting is accomplished by converting a single coordinate position furnished by an
application program into appropriate operations for the output device in use.
- In CRT monitor, the electron beam is turned on to illuminate the screen phosphor at the
selected location.
- How the electron beam is positioned depends on the display technology.
o A random-scan (vector) system stores point-plotting instructions in the display
list, and coordinate values in these instructions are converted to deflection
voltages that position the electron beam at the screen locations to be plotted
during each refresh cycle.
o For a black and white raster system, a point is plotted by setting the bit value
corresponding to a specified screen position within the frame buffer to 1. Then,
as the electron beam sweeps across each horizontal scan line, it plots a point.
o For RGB Raster system, the frame buffer is loaded with the color codes for the
intensities that are to be displayed at the screen pixel positions. Figure (4-2)
shows how a point is plotted in CRT Display devices.
Figure (4-2): a point is plotted in CRT Display devices.
Line Digital devices display a straight line segment by plotting discrete points between
the start point and the end point.
Discrete coordinate positions along the line path are calculated from the equation of
the line.
Output Primitives Lecture: 3
Dr. Ayman Elshenawy Elsefy Page | 3
For a raster video display, the line color (intensity) is then loaded into the frame
buffer at the corresponding pixel coordinates.
After reading from the frame buffer, the video controller then "plots" the screen
pixels.
Screen locations (coordinates) are referenced with integer values, so plotted
positions may only approximate actual line positions between two specified
endpoints.
A computed line position of (10.48, 20.51) [Mathematical Point], for example,
would be converted to pixel position (10, 21). Thus rounding of coordinate values to
integer’s causes lines to be displayed with a stair-step appearance, as represented
in Fig 4-3.
The characteristic stair-step shape of raster lines is particularly noticeable on systems
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.
The 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. 4-4. Scan lines are numbered consecutively from 0, starting at the top of the
screen; and pixel columns are numbered from 0, left to right across each scan line.
Figure (4-3): Stair-step appearance Figure (4-4): Pixel positions referenced by scan line number and column number
One of 2N intensities or colors is associated with each pixel, where N is the number
of bits per pixel.
Output Primitives Lecture: 3
Dr. Ayman Elshenawy Elsefy Page | 4
Gray-scale typically has one byte per pixel, for 28 = 256 intensities.
Color often requires one byte per channel, with three color channels per pixel: red,
green, and blue. Color data is stored in a frame buffer. This is sometimes called an
image map or bitmap.
To load a specified color into the frame buffer at a position corresponding to column
x along scan line y, we will assume we have available a low-level procedure of the
form
setpixel(x, y, color): Sets the pixel at position (x, y) to the given color.
We sometimes will also want to be able to retrieve the current frame buffer intensity
setting for a specified location. We accomplish this with the low-level function
getpixel(x, y) : Gets the color at the pixel at position (x, y).
Line drawing algorithms
The equation of straight line can be computed using the following equations,
푦 = 푚푥 + 푏 (1)
푚 = ∆∆
= (2)
And 푏 = 푦 − 푚푥 (3)
Figure (4-4): Line path between endpoint positions (x , y ) and (x , y )
With m representing the slope of the line and b as the y intercept. Given that the two
endpoints of a line segment are specified at positions (푥 ,푦 ) and (푥 ,푦 ) as shown in Fig. 4-
4, we can determine values for the slope m and y intercept b with the following calculations:
1. Brute Force Algorithm
The starting point is the equation of a straight line:
Output Primitives Lecture: 3
Dr. Ayman Elshenawy Elsefy Page | 5
푦 = 푚푥 + 푐 where 푚 is the gradient of the line:
푚 =∆푦∆푥
=푦 − 푦푥 − 푥
and c is its intercept of the y-axis
푐 = 푦 − 푚푥
Assume that the endpoints of the line are known, For any value of x we can compute y. A
for loop can be used to iterate through all the values of x between xfrom and xto and for each of
these, the corresponding y value calculated.
figure 4-5
Enumerating the values of x
Disadvantage of brute force method
a. Gaps started to be appeared
The results of running such an algorithm look like Figure 4-5. Unfortunately, if we pick
coordinates for the end points such that the line is steep (in fact when its gradient m is >1 the
results look something like Figure:
Output Primitives Lecture: 3
Dr. Ayman Elshenawy Elsefy Page | 6
Figure 4-6. Brute force algorithm with steep gradient
The solution to the gaps problem is to always use the most rapidly changing variable (x or
y) as the index to the loop (Figure 31). i.e. When the gradient (m) >1 - use y as the control
variable in the loop and make x the subject of the equation:
푋 = (푦 − 푐)/푚
Figure 4-7-Comparison of x & y values for different gradients
b. It requires floating point
There is a second problem with the brute force approach: It requires floating point
arithmetic which is slow when compared with using integer only arithmetic. An approach
which used solely integers would result in a much quicker algorithm. It can be solved by
removing the effect of the floating points
2. Bresenham’s Algorithm
One such integer only algorithm is Bresenham’s. Start by considering the simple case
where 0 < m <1
Consider, as before, iterating the x values from left to right. If the pixel at (푥 , 푦 ) has been
plotted, then the next one MUST be either (푥 ,푦 ) or (푥 ,푦 ). Why? - Because of the
Output Primitives Lecture: 3
Dr. Ayman Elshenawy Elsefy Page | 7
gradient - it can’t go up more than one step in the y direction for one step in the x (m<1) and it
can’t go down at all (0<=m). How can we work out which pixel to plot?
Figure 4-8. The basis of Bresenham’s algorithm
- By calculating the difference between the true mathematical value of y at 푥 (we’ll call
that 푦 ) and the y values represented by the pixels. 훥푦 and 훥푦 present these distances and
the decision on which pixel to plot can be made by following the following pseudo code
To illustrate Bresenham’s approach, we first consider the scan conversion process for lines
with positive slope less than 1. Pixel positions along a line path are then determined by
sampling at unit x intervals. Starting from the left endpoint (푥 , 푦 ) of a given line, we step to
each successive column (x position) and plot the pixel whose scan-line y value is closest to the
line path. Figure 4-7(a) demonstrates the 퐾 step in this process. Assuming we have
determined that the pixel at (푥 , 푦 ) is to be displayed, we next need to decide which pixel to
plot in column 푥 + 1,. Our choices are the pixels at positions (푥 ,푦 ) and (푥 ,푦 ). At
sampling position 푥 , we label vertical pixel separations from the mathematical line path as
푑 , and 푑 . They coordinate on the mathematical line at pixel column position 푥 is
calculated as :
Output Primitives Lecture: 3
Dr. Ayman Elshenawy Elsefy Page | 8
푦 = 푚(푥 ) + 푐
푑 = 푦 − 푦 = 푚(푥 ) + 푐 − 푦
푑 = 푦 − 푦 = 푦 − 푚(푥 ) + 푐
The difference between these two separations is
푑 − 푑 = 2푚(푥 ) − 2푦 + 2푐 − 1
A decision parameter 푝 for the 푖 step in the line algorithm can be obtained by
rearranging the above Eq. so that it involves only integer calculations. We accomplish this by
substituting 푚 = ∆푦/∆푥, where ∆푦푎푛푑∆푥 are the vertical and horizontal separations of the
endpoint positions, and defining:
푝 = ∆푥(푑 − 푑 ) = 2∆푦푥 + 2∆푦 − 2∆푥푦 + 2∆푥푏 − ∆푥
푝 = ∆푥(푑 − 푑 ) = 2∆푦푥 − 2∆푥푦 − 푅
where푅 = 2∆푦 + 2∆푥(푏 − 1)
The sign of 푝 , is the same as the sign of 푑 − 푑 , since ∆푥 > 0 for our example. Parameter
R is constant and has the value 2∆푦 + 2∆푥(푏 − 1), which is independent of pixel position
and will be eliminated in the recursive calculations for 푝 . If the pixel at 푦 is closer to the line
path than the pixel at 푦 +1 (that is, 푑 < 푑 ), then decision parameter 푝 is negative. In that
case, we plot the lower pixel; otherwise, we plot the upper pixel.
Coordinate changes along the line occur in unit steps in either the x or y directions.
Therefore, we can obtain the values of successive decision parameters using incremental
integer calculations. At step k + 1, the decision parameter is evaluated from Eq. as
푝 = 2∆푦푥 − 2∆푥푦 − 푅
Subtracting 푝 −푝
푝 −푝 = 2∆푦(푥 − 푥 ) − 2∆푥(푦 − 푦 )
But 푥 = 푥 + 1
푝 = 푝 + 2∆푦 − 2∆푥(푦 − 푦 )
where the term 푦 − 푦 is either 0 or 1, depending on the sign of parameter 푝 . This
recursive calculation of decision parameters is performed at each integer x position, starting at
Output Primitives Lecture: 3
Dr. Ayman Elshenawy Elsefy Page | 9
the left coordinate endpoint of the line. The first parameter, 푝 is evaluated from at the starting
pixel position (푥 , 푦 ) and with m evaluated as ∆∆
:
푝 = 2∆푦 − ∆푥
We can summarize Bresenham’s line drawing for a line with a positive slope less than 1 in
the following listed steps. The constants 2∆푦 and 2∆푦 − 2∆푥 are calculated once for each line
to be scan converted, so the arithmetic involves only integer addition and subtraction of these
two constants.
Bresenham's Line-Drawing Algorithm for |풎| < ퟏ 1. Input the two line endpoints and store the left endpoint in (푥 , 푦 ) 2. Load (푥 ,푦 ) into the frame buffer; that is, plot the first point. 3. Calculate constants 훥푥,훥푦, 2훥푦, 푎푛푑2훥푦 − 2훥푥, and obtain the starting value for the
decision parameter as 푝 = 2∆푦 − ∆푥 4. At each 푥 along the line, starting at k = 0, perform the following test: If 푝 < 0 , the next point to plot is (푥 ,푦 ) and
푝 = 푝 + 2∆푦 Otherwise, the next point to plot is (푥 ,푦 ) and
푝 = 푝 + 2∆푦 − 2∆푥 5. Repeat step 4 훥푥 times.
An implementation of Bresenham’s line drawing for slopes in the range |m| < 1 is given in
the following procedure. Endpoint pixel positions for the line are passed to this procedure, and
pixels are plotted from the left endpoint to the right endpoint.
#Include <device.h> void lineBresenham (int xa, int ya , int xb, int yb) { int dx = abs ( xa - xb ) , dy = abs (ya - yb); int p = 2 * dy - d x ; int twoDy = 2 * dy, twoDyDx = 2 * (dy - Ax); int x , y, xEnd: /* Determine which point to use a s start, which as end * / if (xa > xb ) {
x = xb; Y = yb; xEnd = xa; } else{
x = xa; Y = ya; xEnd = xb; } setpixel (x, y); while (x < xEnd) {
x++;
Output Primitives Lecture: 3
Dr. Ayman Elshenawy Elsefy Page | 10
if (p < 0) { p += twoDy; } else { y++; p+= twoDyDx; } setpixel ( x , y); }
}
3. Digital Differential Analyzer (DDA) algorithm
This algorithm samples the line at unit interval in one coordinate and determines the corresponding integer values nearest the line path for other coordinates.
The equation of the line is 풚 = 풎풙 + 풄,풎 = 풚ퟐ 풚ퟏ풙ퟐ 풙ퟏ
, For any interval ∆풙, the corresponding interval is given by ∆풚 = 풎 × ∆풙
Case 1: |풎| < ퟏ
The sampling is done using x axis, ∆풙 = ퟏ풂풏풅∆풚 = 풎 then: 풙풊 ퟏ = 풙풊 + ퟏ Then calculate ∆풚 = 풎 풚풊 ퟏ = 풚풊 + ∆풚 = 풚풊 + 풎
Case 1: |풎| > ퟏ
The sampling is done using y axis, ∆풚 = ퟏ풂풏풅∆풙 = ퟏ/풎 then
Output Primitives Lecture: 3
Dr. Ayman Elshenawy Elsefy Page | 11
풚풊 ퟏ = 풚풊 + ퟏ Then calculate ∆풙 = ퟏ/풎 퐱풊 ퟏ = 퐱풊 + ∆퐱 = 퐱풊 + ( ퟏ
풎)
Algorithm
1. START 2. Get the values of the starting and ending co-ordinates i.e. , (푥 ,푦 ) and (푥 ,푦 ). 3. Find the value of slope m 푚 = ∆
∆=
4. If |푚| ≤ 1 then 훥푥 = 1,훥푦 = 푚훥푥 푥 + 1=푥 + 1,푦 + 1 = 푦 + 푚
5. If |푚| ≥ 1 then 훥푦 = 1,훥푥 = 훥푦/푚 푥 + 1 = 푥 + 1/푚,푦 + 1 = 푦 + 1
6. STOP
/**** Program to Draw a Line using DDA Algorithm ****/ #include <stdio.h> #include <dos.h> #include <graphics.h> void lineDDA(int, int, int, int); void main() { int x1, y1, xn, yn; int gd = DETECT, gm; initgraph(&gd, &gm, ""); printf("Enter the starting coordinates of line: "); scanf("%d %d", &x1, &y1); printf("Enter the ending coordinates of line: "); scanf("%d %d", &xn, &yn); lineDDA(x1, y1, xn, yn); getch(); } void lineDDA(int x1, int y1, int xn, int yn) { int dx, dy, m, i; m = (yn-y1)/(xn-x1); for (i=x1; i<=xn; i++) { if (m <= 1) { dx = 1; dy = m * dx; } else { dy = 1; dx = dy / m; } x1 = x1 + dx; y1 = y1 + dy; putpixel(x1, y1, RED); delay(20);
} }
Output Primitives Lecture: 3
Dr. Ayman Elshenawy Elsefy Page | 12
Figure 4-8
Comments:
1. It is faster to calculate pixel position.
2. Due to propagation of round off errors due to successive addition the calculated pixel
may shift among from the true line path.
Sheet 3 1. Describe how a point can be represented in display device?
2. Define a graphical line and how it can be displayed on a specific display device?
3. Explain the Basic concept of drawing a line using the brute force algorithm?
4. What are the main disadvantages of the brute force algorithm and how can we solve it?
5. For the Bresenham's line drawing algorithm :
a. Explain the basic concept of the Presenham algorithm?
b. Write the Algorithm
c. Write a c++ implementation for this algorithm.
6. For the DDA line drawing algorithm :
a. Explain the basic concept.
b. Write the Algorithm
c. Write a c++ implementation for this algorithm.
7. Write a Bresenham's line algorithm for line where |m| ≤ 1. Digitize a line with end
points (20, 10) and (30, 18).