Lecture 3 DDA
Lecture 3 DDA
1
2D Graphics Primitives
2D Graphics Primitives
• Graphics drawing is based on basic geometric
structures called graphics primitives
• Points
• Lines
• Circles
• Conic Sections
Towards the Ideal Line
• We can only do a discrete approximation
Based on slope-intercept
algorithm from algebra:
y = mx + b
Simple approach:
increment x, solve for y
Floating point arithmetic
required
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
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
• 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;