[
next
] [
prev
] [
prev-tail
] [
tail
] [
up
]
13.7
Scan Converting Lines
Given:
p
1
= (
x
1
,y
1
) and
p
2
= (
x
2
,y
2
)
Objective: Find pixels which approximate the line segment (
p
1
,p
2
)
Basic Algorithm (Digital Differential Analyzer, DDA):
slop
:=
(y2
-
y1)
/
(x2
-x1)
y
:=
y1
FOR
x
:=
x1
TO
x2
DO
PaintPixel(x,round(y))
y
:=
y
+
slop
END
Gaps occur for slop
>
1
Bresenham’s Midpoint Line Algorithm
(
x,y
) -- intersection point of the given line with a vertical grid line.
-- vertical distance of current point (
x,y
) from the “midpoint” above it.
Y -- pixel (x,Y) corresponding to (x,y)
For the next intersection point we have
x
+1
:=
If (
x
+1
>
0)
real
variable:
D
dx
:=
x2
-
x1
dy
:=
y2
-
y1
D
:=
-0.5
x
:=
x1
Y
:=
y1
FOR
i
:=
x1
TO
x2
DO
PaintPixel(x,Y)
x
:=
x
+
1
D
:=
D
+
dy
/
dx
IF
D>0
THEN
Y
:=
Y
+
1
D
:=
D
-
1
END
END
Remove division and floating point arithmetics
dx
:=
x2
-
x1
dy
:=
y2
-
y1
D
:=
-dx
*
(2
*
dx)
x
:=
x1
Y
:=
y1
FOR
i
:=
x1
TO
x2
DO
PaintPixel(x,Y)
x
:=
x
+
1
D
:=
D
+
2
*
dy
*
(2
*
dx)
IF
D>0
THEN
Y
:=
Y
+
1
D
:=
D
-
2
*
dx
*
(2
*
dx)
END
END
Generalization
1
2
switch the roles of x & y
...
...
[
next
] [
prev
] [
prev-tail
] [
front
] [
up
]