CGMUNIT2 - Graphics Primitives

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 22

Graphics Primitives: Introduction

A primitive is a low level object or operation from which higher-level,


more complex objects and operations can be constructed. In graphics,
primitives are basic elements, such as lines, curves, and polygons,
which can be combined to create more complex graphical images. In
programming, primitives are the basic operations supported by the
programming language. To creative any drawing in the computer these
primitives form a part of the software and the type of display to store
these in the form of data is important.

Graphics Primitives

Graphics Primitive is a basic object that is essential for the creation or


construction of complex images. Graphics is constructed from three
basic elements, as opposed to the great variety of graphics
applications. The most basic of these elmental structures is the pixel,
short for picture element.

10.2.1.Pixel

A pixel is a point of light. It is just one tiny dot on the raster displays.
Though it has no structure, it is definitely a building block and hence it
can be considered as the graphics primitive. The resolution of CRT is
related to the dot size, the diameter of a single dot. A resolution of 100
dots lines/inch implies a dot size of 0.01 inch. The ratio of the distance
between the centres of two adjacent horizontal pixels to that of the
vertical ones is called the pixel ratio. Pixel ratio should be considered in
line-generating algorithms.

10.2.2.Line

Line, especially straight lines, constitute the basic building block of Line
graphs, bar and pie charts, two and three-dimensional graphs of
mathematical functions, engineering drawings and architectural plans.
In computer graphics, straight line is so basic in creating images that we
call it a graphics primitive. Straight lines can be developed in two
different ways. A structural method determines which pixels should be
set before drawing the line; a conditional method tests certain
conditions to find which pixel should be set next.

10.2.3.Polygon
A polygon is a closed area of image bounded by straight or curved lines
and filled with one solid color. Since images are two dimensional, a
polygon is a closed planar figure.A polygon, is an important graphics
primitive. So often we want to handle polygon as a single entity, as
images of objects from the real world consist in large, part of polygons.

10.3. Display file

A display file is a set of uncorrelated data, such as a histogram array or


bivariate array. The arrays are filled event by event from a list data in
order to create a display. The saved arrays usually take up far less disk
space, but can the data can no longer be gated.

10.4.Frame buffer

The video output device which drives a video display from the memory
buffer containing a complete set of data is known as frame buffer. The
image is stored in terms of pixel by pixel. The memory can be discs,
Integrated circuits etc.,

10.5.Display control

This controls the view of the image so that the user can view the mage
fromdesired angle and desired magnification.

10.6.Display Processor

The display processor read the data from the frame buffer and convert it
into corresponding 1's and 0's according to there pixels and then put it
on to a monitor screen.The display processor do this work 30 times per
second to maintain a steady picture on the screen,and if we want to
change the picture on the screen then we have to change the contents
of frame buffer.

10.7.Line generation.

In mathematics and computer science an algorithm is a step by step


procedure for making calculations. Algorithms are made for calculation,
data processing and automated reasoning. In order to draw lines on a
computer screen, the Bresenham line algorithm is used that determines
which order to form a close approximation to a straight line between two
given points. It uses only integer addition, subtraction and bit shifting
where the digits are moved or shifted left or right, all of which are very
cheap operations in standard computer architectures. It is one of the
earliest algorithms developed in the field of computer graphics. A minor
extension to the original algorithm also deals with drawing circles.

Another algorithm namely, Digital Differential Analyzer is a scan


conversion line algorithm based on calculating either dy or dx. We
sample the line at unit intervals in one coordinate & determine
corresponding integer values nearest to the line path for the other
coordinate.
The algorithm accepts as input the two endpoint pixel positions.
Horizontal & vertical differences between the endpoint positions are
assigned to parameters dx & dy. The difference with the greater
magnitude determines the increment of the parameter steps. Starting
with the pixel position (xa , ya), we determine the offset needed at each
step to generate the next pixel position along the line path.

10.8.Graphics software

