0% found this document useful (0 votes)
3 views

Unit 1 Primitive Section[1]

The document discusses basic graphics primitives, focusing on scan conversion techniques for lines, circles, and ellipses. It explains the importance of pixels in creating graphics, outlines various algorithms for line drawing such as DDA and Bresenham’s Algorithm, and highlights their advantages and disadvantages. Additionally, it addresses the issue of aliasing in line drawing and introduces antialiasing methods to improve image quality.

Uploaded by

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

Unit 1 Primitive Section[1]

The document discusses basic graphics primitives, focusing on scan conversion techniques for lines, circles, and ellipses. It explains the importance of pixels in creating graphics, outlines various algorithms for line drawing such as DDA and Bresenham’s Algorithm, and highlights their advantages and disadvantages. Additionally, it addresses the issue of aliasing in line drawing and introduces antialiasing methods to improve image quality.

Uploaded by

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

UNIT I: Graphics

Primitives
Primitives: Basic Graphics
Primitives
I. Scan conversion a Line.

II. Scan conversion Circle.

III. Scan conversion Ellipse.

 Filled area Primitives.


I. Scan conversion a Line
 It is a process of representing graphics objects a collection of pixels.
 The graphics objects are continuous. The pixels used are discrete. Each
pixel can have either on or off state.
 The circuitry of the video display device of the computer is capable of
converting binary values (0, 1) into a pixel on and pixel off information. 0
is represented by pixel off. 1 is represented using pixel on. Using this
ability graphics computer represent picture having discrete dots.
 Any model of graphics can be reproduced with a dense matrix of dots or
points. Most human beings think graphics objects as points, lines, circles,
ellipses. For generating graphical object, many algorithms have been
developed.
Advantages for scan
conversion a line
 Algorithms can generate graphics objects at a faster
rate.

 Using algorithms memory can be used efficiently.

 Algorithms can develop a higher level of graphical


objects.
Pixel or Pel

 The term pixel is a short form of the picture element. It is


also called a point or dot. It is the smallest picture unit
accepted by display devices.
 A picture is constructed from hundreds of such pixels.
Pixels are generated using commands. Lines, circle, arcs,
characters; curves are drawn with closely spaced pixels. To
display the digit or letter matrix of pixels is used.
 The closer the dots or pixels are, the better will be the
quality of picture. Closer the dots are, crisper will be the
picture.
 Picture will not appear jagged and unclear if pixels are
closely spaced. So the quality of the picture is directly
proportional to the density of pixels on the screen.
Pixel or Pel…
Pixel or Pel…
 Different graphics objects can be generated by setting the different
intensity of pixels and different colors of pixels. Each pixel has some co-
ordinate value. The coordinate is represented using row and column.
 P (5, 5) used to represent a pixel in the 5th row and the 5th column. Each
pixel has some intensity value which is represented in memory of
computer called a frame buffer.
 Frame Buffer is also called a refresh buffer. This memory is a storage area
for storing pixels values using which pictures are displayed. It is also
called as digital memory.
 Inside the buffer, image is stored as a pattern of binary digits either 0 or
1. So there is an array of 0 or 1 used to represent the picture. In black and
white monitors, black pixels are represented using 1's and white pixels are
represented using 0's. In case of systems having one bit per pixel frame
buffer is called a bitmap. In systems with multiple bits per pixel it is
called a pixmap.
Scan converting a Point

 Each pixel on the graphics display does not represent a


mathematical point.
 Instead, it means a region which theoretically can
contain an infinite number of points.
 Scan-Converting a point involves illuminating the pixel
that contains the point.
Scan converting a Point…
Scan converting a Straight
Line
 A straight-line segment in a scene is defined by the
coordinate positions for the endpoints of the segment.
 To display the line on a raster monitor, the graphics
system must first project the endpoints to integer
screen coordinates and determine the nearest pixel
positions along the line path between the two
endpoints.
 Then the line color is loaded into the frame buffer at
the corresponding pixel coordinates.
Scan converting a Straight
Line…
 Reading from the frame buffer, the video controller
plots the screen pixels.
 This process digitizes the line into a set of discrete
integer positions that, in general, only approximates the
actual line path.
Line Equations

 We determine pixel positions along a straight-line path


from the geometric properties of the line.
 The Cartesian slope-intercept equation for a straight-
line is: y=m.x + b
Properties of Good Line
Drawing Algo.:
 Line should appear Straight.

 Lines should terminate accurately.

 Lines should have constant density.

 Line density should be independent of line length and


angle.

 Line should be drawn rapidly.


Algorithms for Line drawing

 Direct use of line equation.

 Digital Differential Analyzer (DDA).

 Bresenham’s Algorithm.
Basic concept of Line drawing
Direct use of Line Equation

 It is the simplest form of conversion. First of all scan


P1 and P2 points. P1 has co-ordinates (x1',y1') and (x2' y2'
).
 Then m = (y2',y1')/( x2',x1') and
 If value of |m|≤1 for each integer value of x. But do not
consider
 If value of |m|>1 for each integer value of y. But do not
consider
Algorithm for drawing line
using

equation
Step1: Start Algorithm
 Step2: Declare variables x1,x2,y1,y2,dx,dy,m,b,
 Step3: Enter values of x1,x2,y1,y2.
The (x1,y1) are co-ordinates of a starting point of the line.
The (x2,y2) are co-ordinates of a ending point of the line.
 Step4: Calculate dx = x2- x1
 Step5: Calculate dy = y2-y1

 Step6: Calculate m =

 Step7: Calculate b = y1-m* x1


 Step8: Set (x, y) equal to starting point, i.e., lowest point and xendequal
to largest value of x.
 If dx < 0
then x = x2
y = y2
xend= x1
If dx > 0
then x = x1
y = y1
xend= x2
 Step9: Check whether the complete line has been drawn if x=xend, stop
 Step10: Plot a point at current (x, y) coordinates
 Step11: Increment value of x, i.e., x = x+1
 Step12: Compute next value of y from equation y = mx + b
 Step13: Go to Step9.
 Example: A line with starting point as (0, 0) and ending point (6, 18) is
given. Calculate value of intermediate points and slope of line.
 Solution: P1 (0,0) P7 (6,18)
 x1=0
y1=0
x2=6
y2=18

 We know equation of line is


y =m x + b
y = 3x + b..............equation (1)
 put value of x from initial point in equation (1), i.e., (0, 0) x =0, y=0
0=3x0+b
0 = b ⟹ b=0
 put b = 0 in equation (1)
 y = 3x + 0
y = 3x
 Now calculate intermediate points
Let x = 1 ⟹ y = 3 x 1 ⟹ y = 3
Let x = 2 ⟹ y = 3 x 2 ⟹ y = 6
Let x = 3 ⟹ y = 3 x 3 ⟹ y = 9
Let x = 4 ⟹ y = 3 x 4 ⟹ y = 12
Let x = 5 ⟹ y = 3 x 5 ⟹ y = 15
Let x = 6 ⟹ y = 3 x 6 ⟹ y = 18
 So points are P1 (0,0)
P2 (1,3)
P3 (2,6)
P4 (3,9)
P5 (4,12)
P6 (5,15)
P7 (6,18)
Direct Differential Analyzer
(DDA)
 DDA stands for Digital Differential Analyzer.
 It is an incremental method of scan conversion of line.
In this method calculation is performed at each step but
by using results of previous steps.
 The DDA is a scan-conversion line algorithm based on
calculating either ᵟy and ᵟx, using line equations.
DDA Algorithm
Advantages of DDA

 Advantages:
 It is a faster method than method of using direct use of
line equation.
 This method does not use multiplication theorem.
 It allows us to detect the change in the value of x and y, so
plotting of same point twice is not possible.
 This method gives overflow indication when a point is
repositioned.
 It is an easy method because each step involves just two
additions.
Disadvantages of DDA

 Disadvantages:
 It involves floating point additions rounding off is done.
Accumulations of round off error cause accumulation of
error.
 Rounding off operations and floating point operations
