0% found this document useful (0 votes)
78 views7 pages

Lines 03

The document discusses algorithms for scan converting 2D lines and circles. It describes the basic naive line algorithm and introduces Bresenham's line algorithm which uses integer arithmetic. It also covers the midpoint circle algorithm, initializing it to draw one octant based on the sign of the decision variable f at the midpoint.

Uploaded by

Erol Kerem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views7 pages

Lines 03

The document discusses algorithms for scan converting 2D lines and circles. It describes the basic naive line algorithm and introduces Bresenham's line algorithm which uses integer arithmetic. It also covers the midpoint circle algorithm, initializing it to draw one octant based on the sign of the decision variable f at the midpoint.

Uploaded by

Erol Kerem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

2D Line

Scan Conversion

Implicit representation:

Dx  E y  J

Explicit representation:

y mx B

Parametric representation:

x
P
y

(Chapter 3 in Foley & Van Dam)

P P0 (P1 P0)t t [0..1]


y
P1

y1
x0

x1

P0

Lines and Circles

y1  y0
x1  x0

y0

Scan Conversion - Lines

Scan Conversion - Lines

y = mx + B
Basic nave algorithm

y = mx + B

(x1,y1)

For x = x0 to x1
y = mx + B
PlotPixel (x,round(y))
end;

(x0,y0)

y -y
slope = m = x1 - x0
1
0
offset= B = y1-mx1

For each iteration: 1 float multiplication, 1 addition, 1 Round

Assume |m| d 1
Assume x0 d x1

4-connectivity

4-adjacency

8-connectivity

8-adjacency

An 8-conncected closed
curve with a hole

Incremental Algorithm:

A 4-connected open arc


with a hole

Symmetric Cases:
|m| t 1