Graphics software is a program or set of programs that enables us to


manipulate the visual images on computer system. There two types of
graphics namely, Raster graphics and Vector graphics. The Raster
graphics or bitmap, is a dot matrix data structure representing a
generally rectangular grid of pixels, or points of color, Vector Graphics is
the use of geometrical primitives such as points, lines, curves,
and shapes or polygon(s), which are all based on mathematical
expressions, to represent images in computer graphics. It is easy to
convert from vector graphics to raster graphics, but going the other way
is harder. Some software attempts to do this.In addition to static
graphics, there are animation and video editing software. Computer
graphics also can be used by other editing software such as Adobe
Photoshop, Pizap, Microsoft Publisher, Picasa and etc. Other software
that can be used is animation software, video editor software such as
Windows Movie Maker etc.

10.9. Points and lines

Points are used throughout graphics as building blocks for more


complicated shapes (e.g. triangles created with three points). Another
fundamental geometric object in 2D graphics is the line. A line is defined
as containing all 2D points (x,y) which satisfy teh equation ax+by+d =0

10.10. Polygons

Polygons are used in computer graphics to compose images that


are three-dimensional in appearance. Usually triangular, polygons arise
when an object's surface is modeled, vertices are selected, and the
object is rendered in a wire frame model. This is quicker to display than
a shaded model; thus the polygons are a stage in computer animation.
The polygon count refers to the number of polygons being rendered
per frame.

10.11Filling of polygons

The polygon is filled to considered the entire area when it is rendered. If


it is not filled, only the points on the perimeter of the polygon will be
drawn. When a polygon is filled, the interior of the polygon is
considered. All of the pixels within the boundaries of the polygon is set
to the specified color or pattern. In order to determine which pixels are
inside the polygon, the odd-parity rule determining which pixel lies
within the polygon and which lies outside, is used within the scan-line
polygon fill algorithm.

10.12.Text primitive

With the Text graphics primitive, we can insert text at any position in
two- or three-dimensional Mathematical graphics. The text will be given
in the graphic's base style.

10.13.Windowing and clipping

In computer graphics any object that is larger than the computer screen
cannot be seen through the monitor i.e., window. So we have to remove
the unseen portions of the image or block out those portions. This
process is known as clipping and making the object to be seen through
the window by using algorithms is known as windowing.

10.14.View port

A viewport is a rectangular viewing region in computer graphics, or a


term used for optical components.

Scan Conversion Definition


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.

Advantage of developing algorithms for scan conversion

1. Algorithms can generate graphics objects at a faster rate.


2. Using algorithms memory can be used efficiently.
3. Algorithms can develop a higher level of graphical objects.

Examples of objects which can be scan converted

1. Point
2. Line
3. Sector
4. Arc
5. Ellipse
6. Rectangle
7. Polygon
8. Characters
9. Filled Regions

The process of converting is also called as rasterization. The algorithms


implementation varies from one computer system to another computer
system. Some algorithms are implemented using the software. Some
are performed using hardware or firmware. Some are performed using
various combinations of hardware, firmware, and software.

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.

Pixels are also defined as the smallest addressable unit or element of


the screen. Each pixel can be assigned an address as shown in fig:

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.

Example: Display coordinates points as shown in


