output primitives - eaymanelshenawy.files.wordpress.com · 2/3/2016 · each output primitive is...

12
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.

Upload: vokhuong

Post on 04-Apr-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Output Primitives - eaymanelshenawy.files.wordpress.com · 2/3/2016 · Each output primitive is specified with input ... application program into appropriate operations for the

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.

Page 2: Output Primitives - eaymanelshenawy.files.wordpress.com · 2/3/2016 · Each output primitive is specified with input ... application program into appropriate operations for the

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.

Page 3: Output Primitives - eaymanelshenawy.files.wordpress.com · 2/3/2016 · Each output primitive is specified with input ... application program into appropriate operations for the

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.

Gray-scale typically has one byte per pixel, for 28 = 256 intensities.

Page 4: Output Primitives - eaymanelshenawy.files.wordpress.com · 2/3/2016 · Each output primitive is specified with input ... application program into appropriate operations for the

Output Primitives Lecture: 3

Dr. Ayman Elshenawy Elsefy Page | 4

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−𝑦1

𝑥2−𝑥1 (2)

And 𝑏 = 𝑦1 − 𝑚 𝑥1 (3)

Figure (4-4): Line path between endpoint positions (x1, y1) and (x2, y2)

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 (𝑥1, 𝑦1) and (𝑥2, 𝑦2) 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:

Page 5: Output Primitives - eaymanelshenawy.files.wordpress.com · 2/3/2016 · Each output primitive is specified with input ... application program into appropriate operations for the

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:

Page 6: Output Primitives - eaymanelshenawy.files.wordpress.com · 2/3/2016 · Each output primitive is specified with input ... application program into appropriate operations for the

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 (𝑥𝑖+1, 𝑦𝑖) or (𝑥𝑖+1, 𝑦𝑖+1). Why? - Because of the

Page 7: Output Primitives - eaymanelshenawy.files.wordpress.com · 2/3/2016 · Each output primitive is specified with input ... application program into appropriate operations for 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 𝑥𝑖+1 (we’ll call

that 𝑦𝑟𝑒𝑎𝑙) and the y values represented by the pixels. 𝛥𝑦1 and 𝛥𝑦0 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 (𝑥0, 𝑦0) 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 (𝑥𝑖+1, 𝑦𝑖) and (𝑥𝑖+1, 𝑦𝑖+1). At sampling position 𝑥𝑖+1,

we label vertical pixel separations from the mathematical line path as 𝑑1, and 𝑑2. They

coordinate on the mathematical line at pixel column position 𝑥𝑘+1 is calculated as :

𝑦𝑟𝑒𝑎𝑙 = 𝑚(𝑥𝑖+1) + 𝑐

Page 8: Output Primitives - eaymanelshenawy.files.wordpress.com · 2/3/2016 · Each output primitive is specified with input ... application program into appropriate operations for the

Output Primitives Lecture: 3

Dr. Ayman Elshenawy Elsefy Page | 8

𝑑1 = 𝑦𝑟𝑒𝑎𝑙 − 𝑦𝑖 = 𝑚(𝑥𝑖+1) + 𝑐 − 𝑦𝑖

𝑑2 = 𝑦𝑖+1 − 𝑦𝑟𝑒𝑎𝑙 = 𝑦𝑖+1 − 𝑚(𝑥𝑖+1) + 𝑐

The difference between these two separations is

𝑑1 − 𝑑2 = 2𝑚(𝑥𝑖+1) − 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:

𝑝𝑘 = ∆𝑥(𝑑1 − 𝑑2) = 2∆𝑦𝑥𝑖 + 2∆𝑦 − 2 ∆𝑥 𝑦𝑖 + 2 ∆𝑥 𝑏 − ∆𝑥

𝑝𝑖 = ∆𝑥(𝑑1 − 𝑑2) = 2∆𝑦𝑥𝑖 − 2 ∆𝑥 𝑦𝑖 − 𝑅

where 𝑅 = 2∆𝑦 + 2 ∆𝑥 (𝑏 − 1)

The sign of 𝑝𝑖, is the same as the sign of 𝑑1 − 𝑑2, 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, 𝑑1 < 𝑑2), 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

𝑝𝑖+1 = 2∆𝑦𝑥𝑖+1 − 2 ∆𝑥 𝑦𝑖+1 − 𝑅

Subtracting 𝑝𝑖+1 − 𝑝𝑖

𝑝𝑖+1 − 𝑝𝑖 = 2∆𝑦(𝑥𝑖+1 − 𝑥𝑖) − 2∆𝑥(𝑦𝑖+1 − 𝑦𝑖)

But 𝑥𝑖+1 = 𝑥𝑖 + 1

𝑝𝑖+1 = 𝑝𝑖 + 2∆𝑦 − 2∆𝑥(𝑦𝑖+1 − 𝑦𝑖)

where the term 𝑦𝑖+1 − 𝑦𝑖 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

the left coordinate endpoint of the line. The first parameter, 𝑝𝑖 is evaluated from at the starting

pixel position (𝑥0, 𝑦0) and with m evaluated as ∆𝑦

∆𝑥 :

Page 9: Output Primitives - eaymanelshenawy.files.wordpress.com · 2/3/2016 · Each output primitive is specified with input ... application program into appropriate operations for the

Output Primitives Lecture: 3

Dr. Ayman Elshenawy Elsefy Page | 9

𝑝0 = 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 (𝑥0, 𝑦0)

2. Load (𝑥0, 𝑦0) 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 𝑝0 = 2∆𝑦 − ∆𝑥

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

If 𝑝𝑘 < 0 , the next point to plot is (𝑥𝑘+1, 𝑦𝑘) and

𝑝𝑘+1 = 𝑝𝑘 + 2∆𝑦

Otherwise, the next point to plot is (𝑥𝑘+1, 𝑦𝑘+1) and

𝑝𝑘+1 = 𝑝𝑘 + 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++;

if (p < 0) { p += twoDy; }

else { y++; p+= twoDyDx; }

setpixel ( x , y);

Page 10: Output Primitives - eaymanelshenawy.files.wordpress.com · 2/3/2016 · Each output primitive is specified with input ... application program into appropriate operations for the

Output Primitives Lecture: 3

Dr. Ayman Elshenawy Elsefy Page | 10

}

}

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 = mx + c , m =y2−y1

x2−x1 , For any interval ∆x, the corresponding

interval is given by ∆y = m × ∆x

Case 1: |𝒎| < 𝟏

The sampling is done using x axis, ∆x = 1 and ∆y = m then:

𝒙𝒊+𝟏 = 𝒙𝒊 + 𝟏

Then calculate ∆y = m

yi+1 = yi + ∆y = yi + m

Case 1: |m| > 1

The sampling is done using y axis, ∆y = 1 and ∆x = 1/m then

yi+1 = yi + 1

Then calculate ∆x = 1/m

Page 11: Output Primitives - eaymanelshenawy.files.wordpress.com · 2/3/2016 · Each output primitive is specified with input ... application program into appropriate operations for the

Output Primitives Lecture: 3

Dr. Ayman Elshenawy Elsefy Page | 11

xi+1 = xi + ∆x = xi + (1

m)

Algorithm

1. START

2. Get the values of the starting and ending co-ordinates i.e. , (𝑥1, 𝑦1) and (𝑥2, 𝑦2).

3. Find the value of slope m 𝑚 =∆𝑦

∆𝑥=

𝑦2−𝑦1

𝑥2−𝑥1

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);

}

}

Page 12: Output Primitives - eaymanelshenawy.files.wordpress.com · 2/3/2016 · Each output primitive is specified with input ... application program into appropriate operations for the

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).