consumes a lot of time.
 It is more suitable for generating line using the software.
But it is less suited for hardware implementation.
 Example: If a line is drawn from (2, 3) to (6, 15) with use of DDA. How
many points will needed to generate such line?
 Solution: P1 (2,3) P11 (6,15)
 x1=2
y1=3
x2= 6
y2=15
dx = 6 - 2 = 4
dy = 15 - 3 = 12
m=
 For calculating next value of x takes x = x + 1/m

Problems of DDA

 Consider the line from (0,0) to (4,6). Use the simple


DDA algorithm to rasterize this line segment.

 Consider the line from (0,0) to (-6,-6). Use the simple


DDA algorithm to rasterize this line segment.
Bresenham’s Line Drawing
Algorithm
 This algorithm is used for scan converting a line.
 It was developed by Bresenham.
 It is an efficient method because it involves only integer
addition, subtractions, and multiplication operations.
These operations can be performed very rapidly so lines
can be generated quickly.
 In this method, next pixel selected is that one who has
the least distance from true line.
Bresenham’s Algorithm
Advantages of Bresenham’s
Algorithm
 Advantages:
 It involves only integer arithmetic, so it is simple.
 It avoids the generation of duplicate points.
 It can be implemented using hardware because it does not
use multiplication and division.
 It is faster as compared to DDA (Digital Differential
Analyzer) because it does not involve floating point
calculations like DDA Algorithm.
Disadvantages of
Bresenham’s Algorithm
 Disadvantages:
 This algorithm is meant for basic line drawing only
Initializing is not a part of Bresenham's line algorithm. So
to draw smooth lines, you should want to look into a
different algorithm.
 Example: Starting and Ending position of the line are
(1, 1) and (8, 5). Find intermediate points.
 Solution: x1=1
y1=1
x2=8
y2=5
dx= x2-x1=8-1=7
dy=y2-y1=5-1=4
I1=2* ∆y=2*4=8
I2=2*(∆y-∆x)=2*(4-7)=-6
d = I1-∆x=8-7=1
x y d=d+I1 or I2

1 1 d+I2=1+(-6)=-5
2 2 d+I1=-5+8=3
3 2 d+I2=3+(-6)=-3
4 3 d+I1=-3+8=5
5 3 d+I2=5+(-6)=-1
6 4 d+I1=-1+8=7
7 4 d+I2=7+(-6)=1
8 5
Difference Between DDA and
Bresenham’s Line Algorithms
DDA Algorithm Bresenham's Line Algorithm

1. DDA Algorithm use floating point, 1. Bresenham's Line Algorithm use


i.e., Real Arithmetic. fixed point, i.e., Integer Arithmetic

2. DDA Algorithms uses multiplication & 2.Bresenham's Line Algorithm uses only
division its operation subtraction and addition its operation

3. DDA Algorithm is slowly than 3. Bresenham's Algorithm is faster than


Bresenham's Line Algorithm in line DDA Algorithm in line because it
drawing because it uses real arithmetic involves only addition & subtraction in
(Floating Point operation) its calculation and uses only integer
arithmetic.

4. DDA Algorithm is not accurate and 4. Bresenham's Line Algorithm is more


efficient as Bresenham's Line Algorithm. accurate and efficient at DDA
Algorithm.

5.DDA Algorithm can draw circle and 5. Bresenham's Line Algorithm can
curves but are not accurate as draw circle and curves with more
Bresenham's Line Algorithm accurate than DDA Algorithm.
Antialiasing of Lines
 In the line drawing algorithm, we have seen that all rasterized locations
do not match with the true line and we have to select the optimum
raster locations to represent a straight line.

 This problem is severe in low resolution screens. In such screens line


appears like a stair-step.

 The aliasing effect is the appearance of jagged edges or “jaggies” in a


rasterized image (an image rendered using pixels).
 The problem of jagged edges technically occurs due to distortion of the
image when scan conversion is done with sampling at a low frequency,
which is also known as Undersampling.
 Aliasing occurs when real-world objects which comprise of smooth,
