Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
7 views

Lecture 5 Line Drawing Algos

Uploaded by

jaisika2222
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Lecture 5 Line Drawing Algos

Uploaded by

jaisika2222
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

Output Primitives

Output Primitives
• Points
• Lines
– DDA Algorithm
– Bresenham’s Algorithm
A picture can be described as:
• The set of intensities for the pixel position in the display.
OR
• As a set of complex objects, such as trees and terrain or
furniture and walls, positioned at specified coordinate locations
within the scene.

Shapes and colors of the objects can be described:


• As internally with pixel arrays
OR
• Sets of basic geometric structures, such as straight line
segments and polygon color areas.
The scene is converted by:
• Loading the pixel arrays into frame buffer.
OR
• Scan converting the basic geometric-structure specifications
into pixel patterns.
• Output primitives - Basic geometric structures.

• A scene is described in such a way that the graphics programming


packages provide functions to describe a scene in terms of basic
geometric structures called output primitives and to group sets of
o/p primitives into more complex structures

• Each output primitive is specified with the input coordinate data


and other information about the way that object is to be displayed.

• Points and straight line segments are the simplest geometric


components of pictures.
• Additional output primitives that can be used to construct a
picture include circles and other conic sections, quadric
surfaces, spline curves and surfaces, polygon color areas, and
character strings.
Points- Point plotting is accomplished by converting a single
coordinate position in an application program into appropriate
operations for the output device in use.

• A random-scan system stores point-plotting instructions in the


display list, and coordinate values in these instructions are
converted to deflection voltages that position the electron beam
at the screen locations to be plotted during each refresh cycle.

• In raster scan system, point is plotted by setting the bit value


corresponding to a specified screen position within the frame
buffer to 1. Then, as the electron beam sweeps across each
horizontal scan line, it emits a burst of electrons whenever a
value of 1 is encountered in the frame buffer.
• Single Coordinate Position
– Set the bit value(color code) corresponding to a specified screen
position within the frame buffer

y
setPixel (x, y)

cgvr.korea.ac.kr
Line drawing is accomplished by calculating intermediate
positions along the line path between two specified endpoint
positions.

An output device is directed to fill in these positions between


the endpoints.

• For analog devices,


 a straight line can be drawn smoothly from one end point to the
other.
 Linearly varying horizontal and vertical deflection voltages are
generated that are proportional to the required changes in the x and y
directions to produce the smooth line.
• For digital devices,
 a straight line segment is displayed by plotting discrete points
between two end points.
 Discrete coordinate positions along the line path are calculated from
the equation of line.
 For a raster video display, the line color is loaded into the frame
buffer at the corresponding pixel coordinates.
 Reading from the frame buffer, the video controller plots the screen
pixels.
 Screen locations are referenced with integer values, so plotted
positions may only approximate actual line positions between two
endpoints.
 A computed line positions, for example (10.48, 20.51) would be
converted to pixel position (10,21).
 This rounding of coordinate values to integers causes lines to be
displayed with a stairstep appearance “the jaggies”.

 The characteristic stairstep shape of raster lines is noticeable on


systems with low resolution.
 This can be improved somewhat by displaing them on high-
resolution systems.
Line Drawing Algorithms

The lines of this object However, they are


appears continuous made of pixels
Straight line segment with 4 sampling positions
along the x axis and y axis

y2

y1

x1 x2
• The Cartesian slope intercept equation for straight line is
y=mx+b
where m is slope of the line and b as the y intercept
• Let (x1,y1) & (x2,y2) be the two endpoints of the line, then
m=(y2-y1)/(x2-x1)
b= y1- mx1

( y2-y1) = m (x2 – x1)


m=(y2-y1)/(x2-x1) = ∆y/ ∆x
• For any given x interval ∆x along a line, compute the
corresponding y interval ∆y as ∆y = m ∆x
• Similarly the x interval is obtained as
∆x = ∆y /m
• The above equations form the basis for determining deflection
voltages.
– |m| < 1, ∆x can be set proportional to a small horizontal deflection
voltage and corresponding vertical deflection is set proportional to ∆y
– |m| > 1, ∆y can be set proportional to a small vertical deflection voltage
and corresponding horizontal deflection is set proportional to ∆x
– |m| =1, ∆x = ∆y and the horizontal and vertical deflection voltages are
equal, which leads to a smooth line with slope m.
Digital Differential Analyzer (DDA) Algorithm
• Intermediate Positions between Two Endpoints
– DDA, Bresenham’s line algorithms

Jaggies
= Aliasing
DDA Algorithm
• Digital Differential Analyzer
0 < Slope <= 1
• Unit x interval = 1
y2
y1

x1 x2

yk 1  yk  m
• Digital Differential Analyzer

Slope > 1
• Unit y interval = 1

y2

