CGMT PPT Unit-2
CGMT PPT Unit-2
CGMT PPT Unit-2
By:
Er. Naveen Kumar Tiwari
Arya Group Of Colleges
UNIT-2 CONTENTS
2.1 Graphics Primitives
2.1.1 Point And Lines Primitives
2.1.2 Circles And Ellipses Primitives
2.2 Scan Conversion Algorithm For Primitives
2.2.1 DDA Algorithm
2.2.2 Bressman Algorithm
2.2.3 Mid-Point Circle Algorithm
2.2.4 Flood Fill And Boundary Fill Algorithm
2.3 Character Generation Algorithm
2.3.1 Line-Attributes
2.3.2 Area-Fill Attributes
2.3.3 Character Attributes
2.4 Aliasing
2.5 Anti-Aliasing
Back
DEFINITION:
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 you can combine to create more complex graphical images. In
programming, primitives are the basic operations supported by the programming
language.
Which of the following graphic primitives are considered as the basic building
blocks of computer graphics ?
Points(Co-ordinate) and lines are the basic building blocks of computer graphics.
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).
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.
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.
Lines should terminate accurately: Unless lines
are plotted accurately, they may terminate at
the wrong place.
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.
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.
Line should be drawn rapidly: This computation
should be performed by special-purpose
hardware.
Direct use of line equation
DDA (Digital Differential Analyzer)
Bresenham's Algorithm
It is the simplest form of conversion. First of all
scan P1 and P2 points. P1 has co-ordinates (x1',y1')
and (x2' y2' ).
Y= mx+b
SIMPLE LINE EQUATION
◦ x = xc + r cos
◦ y = yc + r sin
When a circle is generated with these
equations using a fixed angular step size, a
circle is plotted with equally spaced points
along the circumference.
Otherwise,
Solution:
p0 =1 – r = – 9
Plot the initial point (x0, y0 ) = (0, 10),
0 –9 (1, 10) 2 20
1 –6 (2, 10) 4 20
2 –1 (3, 10) 6 20
3 6 (4, 9) 8 18
4 –3 (5, 9) 10 18
5 8 (6,8) 12 16
6 5 (7,7) 14 14
CS 380
Given a circle radius r = 15, demonstrate the
midpoint circle algorithm by determining
positions along the circle octant in the first
quadrant from x = 0 to x = y.
Solution:
p0 = 1 – r = – 14
plot the initial point (x0 , y0) = (0, 15),
2x0 = 0 and 2y0 = 30.
Successive decision parameter values and
positions along the circle path are calculated
using the midpoint method as:
Mid-point Circle Algorithm – Example (2)
0 – 14 (1, 15) 2 30
1 – 11 (2, 15) 4 30
2 – 6 (3, 15) 6 30
3 1 (4, 14) 8 28
4 – 18 (5, 14) 10 28
K Pk (xk+1, yk+1) 2 xk+1 2 yk+1
5 –7 (6,14) 12 28
6 6 (7,13) 14 26
7 –5 (8,13) 16 26
8 12 (9,12) 18 24
9 7 (10,11 ) 20 22
10 6 (11,10) 22 20
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.
Midpoint ellipse algorithm
plots(finds) points of an ellipse
on the first quadrant by dividing
the quadrant into two regions.
Each point(x, y) is then projected
into other three quadrants (-x, y),
(x, -y), (-x, -y) i.e. it uses 4-way
symmetry.
The midpoint ellipse method is applied throughout
the first quadrant in two parts. Now let us take the
start position at (0,ry) and step along the ellipse path
in clockwise order throughout the first quadrant.
Ellipse function can be defined as:
fellipse(x,y)=ry2x2+rx2y2-rx2ry2
Examples:
•A square has all its sides equal to 5cm, and all the angles are at 90°.
•An equilateral triangle has all three sides equal to 10cm and angles measure
to 60°.
•A regular pentagon has 5 equal sides and all the interior angles measures to
108 degrees.
Irregular Polygon
A polygon with an irregular shape. It means the sides and angles of the polygon
are not equal.
Example:
•A quadrilateral with unequal sides.
•An isosceles triangle has only two of its sides equal, and the third side has a
different measurement.
Convex Polygon
In a convex polygon, the measure of the interior angle is less than 180 degrees. It is
exactly opposite to the concave polygon. The vertices of a convex polygon are always
outwards.
Example: See the figure of an irregular hexagon, whose vertices are outwards.
Concave Polygon
In a concave polygon, at least one angle measures more than 180 degrees. The
vertices of a concave polygon are inwards as well as outwards.
Polygon filling is the process of coloring the area of polygon. Area may be defined at
the pixel or geometric level.
Describe the area as the total number of pixels it contain by the boundling pixels that
outline the polygon.
Polygon is an ordered list of vertices as shown in the following figure. For filling
polygons with particular colors, you need to determine the pixels falling on the border
of the polygon and those which fall inside the polygon. In this chapter, we will see how
we can fill polygons using different techniques.
Scan Line Algorithm
This algorithm works by intersecting scanline with polygon edges and fills the polygon
between pairs of intersections. The following steps depict how this algorithm works.
Step 1 − Find out the Ymin and Ymax from the given polygon.
Step 2 − ScanLine intersects with each edge of the polygon from Ymin to Ymax.
Name each intersection point of the polygon. As per the figure shown above, they are
named as p0, p1, p2, p3.
Step 3 − Sort the intersection point in the increasing order of X coordinate
i.e. p0,p1p0,p1, p1,p2p1,p2, and p2,p3p2,p3.
Step 4 − Fill all those pair of coordinates that are inside polygons and ignore the
alternate pairs.
Inside-outside Test
This method is also known as counting number method. While filling an object, we often
need to identify whether particular point is inside the object or outside it. There are two
methods by which we can identify whether particular point is inside an object or outside.
•Odd-Even Rule
•Nonzero winding number rule
Odd-Even Rule
In this technique, we will count the edge crossing along the line from any point x,y to infinity.
If the number of interactions is odd, then the point x,y is an interior point; and if the number
of interactions is even, then the point x,y is an exterior point. The following example depicts
this concept.
From the above figure, we can see that from the point x,yx,y, the number of
interactions point on the left side is 5 and on the right side is 3. From both ends, the
number of interaction points is odd, so the point is considered within the object.
Nonzero Winding Number Rule
This method is also used with the simple polygons to test the
given point is interior or not. It can be simply understood with
the help of a pin and a rubber band. Fix up the pin on one of
the edge of the polygon and tie-up the rubber band in it and
then stretch the rubber band along the edges of the polygon.
When all the edges of the polygon are covered by the rubber
band, check out the pin which has been fixed up at the point to
be test. If we find at least one wind at the point consider it
within the polygon, else we can say that the point is not inside
the polygon.
In another alternative method, give directions to all the
edges of the polygon. Draw a scan line from the point to
be test towards the left most of X direction.
Give the value 1 to all the edges which are going to
upward direction and all other -1 as direction values.
Check the edge direction values from which the scan line
is passing and sum up them.
If the total sum of this direction value is non-zero, then
this point to be tested is an interior point, otherwise it is
an exterior point.
In the above figure, we sum up the direction values from
which the scan line is passing then the total is 1 – 1 + 1 =
1; which is non-zero. So the point is said to be an interior
point.
69
Fill-Area algorithms are used to
fill the interior of a polygonal
shape.
Many algorithms perform fill
operations by first identifying
the interior points, given the
polygon boundary.
70
The basic filling algorithm is commonly used in
interactive graphics packages, where the user
specifies an interior point of the region to be filled.
4-connected pixels
71
[1] Set the user specified point.
[2] Store the four neighboring pixels
in a stack.
[3] Remove a pixel from the stack.
[4] If the pixel is not set,
Set the pixel
Push its four neighboring pixels into the
stack
[5] Go to step 3
[6] Repeat till the stack is empty.
72
void fill(int x, int y) {
if(getPixel(x,y)==0){
setPixel(x,y);
fill(x+1,y);
fill(x-1,y);
fill(x,y+1);
fill(x,y-1);
}
}
73
Requires an interior point.
Involves considerable amount of stack
operations.
The boundary has to be closed.
Not suitable for self-intersecting
polygons
74
Boundary Fill Algorithm
◦ For filling a region with a single
boundary color.
◦ Condition for setting pixels:
Color is not the same as border color
Color is not the same as fill color
75
Start with max y and move to min y or vice versa
For each scan line:
❖ Find by increasinthe intersections of the scan line with
all edges of the polygon.
❖ Sort the intersections g x coordinate.
Assumption:
Start Position
1
3
2 3
2
1
1 4
2 4
2
1
1
2
2
1
5 1
5
1
1
1
Some region remains unfilled
8-connected (Example)
Start Position
4 1 5
5
2 3
4
3
2
1
6
4 1 6
2 3 4
3
2
1
7 8 8
7
4 1 4
2 3 3
2
1
12
11 9 12 11
7 10 10
9
4 1 7
2 3 4
3
2
1
11
11 9
10
7 10
9
7
4 1
4
2 3
3
2
1
9
10
7 10
9
7
4 1
4
2 3
3
2
1
9
7
9
4 1 7
2 3 4
3
2
1
7
4 1 7
2 3 4
3
2
1
4 1
2 3 4
3
2
1
1
2 3
3
2
1
1
2
2
1
1
1
FLOOD-FILL ALGORITHM BOUNDARY-FILL ALGORITHM
It can process the image containing more than one boundary It can only process the image containing single boundary
colours. colour.
Flood-fill algorithm is comparatively slower than the Boundary-fill algorithm is faster than the Flood-fill
Boundary-fill algorithm. algorithm.
Flood-fill algorithms are simple and efficient. The complexity of Bounndary-fill algorithm is high.
Definition
Parameter that affects the way a primitive will be
displayed
Line Attribute
. Type
. Width
. Color
. Pen & Brush
10
Lecture 2 30/9/2008 7
Type
. Solid
. Dotted – very short dash with spacing equal to
or greater than dash itself
. Dashed – displayed by generating an interdash
spacing
Pixel count for the span and interspan length is specified
by the mask . Ex. 111100011110001111
Note : Fixed pixel with dashes can produce unequal length
dashes. It depend on line orientation. So, need to adjust the
number of plotted pixels for different slopes.
10
Lecture 2 30/9/2008 8
Width
. Specify in pixels and proportion of a standard line width.
. Thicker line can be produced by.
. Adding extra pixel vertically when |m| < 1
. Adding extra pixel horizontally when |m| > 1
. Issues:
. Line have different thickness on the slope
. Problem with
. End of the line
. Joining the two lines (polygon)
Width
11
Lecture 2 30/9/2008 0
11
Lecture 2 30/9/2008 1
11
Lecture 2 30/9/2008 2
Pen and Brush
. The selected “pen” or “brush” determine the way a line will be drawn.
. Pens and brushes have size, shape, color and pattern attribute.
. Pixel mask is applied in both of them.
11
Lecture 2 30/9/2008 3
Similar to line : type + width
Thicker curves can be produced by:
1. Plotting additional pixel
2. Filling the space between two concentric circles.
3. Using thicker pen or brush
11
Lecture 2 30/9/2008 4
Width
11
Lecture 2 30/9/2008 5
11
Lecture 2 30/9/2008 6
11
Lecture 2 30/9/2008 7
11
Lecture 2 30/9/2008 8
11
Lecture 2 30/9/2008 9
12
Lecture 2 30/9/2008 0
Text and Characters
•Very important output primitive
• Many pictures require text
• Two general techniques used
– Bitmapped (raster)
– Stroked (outline)
12
Lecture 2 30/9/2008 1
Each character represented (stored) as a 2-D array
– Each element corresponds to a pixel in a rectangular
“character cell”
– Simplest: each element is a bit (1=pixel on, 0=pixel off)
00111000
01101100
11000110
11000110
11111110
11000110
11000110
00000000
12
Lecture 2 30/9/2008 2
Each character represented (stored) as
a series of line segments
– sometimes as more complex primitives
Parameters needed to draw each stroke
– endpoint coordinates for line segments
12
Lecture 2 30/9/2008 3
Characteristics of Bitmapped Characters
• Each character in set requires same amount of memory
to store
• Characters can only be scaled by integer scaling factors
• "Blocky" appearance
• Difficult to rotate characters by arbitrary angles
• Fast (BitBLT)
.The table for each primitive that defines group of attribute values is
called bundled table.
2. Outline font
Bitmap font (or bitmapped font): A
simple method for representing the
character shapes in a particular typeface
is to use rectangular grid pattern.
The character grid only need to be
mapped to a frame buffer position.
Bitmap fonts required more space,
because each variation (size and format)
must be stored in a font cash.
Bold Italic
Graphic primitives such as lines and
arcs are used to define the outline of
each character.
Require less storage since variation
does not require a distinct font cash.
We can produce boldface, italic, or
different size by manipulating the curve
definition for the character outlines.
(1) In computer graphics, the process by which smooth curves and other lines become
jagged because the resolution of the graphics device or file is not high enough to
represent a smooth curve. Smoothing and antialiasing techniques can reduce the effect
of aliasing.
(2) In digital sound, aliasing is a static distortion resulting from a low sampling rate-
below 40 kilohertz (Khz).
Aliasing is generally avoided by applying low pass filters or anti-aliasing filters (AAF) to
the input signal before sampling and when converting a signal from a higher to a lower
sampling rate.
Suitable reconstruction filtering should then be used when restoring the sampled signal to
the continuous domain or converting a signal from a lower to a higher sampling rate.
For spatial anti-aliasing, the types of anti-aliasing include
fast sample anti-aliasing (FSAA), multisampling anti-aliasing, and super sampling.
Anti-aliasing is a technique used in computer graphics to remove the aliasing effect.
The aliasing effect is the appearance of jagged edges or “jaggies” in a rasterized image (an
image rendered using pixels).
Aliasing occurs when real-world objects which comprise of smooth, continuous curves are
rasterized 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 Under
sampling. Aliasing occurs when real-world objects which comprise of smooth, continuous
curves are rasterized using pixels.