continuous curves are rasterized using pixels.
 Antialiasing is a technique used in computer graphics to remove the
aliasing effect.
 The aliasing effect can be reduced by adjusting intensities of the pixels
along the line.
 The process of adjusting intensities of the pixels along the line to
minimize the effect of aliasing is called antialiasing.
Methods of Antialiasing:

 Increasing resolution:
 Unweighted area sampling: The intensity of pixel is proportional to the
amount of line area occupied by the pixel. This technique produces
noticeably better results than does setting pixels either to full intensity or
to zero intensity.
 Weighted area sampling: in weighted area sampling equal areas
contribute unequally i.e. a small area closer to the pixel center has
greater intensity than does one at a greater distance. Thus, the intensity
of the pixel is dependent on the line area occupied and the distance of
area from the pixel’s center.
Displaying Polylines
 Implementation of a polyline procedure is accomplished by invoking a
line-drawing routine n-1 times to display the line connecting the n
endpoints.
 Each successive call passes the coordinate pair needed to plot the next
line section, where the first endpoint of each coordinate pair is the last
endpoint of the previous section.
 Once the color values for pixel positions along the first line segment have
been set in the frame buffer, we process subsequent line segments
starting with the next pixel position following the first endpoint for that
segment.
 In this way, we can avoid setting the color of some endpoints twice.
Polyline representation
Parallel Line Algorithms

 Using parallel processing, we can calculate multiple


pixel positions along a line path simultaneously by
partitioning the computations among the various
processors available.
 One approach to the partitioning problem is to adapt an
existing sequential algorithm to take advantage of
multiple processors.
 Alternatively, to set up the processing so that pixel
positions can be calculated efficiently in parallel.
 An important consideration in devising a parallel
algorithm is to balance the processing load among the
available processors.
 Given np processors, we can set up a parallel Bresenham line algorithm by
subdividing the line path into np partitions and simultaneously generating
line segments in each of the subintervals.
 For a line with slope 0 < m < 1 and left endpoint coordinate position (x0,
y0), we partition the line along the positive x direction.
 The distance between beginning x positions of adjacent partitions can be
calculated as:
 Numbering the partitions, and the processors, as 0,1,2, up to np-1, we
calculate the starting x coordinate for the kth partition as:
Scan Conversion Circle

 A circle is defined as the set of points that are all at a


given distance r from a center position.
 Circle is an eight-way symmetric figure.
 The shape of circle is the same in all quadrants. In each
quadrant, there are two octants.
 If the calculation of the point of one octant is done,
then the other seven points can be calculated easily by
using the concept of eight-way symmetry.
Defining circle using
Polynomial method
 The first method defines a circle with the second-order
polynomial equation as shown in fig:
 y2=r2-x2
Where x = the x coordinate
y = the y coordinate
r = the circle radius
 With the method, each x coordinate in the sector, from
90° to 45°, is found by stepping x from 0 to r/√2 &
each y coordinate is found by evaluating √ r2-x2 for each
step of x.
Defining circle using Polar
Coordinates
 The second method of defining a circle makes use of
polar coordinates as shown in fig:
 x=r cos θ y = r sin θ
Where θ=current angle
r = circle radius
x = x coordinate
y = y coordinate
 By this method, θ is stepped from 0 to π/4 & each
value of x & y is calculated.
DDA Circle Drawing Algorithm

 The equation of circle, with origin as the center of the


circle is given as:
 The DDA algorithm can be used to draw the circle by
defining circle as a differential equation.
DDA Circle Drawing
Algorithm…
Bresenham’s Circle Algorithm

 The Bresenham’s circle drawing algorithm considers the


eight-way symmetry of the circle to generate it.
 It plots 1/8th part of the circle, i.e. from 90 degree to
45 degree.
 As circle is drawn from 90 degree to 45 degree, the x
moves in positive direction and y moves in the negative
direction.
 To achieve best approximation to the true circle we have to select those
pixels in the raster that fall the least distance from the true circle.
 Let us observe the 90 degree to 45 degree portion of the circle.
 It can be noticed that if points are generated from 90 degree to 45
degree, each point closest to the true circle can be found by applying
either of the two options:
 Increment in positive x direction by one unit or
 Increment in positive x direction and negative y direction both by one unit.
Bresenham’s algo. to plot
1/8th of the circle
Midpoint circle drawing
algorithm
 The midpoint circle drawing algorithm also uses the
eight-way symmetry of the circle to generate it.
 As in the raster line algorithm, we sample at unit
intervals and determine the closest pixel position to the
specified circle path at each step.
 For a given radius r and screen center position (xc, yc),
we can first set up our algorithm to calculate pixel
positions around a circle path centered at the
coordinate origin (0, 0).
 Then each calculated position (x, y) is moved to its
proper screen position by adding xc to x and yc to y.
Midpoint circle drawing
algorithm…
Scan conversion of Ellipse
 An ellipse is an elongated circle.

 Therefore, elliptical curves can be generated by modifying circle-drawing


procedures to make into account the different dimensions of an ellipse
along the major and minor axes.

 The ellipse is also a symmetric figure like a circle but is four-way


symmetry rather than eight-way.

 Two methods of defining an Ellipse:


 Polynomial method
 Trigonometric method
Polynomial method for Ellipse

 The ellipse has a major and minor axis. If a1 and b1 are


major and minor axis respectively. The centre of ellipse
is (i, j). The value of x will be incremented from i to a1
and value of y will be calculated using the following
formula
Drawback of Polynomial method:
 It requires squaring of values. So floating point
calculation is required.
 Routines developed for such calculations are very
complex and slow.
Trigonometric method for
Ellipse
 The following equation defines an ellipse
trigonometrically as shown in fig:
x = a * cos (θ) +h and
y = b * sin (θ)+k
where (x, y) = the current coordinates
a = length of major axis
b = length of minor axis
θ= current angle
(h, k) = ellipse center

 In this method, the value of θ is varied from 0


to π/2 radians. The remaining points are found by
symmetry.
Drawback of Trigonometric method:
 This is an inefficient method.
 It is not an interactive method for generating ellipse.
 The table is required to see the trigonometric value.
 Memory is required to store the value of θ.
Properties of Ellipses

 An ellipse is defined as the set of points such that the


sum of the distances from two fixed positions (foci) is
the same for all points.
 If the two foci from any point P = (x, y) on the ellipse
are labeled d1 and d2, then the general equation of an
ellipse can be stated as:
 By squaring the last equation, isolating the
remaining radical, and squaring again, we
can rewrite the general ellipse equation in
the form as:
 Ellipse equations are greatly simplified if the
major and minor axes are oriented to align with
the coordinate axes.
 The equation for the ellipse shown in the figure
can be written in terms of the ellipse center
coordinates and parameter rx and ry as:
Midpoint Ellipse Algorithm

 This is an incremental method for scan converting an


ellipse that is centered at the origin in standard position
i.e., with the major and minor axis parallel to
coordinate system axis.
 It is very similar to the midpoint circle algorithm.
Because of the four-way symmetry property we need to
consider the entire elliptical curve in the first quadrant.
 The midpoint ellipse method is applied
throughout the first quadrant in two parts.
 Regions 1 and 2 can be processed in various
ways. We can start at position (0, ry) and step
clockwise along the elliptical path in the first
quadrant, shifting from unit steps in x to unit
steps in y when slop< -1.0.
 At each step we need to test the value of the
slope of the curve.
 The ellipse slope is calculated from equation as:
 2
To simplify the calculation of p 0, we could select pixel positions in
counterclock-wise order starting at (rx, 0).

 Unit steps would then be taken in the positive y direction up to the last
position selected in region 1.
MidPoint Ellipse drawing
algorithm…
Lab4 Assignment for the Lab: write
an OpenGL program for displaying
the following diagram:
Scan-conversion of conic
section
Area Filling

 Rasterizing a Polygon.
 A polyline is a chain of connected line segments. It is
specified by giving the vertices or nodes P0,P1,P2,… and
so on.
 The first vertex is called the initial or starting point and
