0% found this document useful (0 votes)
23 views

Graphics Lecture 02

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Graphics Lecture 02

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

IT-331 ( Computer Graphics

1)

Lecture 2
Line - Drawing Algorithms
DDA

Dr. Mohammed El-Said


Assistant Professor, Computer Science Dept.,
Faculty of Computers & Artificial Intelligence,
Helwan University - Cairo, Egypt.
1
Basic Elements

Two basic elements


– Points
– Vectors

• Points are associated with locations in space


• Vectors have magnitude and direction
– represent displacements between points, or directions
Basic Elements

• Points, vectors, and operators that combine them are the


common tools for solving many geometric problems that
arise in
– Geometric Modeling,
– Computer Graphics,
– Animation,
– Visualization, and
– Computational Geometry.
COORDINATE REFERENCE FRAMES

• Scan-line algorithms for the graphics primitives use the coordinate


descriptions to determine the locations of pixels
– E.g., given the endpoint coordinates for a line segment, a display
algorithm must calculate the positions for those pixels that lie
along the line path between the endpoints.
• Since a pixel position occupies a finite area of the screen
– the finite size of a pixel must be taken into account by the
implementation algorithms.
– for the present, we assume that each integer screen position
references the centre of a pixel area.
COORDINATE REFERENCE FRAMES

• Once pixel positions have been identified the color values must be
stored in the frame buffer
• Assume we have available a low-level procedure of the form

setPixel (x, y);


stores the current color setting into the frame buffer at integer position
(x, y), relative to the position of the screen-coordinate origin

getPixel (x, y, color);


retrieves the current frame-buffer setting for a pixel location;
• parameter color receives an integer value corresponding to the
combined RGB bit codes stored for the specified pixel at position (x,y).
• additional screen-coordinate information is needed for 3D scenes. For
a two-dimensional scene, all depth values are 0.
LINE-DRAWING ALGORITHMS

A straight-line segment in a scene is defined by the


coordinate positions for the endpoints of the segment.
1. 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.
2. Then the line color is loaded into the frame buffer at the
corresponding pixel coordinates.
3. Reading from the frame buffer, the video controller
plots the screen pixels.
– This process digitizes the line into a set of discrete integer
positions that, in general, only approximates the actual line
path.
LINE-DRAWING ALGORITHMS
• On raster systems, lines are plotted with pixels, and step
sizes in the horizontal and vertical directions are
constrained by pixel separations.

• That is, we must "sample" a line at discrete positions and


determine the nearest pixel to the line at sampled
position.
– Sampling is measuring the values of the function at equal
intervals

• Idea: A line is sampled at unit intervals in one coordinate


and the corresponding integer values nearest the line
path are determined for the other coordinate.
Towards the Ideal Line
• We can only do a discrete approximation

• Illuminate pixels as close to the true path as


possible, consider bi-level display only
– Pixels are either lit or not lit
• In the raster line alg.,
– we sample at unit intervals and
– determine the closest pixel position to the specified
line path at each step
What is an ideal line
• Must appear straight and continuous
– Only possible with axis-aligned and 45o lines
• Must interpolate both defining end points
• Must have uniform density and intensity
– Consistent within a line and over all lines
– What about anti-aliasing ?
• Aliasing is the jagged edges on curves and diagonal lines in a
bitmap image.
• Anti-aliasing is the process of smoothing out those jaggies.
– Graphics software programs have options for anti-aliasing text and
graphics.
– Enlarging a bitmap image accentuates the effect of aliasing.
• Must be efficient, drawn quickly
– Lots of them are required
Simple Line

The Cartesian slope-intercept


equation for a straight line is :
y = mx + b
with m as the slope of the line
and b as the y intercept.
Simple approach:
• increment x, solve for y
Line-Drawing Algorithms:

DDA

Bresenham’s Midpoint
Algorithm
Algorithms for displaying lines are based on the Cartesian
slope-intercept equation
y = m.x + b
where m and b can be calculated from the line endpoints:
m = (y1-y0) / (x1-x0)
b = y0 - m. x0

For any x interval x along a line the corresponding y interval


y = m.x

y1

y0

x0 x1
Simple Line

Based on slope-intercept
algorithm from algebra:
y = mx + b
Simple approach:
increment x, solve for y
Floating point arithmetic
required
Does it Work?

It works for lines with a slope of


1 or less,
but doesn’t work well for lines
with slope greater than 1 – lines
become more discontinuous in
appearance and we must add
more than 1 pixel per column to
make it work.
Solution? - use symmetry.
Modify algorithm per octant

OR, increment along x-axis if dy<dx else increment along


y-axis
DDA Algorithm

• The digital differential analyser (DDA) is a scan-conversion line


algorithm based on using x or y.

• A line is sampled at unit intervals in one coordinate and the


corresponding integer values nearest the line path are determined for
the other coordinate.
Line with positive slope
• If m<=1,
– Sample at unit x intervals (dx=1)
– Compute successive y values as
• yk+1=yk+m 0<=k<=xend-x0
• Increment k by 1 for each step
• Round y to nearest integer value.

• If m>1,
– Sample at unit y intervals (dy=1)
– Compute successive x values as
• xk+1=xk+1/m 0<=k<=yend-y0
• Increment k by 1 for each step
• Round x to nearest integer value.
inline int round (const float a) { return int (a + 0.5); }

void lineDDA (int x0, int y0, int xEnd, int yEnd)
{
int dx = xEnd - x0, dy = yEnd - y0, steps, k;
float xIncrement, yIncrement, x = x0, y = y0;

if (fabs (dx) > fabs (dy))


steps = fabs (dx);
else
steps = fabs (dy);
xIncrement = float (dx) / float (steps);
yIncrement = float (dy) / float (steps);

setPixel (round (x), round (y));


for (k = 0; k < steps; k++) {
x += xIncrement;
y += yIncrement;
setPixel (round (x), round (y));
}
}
3
DDA algorithm
• Need a lot of floating point arithmetic.
– 2 ‘round’s and 2 adds per pixel.

• Is there a simpler way ?


• Can we use only integer arithmetic ?
– Easier to implement in hardware.

You might also like