Bresenham Line

March 30, 2018 | Author: Hussein Adly | Category: Mathematics, Physics & Mathematics, Areas Of Computer Science, Algorithms, Mathematical Concepts
Share Embed Donate


Short Description

no much...

Description

Chapfer Output Primitives

When the start endpoint is at the right (for the same slope), we set Ax = -1 and obtain y positions from Eq. 3-8. Similarly, when the absolute value of a negative slope is w a t e r than 1, we use Ay = -1 and Eq. 3-9 or we use Ay = 1and Eq.3-7. This algorithm is summarized in the following procedure, which accepts as input the two endpolnt pixel positions. Horizontal and vertical differences between the endpoint positions are assigned to parameters dx and dy. The difference with the greater magnitude determines the value of parameter steps.Starting with pixel position (x,, yo), we determine the offset needed at each step to generate the next pixel position along the line path. We loop through this process steps times. If the magnitude of dx is greater than the magnitude of dy and xa is less than xb, the values of the increments in the x and y directions are 1 and m, respectively. If the greater change is in the x direction, but xa is greater than xb, then the decrements - 1 and - m are used to generate each new point on the line. Otherwise, we use a unit increment (or decrement) in they direction and an x increment (or decrement) of l / m . -

-

-

-

--

-

#include 'device. h"

void lineDDA (int xa, int ya, int xb, int yb) (

-

int dx = xb - xa, dy = yb ya, steps, k; float xrncrement, yIncrement, x = xa, y = ya; i t (abs (dx) > abri (dyl) steps = abs ( d x ) ; else steps = abs dy); xIncrement = dx i (float) sceps; yIncrement = dy 1 (float) steps setpixel (ROUNDlxl, ROUND(y)) : for (k=O; k x b ) ( /'

x = xb;

Y = yb; xEnd = x a ; )

!

else I

~ine-DrawingAlgorithms

x = xa; Y = ya; xEnd = xb; 1 setpixel ( x , y); while (x < xEnd) x++; if l p < 0 ) $3 + = twoDy; else [

(

y++;

g += twoDyDx; )

setpixel ( x , y); 1

1

Bresenham's algorithm is generalized to lines with arbitrary slope by considering the symmetry between the various octants and quadrants of the xy plane. For a line with positive slope greater than 1, we intelrhange the roles of the x and y directions. That is, we step along they direction in unit steps and calculate successive x values nearest the line path. Also, we could revise the program to plot pixels starting from either endpoint. If the initial position for a line with positive slope is the right endpoint, both x and y decrease as we step from right to left. To ensure that the same pixels are plotted regardless of the starting endpoint, we always choose the upper (or the lower) of the two candidate pixels whenever the two vertical separations from the line path are equal (d, = dJ. For negative slopes, the procedures are similar, except that now one coordinate decreases as the other increases. Finally, specla1 cases can be handled separately: Horizontal lines (Ay = 01, vertical lines (Ar = O), and diagonal lines with IAr 1 = I Ay 1 each can be loaded directly into the frame buffer without processing them through the line-plotting algorithm. Parallel Line Algorithms

The line-generating algorithms we have discussed so far determine pixel positions sequentially. With a parallel computer, we can calculate pixel positions

Figure 3-9

Pixel positions along the line path between endpoints (20.10) and (30,18), plotted with Bresenham's

hne algorithm.

View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF