Raster Lines

Raster Lines
6
0
0
16
Line Axioms
1. Lines should look straight.
2. Lines should terminate accurately.
3. Lines should have constant density, i.e. they
should have the same numbers of pixels per unit length everywhere.
Drawing Lines
LineFirstTry(x1,y1,x2,y2)
{
m = (y2-y1)/(x2-x1);
for (x = x1;x <= x2; x++)
{
y = m*(x-x1)+y1;
setPixel(round(x),round(y),color);
}
}
Drawing Lines
LineSecondTry(x1,y1,x2,y2)
{
m = (y2-y1)/(x2-x1);
y = y1;
for (x = x1;x <= x2; x++)
{
setPixel(x,round(y),color);
y = y+m;
}
}
Drawing Lines
LineThirdTry(x1,y1,x2,y2)
{
m = (y2-y1)/(x2-x1);
e = m - 0.5;
y = y1;
for (x = x1;x <= x2; x++)
{
setPixel(x,round(y),color);
if (e>0) {
y++;
e--;
}
e += m;
}
}
Drawing Lines
Bresenham's Algorithm
Line(x1,y1,x2,y2)
// Precondition: dx = x2-x1 >0 dy = y2-y1>0
//
m = dy/dx , 0 < m < 1
{
dx = x2-x1; dy = y2-y1;
d = 2*dy – dx;
incrNeg = 2*dy; incrPos = 2*(dy-dx);
x = x1; y = y1;
setPixel(x,y,color);
while (x < x2) {
if (d <= 0)
d += incrNeg;
else {
d += incrPos;
y++;
}
x++;
setPixel(x,y,color);
}
}
Bresenham Example
6
0
0
16
x1 = 0 y1 = 0 x2 = 16 y2 = 6 dx = 16 dy = 6
incrNeg = 12 incrPos = -20 d = -4
if d <= 0 d += incrNeg
else d += incrPos and y++
d
-4
8
-12
0
12
-8
4
-16
-4
8
-12
0
12
-8
4
-16
-4
x
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
y
0
0
1
1
1
2
2
3
3
3
4
4
4
5
5
6
6