fig would both be represented by pixel (2, 1). In general, a point p (x, y)
is represented by the integer part of x & the integer part of y that is
pixels [(INT (x), INT (y).

Scan Converting a Straight Line

A straight line may be defined by two endpoints & an equation. In fig the
two endpoints are described by (x1,y1) and (x2,y2). The equation of the
line is used to determine the x, y coordinates of all the points that lie
between these two endpoints.
Using the equation of a straight line, y = mx + b where m = & b = the
y interrupt, we can find values of y by incrementing x from x =x 1, to x =
x2. By scan-converting these calculated x, y values, we represent the
line as a sequence of pixels.

Properties of Good Line Drawing Algorithm:

1. Line should appear Straight: We must appropriate the line by


choosing addressable points close to it. If we choose well, the line will
appear straight, if not, we shall produce crossed lines.

The lines must be generated parallel or at 45° to the x and y-axes.


Other lines cause a problem: a line segment through it starts and
finishes at addressable points, may happen to pass through no another
addressable points in between.
2. Lines should terminate accurately: Unless lines are plotted
accurately, they may terminate at the wrong place.

3. Lines should have constant density: Line density is proportional to


the no. of dots displayed divided by the length of the line.

To maintain constant density, dots should be equally spaced.

4. Line density should be independent of line length and


angle: This can be done by computing an approximating line-length
estimate and to use a line-generation algorithm that keeps line density
constant to within the accuracy of this estimate.
5. Line should be drawn rapidly: This computation should be
performed by special-purpose hardware.

Algorithm for line Drawing:

1. Direct use of line equation


2. DDA (Digital Differential Analyzer)
3. Bresenham's Algorithm

Direct use of line equation:

It is the simplest form of conversion. First of all scan P 1 and P2 points.


P1 has co-ordinates (x1',y1') and (x2' y2' ).

Then m = (y2',y1')/( x2',x1') and b =

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

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)

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 x endequal
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.

OUTPUT:

Enter Starting and End Points


Enter (X1, Y1, X2, Y2) 200 100 300 200
DDA Algorithm

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.

Suppose at step i, the pixels is (xi,yi)

The line of equation for step i


yi=mxi+b......................equation 1

Next value will be


yi+1=mxi+1+b.................equation 2

m=
yi+1-yi=∆y.......................equation 3
yi+1-xi=∆x......................equation 4
yi+1=yi+∆y
∆y=m∆x
yi+1=yi+m∆x
∆x=∆y/m
xi+1=xi+∆x
xi+1=xi+∆y/m

Case1: When |M|<1 then (assume that x1<x2)


x= x1,y=y1 set ∆x=1
yi+1=y1+m, x=x+1
Until x = x2

Case2: When |M|<1 then (assume that y1<y2)


x= x1,y=y1 set ∆y=1
xi+1= , y=y+1
Until y → y2

Advantage:

1. It is a faster method than method of using direct use of line equation.


2. This method does not use multiplication theorem.
3. It allows us to detect the change in the value of x and y ,so plotting of same point twice is
not possible.
4. This method gives overflow indication when a point is repositioned.
5. It is an easy method because each step involves just two additions.

Disadvantage:

1. It involves floating point additions rounding off is done. Accumulations of round off error
cause accumulation of error.
2. Rounding off operations and floating point operations consumes a lot of time.
3. It is more suitable for generating line using the software. But it is less suited for hardware
implementation.

DDA Algorithm:

Step1: Start Algorithm

Step2: Declare x1,y1,x2,y2,dx,dy,x,y as integer variables.

Step3: Enter value of x1,y1,x2,y2.

Step4: Calculate dx = x2-x1

Step5: Calculate dy = y2-y1

Step6: If ABS (dx) > ABS (dy)


Then step = abs (dx)
Else
Step7: xinc=dx/step
yinc=dy/step
assign x = x1
assign y = y1

Step8: Set pixel (x, y)

Step9: x = x + xinc
y = y + yinc
Set pixels (Round (x), Round (y))

Step10: Repeat step 9 until x = x2

Step11: End Algorithm

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 +


Bresenham's Line 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.
The method works as follows:
Assume a pixel P1'(x1',y1'),then select subsequent pixels as we work
our may to the night, one pixel position at a time in the horizontal
direction toward P2'(x2',y2').
Once a pixel in choose at any step
The next pixel is
Either the one to its right (lower-bound for the line)
One top its right and up (upper-bound for the line)
The line is best approximated by those pixels that fall the least distance
from the path between P1',P2'.

To chooses the next one between the bottom pixel S and top pixel T.
If S is chosen
We have xi+1=xi+1 and yi+1=yi
If T is chosen
We have xi+1=xi+1 and yi+1=yi+1
The actual y coordinates of the line at x = xi+1is
y=mxi+1+b

The distance from S to the actual line in y direction


s = y-yi
The distance from T to the actual line in y direction
t = (yi+1)-y
Now consider the difference between these 2 distance values
s-t
When (s-t) <0 ⟹ s < t
The closest pixel is S
When (s-t) ≥0 ⟹ s < t
The closest pixel is T
This difference is
s-t = (y-yi)-[(yi+1)-y]
= 2y - 2yi -1

Substituting m by and introducing decision variable


di=△x (s-t)

di=△x (2 (xi+1)+2b-2yi-1)
=2△xyi-2△y-1△x.2b-2yi△x-△x
di=2△y.xi-2△x.yi+c
Where c= 2△y+△x (2b-1)
We can write the decision variable di+1 for the next slip on
di+1=2△y.xi+1-2△x.yi+1+c
di+1-di=2△y.(xi+1-xi)- 2△x(yi+1-yi)
Since x_(i+1)=xi+1,we have
di+1+di=2△y.(xi+1-xi)- 2△x(yi+1-yi)
Special Cases
If chosen pixel is at the top pixel T (i.e., di≥0)⟹ yi+1=yi+1
di+1=di+2△y-2△x
If chosen pixel is at the bottom pixel T (i.e., di<0)⟹ yi+1=yi
di+1=di+2△y
Finally, we calculate d1
d1=△x[2m(x1+1)+2b-2y1-1]
d1=△x[2(mx1+b-y1)+2m-1]
Since mx1+b-yi=0 and m = , we have
d1=2△y-△x
Advantage:
1. It involves only integer arithmetic, so it is simple.
2. It avoids the generation of duplicate points.
3. It can be implemented using hardware because it does not use
multiplication and division.
4. It is faster as compared to DDA (Digital Differential Analyzer)
because it does not involve floating point calculations like DDA
Algorithm.
Disadvantage:
1. 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.
Bresenham's Line Algorithm:
Step1: Start Algorithm
Step2: Declare variable x1,x2,y1,y2,d,i1,i2,dx,dy
Step3: Enter value of x1,y1,x2,y2
Where x1,y1are coordinates of starting point
And x2,y2 are coordinates of Ending point
Step4: Calculate dx = x2-x1
Calculate dy = y2-y1
Calculate i1=2*dy
Calculate i2=2*(dy-dx)
Calculate d=i1-dx
Step5: Consider (x, y) as starting point and xendas maximum possible
value of x.
If dx < 0
Then x = x2
y = y2
xend=x1
If dx > 0
Then x = x1
y = y1
xend=x2
Step6: Generate point at (x,y)coordinates.
Step7: Check if whole line is generated.
If x > = xend
Stop.
Step8: Calculate co-ordinates of the next pixel
If d < 0
Then d = d + i1
If d ≥ 0
Then d = d + i2
Increment y = y + 1
Step9: Increment x = x + 1
Step10: Draw a point of latest (x, y) coordinates
Step11: Go to step 7
Step12: End of 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
Differentiate between DDA Algorithm and Bresenham's Line Algorithm:
DDA Algorithm Bresenham's Line
Algorithm

1. DDA Algorithm 1. Bresenham's Line


use floating point, Algorithm use fixed
i.e., Real Arithmetic. point, i.e., Integer
Arithmetic

2. DDA Algorithms 2.Bresenham's Line


uses multiplication & Algorithm uses only
division its operation subtraction and addition
its operation

3. DDA Algorithm is 3. Bresenham's


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

4. DDA Algorithm is 4. Bresenham's Line


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

5.DDA Algorithm can 5. Bresenham's Line


draw circle and Algorithm can draw
curves but are not circle and curves with
accurate as more accurate than
Bresenham's Line DDA Algorithm.
Algorithm

You might also like