yi+1 = mxi+1 + B = m(xi + 'x) + B = yi + m'x


if 'x = 1 then

yi+1 = yi + m

Algorithm
y=y0
For x = x0 to x1
PlotPixel(x,round(y))
y= y + m
end;

x = x0
For y = y0 to y1
PlotPixel(round(x),y)
x = x + 1/m
end;
Special Cases:
m = 1 (diagonals)
m = 0, f (horizontal, vertical)
Symmetric Cases:
if x0 > x1 for |m| d 1

or y0 > y1 for |m| t 1

swap((x0,y0),(x1,y1))

Basic Line Drawing:


For each iteration: 1 addition, 1 Round.
Drawback:
Accumulated error
float arithmetic
Round operations

Pseudo Code for Basic Line Drawing:


Assume x1>x0 and line slope absolute value is d 1

( xi+1,Round(yi+m) )
Line(x0,y0,x1,y1)
begin
float dx, dy, x, y, slope;
dx := x1-x0;
dy := y1-y0;
slope := dy/dx;
y := y0;
for x:=x0 to x1 do
begin
PlotPixel( x,Round(y) );
y := y+slope;
end;
end;

( xi, yi )

( xi,Round(yi) )

( xi+1, yi+m )

Bresenhams Line Algorithm

Midpoint (~Bresenham) Line Drawing


Assumptions:
x0 < x1 , y0 < y1
0 < slope < 1

yi+1

d2

d1

yi

NE
Q
M
xi

xi+1

y = m(xi + 1) + b
d1 = y - yi = m(xi +1) + b - yi
d2 = (yi + 1) - y = yi + 1 - m (xi + 1) - b
d1 - d2 > 0 ?

(xp,yp)

Given (xp,yp):
next pixel is E = (xp +1,yp) or NE = (xp+1,yp+1)
Bresenham: sign(M-Q) determines NE or E
M = (xp +1,yp +1/2)

The vertical distance is equivalent to the Euclidean distance

Bresenhams Line Algorithm

Const1 = 2dy;
Const2 = 2dy - 2dx;
f = 2dy - dx;
set_pixel(x1,y1);
x = x1; y = y1;
while (x++ < x2){
if (f < 0)
f += Const1;
else {
f += Const2;
y++;
}
set_pixel(x,y);
}

Bresenhams Line Algorithm


d1 - d2 = 2m(xi + 1) - 2yi + 2b -1
d1 - d2 = 2(dy/dx)(xi + 1) - 2yi + 2b -1
dx(d1-d2) = 2dy*xi + 2dy - 2dx*yi + 2dx*b - dx
fi = dx(d1-d2)
fi+1 - fi = 2dy(xi+1 - xi) - 2dx(yi+1 - yi)
If y is incremented then fi+1 = fi + 2dy-2dx
else fi+1 = fi + 2dy

Offsets

Bresenhams Line Algorithm


Const1 = 2dy;
Const2 = 2dy - 2dx;
p = A + n*y + x;
offset_h = sign(dx);
offset_d = sign(dx) + n*sign(dy)
f = 2dy - dx;
*p = color;
d8 = dx;
while (d8--){
if (f < 0){
f += Const1;
p += offset_h;
}
else {
f += Const2;
p += offset_d;
}
*p = color;
}

The image is a linear memory

l-n

Address = l

l-n-1

l+n

l-n+1

Mid-point

Mid-point

The line passes above M so it is a d


move to

In 8-connected choose either a h or d move


The midpoint M is located at (x +1,y +1/2)

Current pixel

Current pixel

Mid-point
In 4-connected choose either a h or v move
The midpoint M is located at (x + 1/2,y +1/2)

Mid-point
The line passes below M so it is a h
move to

Current pixel

Current pixel

Mid-point

Mid-point

The line passes Below M so it is a h


move to

The line passes above M so it is a v


move to

Current pixel

Current pixel

Midpoint Line Drawing (cont.)


How to update f - the value at M

y=

dy
x+B
dx

Implicit form of a line:


M

M
f(x,y) = ax + by + c = 0
f(x,y) = dy x - dx y + B dx = 0

If

was chosen
Mi = (x,y)
Mi+1 = (x+1,y), thus
f = ax + by + c, and

f(x,y) < 0

f(x,y) = 0

f i 1

= a(x+1) + by + c, or

f i 1 = f i

f(x,y) > 0

+ a.

Since a is constant we denote


it with 'h, and we have:

Decision Variable :
f = f(M) = f(xp +1,yp +1/2) = a(xp +1) + b(yp +1/2) + c

f += 'h

The sign of f defines the move

Incremental Algorithm:
How to update f - the value at M

Initialization:
First point = (x0,y0), first MidPoint = (x0+1,y0+1/2)
fstart = f(x0 +1,y0+1/2) = a(x0 +1) + b(y0 +1/2) +c
= ax0 + by0 + c + a + b/2
= f(x0,y0) + a + b/2 = a + b/2

dstart =dy - dx/2

If

Enhancement:
To eliminate fractions, define:
f(x,y) = 2(ax + by + c) = 0

dstart =2dy - dx

was chosen
Mi = (x,y)
Mi+1 = (x+1,y+1), thus
f = ax + by + c, and
i

f i 1

= a(x+1) + b(y+1) + c, or

f i 1 = f i

+ a + b.

Since a and b are constants


we denote their sum with 'd,
and we have:
f += 'd

Midpoint Line Drawing Summary


The sign of f(x0+1,y0+1/2) indicates
whether to move East or North-East.
At the beginning d=f(x0+1,y0+1/2)=2dy-dx.
The increment in d (after this step) is:
If we moved East: 'E=2dy
If we moved North-East: '1(=2dy-2dx
Comments:
Integer arithmetic (dx and dy are
integers).
One addition for each iteration.
No accumulated errors.

Mid-point Line Algorithm

'h = 2dy;

'd = 2dy - 2dx;


f = 2dy - dx;
set_pixel(x1,y1);
x = x1; y = y1;
while (x++ < x2){
if (f < 0)
f += 'h;
else {
f += 'd;
y++;
}
set_pixel(x,y);
}

Scan Conversion - Circles

Drawing Circles
Implicit representation (centered
at the origin with radius R):
x2  y2  R2

Basic Algorithm
For x = -R to R
y = sqrt(R2-x2)
PlotPixel(x,round(y))
PlotPixel(x,-round(y))
end;

Explicit representation:
y r R2  x2

Parametric representation:
x

y

R cos t

R sin t

Comments:
square-root operations are expensive.
Float arithmetic.
Large gap for x values close to R.

t [0..2S]

Exploiting Eight-Way Symmetry


For a circle centered at the origin:
If (x,y) is on the circle then (y,x) (y,-x) (x,-y) (-x,-y) (-y,-x) (-y,x) (-x,y)

are on the circle as well.


Therefore we need to compute only
one octant (45o) segment.

(-x,y) (x,y)

(-y,x)

(y,x)

(-y,-x)

(y,-x)

(-x,-y)

Threshold Criteria

(x,-y)

Circle Midpoint (for one octant)


(The circle is located at (0,0) with radius R)

d(x,y)=f(x,y) = x2 + y2 -R2 = 0
f(x,y) = 0

f(x,y) < 0

f(x,y) > 0

We start from (x0,y0)=(0,-R).


One can move either h or d.
Again, f(x,y) will be a decision variable at
the midpoint.

Mid-point

How to update f - the value at M


If

was chosen, as in lines we update M

The arc passes above M so it is a v


move to

Mi+1 = (x+1,y), thus

f i 1

= (x+1) + y - R,

and

f i 1= f i + 2x + 1.
Now, 'h is NOT a constant ,
but a linear term, so we update
it as well:

'h+1 = 2(x+1) + 1, which is

Current pixel

'h+1 = 'h + 2.

Similarly if

was chosen

Mid-point circle (for one octant) Algorithm


One may mirror (*), creating the other seven octans.

Initialize

'h and 'd (home exercise)

2
2
2
f = (1 / 2)  ( R  1 / 2)  R

set_pixel(x = x1,y = y1);


while (in the octant){
if (f < 0) {
f += 'h; 'h += 2;
X++;
else {
f += 'v; 'h += 2;
Y++;
}
set_pixel(x,y);
}

You might also like