the last vertex is called the final or terminal point.
 When starting point and terminal point of any polyline is
same, i.e. when polyline is closed then it is called
polygon.
Scan-converting a Polygon

 Generally closed contours are represented by a cluster


of polygons.
 Thus, to fill or to draw a contour it is necessary to have
methods for filling polygons.
 It may be observed that adjacent pixels are likely to
have the same characteristics in polygon. This property
is called spatial coherence.
 Adjacent pixels on a scan line are likely to have the
same characteristics. This is called scan line coherence.
Types of Polygon

 Convex polygon: it is a polygon in which the line


segment joining any two points within the polygon lies
completely inside the polygon.

 Concave polygon: A concave polygon is a polygon in


which the line segment joining any two points within
the polygon may not lie completely inside the polygon.
Filled-Area Primitives:
Polygon filling
 Region filling is the process of filling image or region.
Filling can be of boundary or interior region.
 Filling the polygon means highlighting all the pixels
which lie inside the polygon with any color other than
background color.
 Boundary Fill algorithms are used to fill the boundary
and flood-fill algorithm are used to fill the interior.
 Seed fill methods
Seed Fill

 The seed fill algorithm is further classified as flood fill


algorithm and boundary fill algorithm.
 An algorithms that fill interior-defined regions are
called flood-fill algorithm.
 An algorithms that fill boundary-defined regions are
called boundary fill algorithm or edge-fill algorithms.
 Boundary fill
 Flood fill
1. Boundary Filled Algorithm

 This algorithm uses the recursive method. First of all, a


starting pixel called as the seed is considered.
 The algorithm checks boundary pixel or adjacent pixels
are colored or not.
 If the adjacent pixel is already filled or colored then
leave it, otherwise fill it.
 The filling is done using four connected or eight
connected approaches.
 Four connected approaches is more suitable than the
eight connected approaches.
 Four connected approaches: In this approach, left,
right, above, below pixels are tested.
 Eight connected approaches: In this approach, left,
right, above, below and four diagonals are selected.
 Boundary can be checked by seeing pixels from left and
right first. Then pixels are checked by seeing pixels
from top to bottom.
 The algorithm takes time and memory because some
recursive calls are needed.
Boundary fill algorithm…
2. Flood Fill Algorithm

 In this method, a point or seed which is inside region is


selected. This point is called a seed point.
 Then four connected approaches or eight connected
approaches is used to fill with specified color.
 The flood fill algorithm has many characters similar to
boundary fill. But this method is more suitable for filling
multiple colors boundary.
 When boundary is of many colors and interior is to be
filled with one color we use this algorithm.
 In fill algorithm, we start from a specified interior point (x, y) and
reassign all pixel values are currently set to a given interior color with the
desired color.
 Using either a 4-connected or 8-connected approaches, we then step
through pixel positions until all interior points have been repainted.

 Disadvantage:
 Very slow algorithm
 May be fail for large polygons
 Initial pixel required more knowledge about surrounding pixels.
Flood fill algorithm…
Scan Line Polygon Fill
Algorithm
 This algorithm lines interior points of a polygon on the
scan line and these points are done on or off according
to requirement.
 The polygon is filled with various colors by coloring
various pixels.
 First of all, scanning is done. Scanning is done using
raster scanning concept on display device.
 The beam starts scanning from the top left corner of
the screen and goes toward the bottom right corner as
the endpoint.
 The algorithms find points of
intersection of the line with polygon
while moving from left to right and
top to bottom.
 The various points of intersection are
stored in the frame buffer.
 The intensities of such points is keep
high.
 Concept of coherence property is
used. According to this property if a
pixel is inside the polygon, then its
next pixel will be inside the polygon.
 Side effects of Scan Conversion:
 Staircase or Jagged: Staircase like appearance is seen
while the scan was converting line or circle.

 Unequal Intensity: It deals with unequal appearance of


the brightness of different lines. An inclined line appears
less bright as compared to the horizontal and vertical line.
Scan line algorithm for polygon
filling

You might also like