Graphics Lecture 02
Graphics Lecture 02
1)
Lecture 2
Line - Drawing Algorithms
DDA
• 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
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
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?
• 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;