y1
x1 x2
1
xk 1  xk 
m
• Digital Differential
Analyzer
-1 <= Slope < 0
• Unit x interval = -1

y1
y2

x1 x2

yk 1  yk  m
• Digital Differential Analyzer

Slope < -1
• Unit y interval = -1

y2

y1
x1 x2
1
xk 1  xk 
m
Digital Differential Algorithm(DDA)
• Scan conversion algorithm based on calculating either ∆x or
∆y.
• Sample the line at unit intervals in one coordinate and
determine the corresponding integer values nearest the line
path for the other coordinate.
Case 1
Line with positive slope less than or equal to 1, we sample at
unit x intervals (∆x =1) and determine the successive y value as

∆y=m ∆x
Yk+1= Yk + m Yk+1- Yk =m ∆x
Yk+1- Yk =m (∆x =1)
• k takes integer values starting from 1 and increases by 1 until the
final point is reached.
• m can be any number between 0 and 1.
• Calculated y values must be rounded off to the nearest integer.
• For lines with positive slope greater than 1, sample at unit y
intervals (∆y = 1) and calculate each successive x value as
xk+1= xk + 1/m
• The above eqns are based on the assumption that lines are
processed from left to right. If the processing is reversed then

• ∆x = -1 & yk+1 = yk – m Slope >1

∆y = -1 & xk+1= xk-1/m Slope <1


Steps
1. P1 ( xa,ya) and P2 (xb,yb) are the two end points.

2. Horizontal and vertical differences between the endpoint positions are


computed & assigned to two parameters namely dx and dy.

3. The difference with greater magnitude determines the ‘value’ of


increments to be done. That means the number of times sampling has to
be done. This value is assigned to a parameter called ‘steps’.
4. Starting from the 1st pixel ,we determine the offset needed at each
step to generate the next pixel position along the line path. Call the
offset value as xincrement and yincrement.
The starting points are xa and ya.
Assign x =xa and y=ya
x= x+ xincr & y= y+ yincr

5. Loop through the process steps times, till the last point xb, yb is
reached.

P1 P2
(xa,ya) (xb,yb)
Steps for DDA Algorithm

Step 1: Accept as input the 2 end point pixel position.

P1(xa,ya) P2 (xb, yb)

Step 2: Horizontal and vertical differences between the


endpoint positions are computed & assigned to two
parameters namely dx and dy.

dx= xb – xa dy = yb - ya
Step 3: If abs(dx) >abs(dy)
steps =abs( dx)
else
steps =abs( dy)
Step 4: xincr = dx/steps
yincr =dy/steps
(ie dy/dx = m)

Assign x= xa and y=ya


x= x+ xincr & y= y+ yincr

Step 5: Loop through the process steps times.


Advantages of DDA
• It calculates the pixel positions faster than the calculations
performed by using the equation y=mx+ b
• Multiplication is eliminated as the x and y increments are used
to determine the position of the next pixel on a line.
Disadvantages of DDA
• The rounding and floating point operations are time consuming.
• The round off error which results in each successive addition
leads to the drift in pixel position which is already calculated.
Exercise:

Draw a line using DDA Algorithm between (10, 100) and (50, 20)
Bresenham’s Line Algorithm
Bresenham Line Algorithm
(For +ve slope and 0<m<1)
BRESENHAM’S LINE DRAWING ALGORITHM
(for |m| < 1.0)
1. Input the two line end-points, storing the left end-point in
(x0, y0)
2. Plot the point (x0, y0)
3. Calculate the constants dx, dy, 2dy, and (2dy – 2dx) and
get the first value for the decision parameter as:
p0  2dy  dx
4. At each xk along the line, starting at k = 0, perform the
following test. If pk < 0, the next point to plot is
(xk+1, yk) and:
pk 1  pk  2dy
Otherwise, the next point to plot is (xk+1, yk+1) and:
pk 1  pk  2dy  2dx
5. Repeat step 4 (dx – 1) times
Bresenham Line Algorithm
(For +ve slope and m>1)
BRESENHAM’S LINE DRAWING ALGORITHM
(for |m| > 1.0)
1. Input the two line end-points, storing the left end-point
in (x0, y0)
2. Plot the point (x0, y0)
3. Calculate the constants dx, dy, 2dx, and (2dx – 2dy)
and get the first value for the decision parameter as:
p0  2dx  dy
4. At each yk along the line, starting at k = 0, perform the
following test. If pk < 0, the next point to plot is
(xk, yk+1 ) and:
pk 1  pk  2dx
Otherwise, the next point to plot is (xk+1, yk+1) and:
pk 1  pk  2dx  2dy
5. Repeat step 4 (dy – 1) times
Exercise:

Draw a line using Bresenham’s Algorithm between (10, 20) and (50, 100)

Draw a line using Bresenham’s Algorithm between (10, 20) and (50, 40)

You might also like