Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 10

WHERE TO DRAW A LINE??

 Line drawing is accomplished by calculating


intermediate positions along the line path between
specified end points.
 Precise definition of line drawing
Given two points P and Q in the plane, both
with integer coordinates, determine which
pixels on a raster screen should be on in order
to make a picture of a unit-width line segment
starting from P and ending at Q.
6 (3, 3)
5
4
3
2
1
0
0 1 2 3 4 5 6
Line drawing (cont)
 The thinnest line is of one-pixel wide. We will
concentrate on drawing a line of 1 pixel resolution.
 The Cartesian slope-intercept equation for a straight

line is
y= m. x + b
m is the slope of the line and b is the y intercept.
Given the endpoints of a line segment.
m = y2-y1 / x2-x1
b= y1-m.x1
Line Drawing (cont)
 Also for any given interval ∆x along a line, we can
compute the corresponding y interval ∆y from
∆y= m. x
 Similarly we can obtain the x interval ∆x
corresponding to a specified ∆y as
∆x= ∆y / m
 These equations form the basis for determining
deflection voltages in analog devices.
Line Drawing (cont)
 Also , for any given x interval ∆x along a line, we
can compute the corresponding y interval ∆y from
∆y= m. ∆ x
 These equations form the basis for determining
deflection voltages in analog devices.
 On Raster systems, lines are plotted with pixels,
and step sizes in the horizontal and vertical
directions are constrained by pixel separations.
Hence we ought to “sample” a line at discrete
positions and determine the nearest pixel to the
line at each sampled position.
Symmetry
 If we could draw lines with positive slope (0<=slope<=1) we
would be done.
 For a line with negative slope (0>=slope>=-1)

We negate all Y values


 For a line with slope > 1 or slope <-1

we just swap x and y axes


(-y,x) (y,x)

(x,-y) (x,y)
450

(-x,-y) (x,-y)

(-y,-x) (y,-x)
Code for drawing a line
……….

Invert_y_draw(int x,int y) If(0 <= slope <= 1)

draw_pixel(x,-y) draw_fn= draw_pixel


draw_lne(Px, PY, QX, QY, draw_fn)
Swap_xy_draw(int x,int y)
Else if (-1 <= slope <= 0)
draw_pixel(y,x)
draw_fn = invert_y_draw
Swap_xy_invert_y_draw(int x,int y) Draw_line(PX, -PY, QX, -QY, draw_fn)

draw_pixel(y,-x) Else if (1 < slope)

draw_fn= swap_xy_draw
Draw_line(PY,PX,QY,QX)
Else
Draw_fn=swap_xy_invert_y_draw
Draw_line(-PY,PX,QY,-QX, draw_fn)
DDA ALGORITHM
 The digital differential analyzer (DDA) samples the line at unit
intervals in one coordinate corresponding integer values nearest
the line path of the other coordinate.

 The following is thus the basic incremental scan-


conversion(DDA) algorithm for line drawing
for x from x0 to x1
Compute y=mx+b
Draw_fn(x, round(y))
 Major deficiency in the above approach :
 Uses floats

 Has rounding operations


DDA Illustration

Desired Line
(xi+1, Round(yj+m))

(xi, yj)
(xi+1, yj+m)

(xi, Round(yj))

y2

y1

x1 x2

You might also like