Upload
vumien
View
249
Download
2
Embed Size (px)
Citation preview
BRESENHAM’S ALGORITHM
BRESENHAM’S LINE ALGORITHM
UNIT - II
MET71 COMPUTER AIDED DESIGN
Bresenham’s algorithm enables the selection of optimum raster locations to represent a
straight line. In this algorithm either pixels along X or Y directions are incremented by one unit
depending upon the slope of the line. The increment in the other direction is determined by
examining the error or distance between actual line location and the nearest grid locations.
The principle of Bresenham’s algorithm can be explained with the aid of Fig a. If the slope
of the line (in the first octant) is more than 1/2, the pixel point in the Y direction is shifted by one.
Thus lines L1 and L2 passes through pixel (0, 0). For line L2 slope is greater than 1/2; hence the
pixel point is (1, 1) whereas for L1 the slope is less than 1/2 and hence (1, 0) is the pixel point.
Bresenham’s algorithm selects optimum raster locations with minimum computation. To
accomplish this, the algorithm always increments by one unit in either X or Y depending upon the
slope of the line. The increment in the other variable either zero or one is determined by
examining the distance (error) between the actual line location and the nearest grid location. Only
the sign of this error needs be examined.
Consider the line of slope m = 0.4 and passing through (0, 0) in Fig (a). The error team e
is initialized to –1/2. The next raster point can be determined by adding the slope (m) to the error
term.
i.e. e = e + m
e = – 0.5 + 0.4 = – 0.1
Since e is negative, the line will pass below the middle of the pixel. Hence the pixel is at
the same horizontal level i.e., (0, 1). For the next location X is incremented to 2. Error e = -0.1 +
0.4 = 0.3. Since e is positive the line passes above the middle of the raster line. Hence the
location of pixel is (2, 1). Before examining the next pixel location the error term has to be re-
initialized as its value is positive. Re-initialization is done by subtracting one from the current e
value. Hence e = 0.3 - 1 = -0.7. Adding the slope 0.4 we get e = -0.3. Table 3.2 shows the
computed values and the location of pixels. A plot of the pixel location is shown in Fig. (b).
Table: Calculation of Pixel Position
M.SRINIVASSAN, ASST. PROF 1 RGCET
MET71 COMPUTER AIDED DESIGN
Fig. a Location of Pixels Using Fig. b Pixels for Line of
Bresenham Algorithm Slope, m = 0.4
The speed of the Bresenham’s algorithm can be increased by using integer arithmetic and
eliminating division to determine slope.The pseudo code and a C-program for implementing
Bresenham’s algorithm are given below:
Pseudo code for Bresenham’s line-drawing algorithm
Given a line from x1, y1 to x2, y2...
dx is the difference between the x components of end points
dy is the difference between the y components of end points
ix is the absolute value of dx
iy is the absolute value of dy
inc is the larger of dx, dy
plotx is x1
ploty is y1 (the beginning of line)
x starts at 0
y starts at 0
plot a pixel at plotx, ploty
increment x using ix
increment y using iy
plot is false
if x is greater than inc
plot is true
decrement x using inc increment
plotx if dx is positive decrement
plotx id dx is negative
if y is greater than inc
plot is true
decrement y using inc increment
ploty if dy is positive decrement
ploty if dy is negative
if plot is true, plot a pixel at plotx, ploty
M.SRINIVASSAN, ASST. PROF 2 RGCET
increment i.
Program in Turbo-C to draw a line
# include <stdio. h>
# include <graphics. h>
# include <stdlb. h>
void draw line (int x1, int y1, int x2, int y2),
void main (void)
{
MET71 COMPUTER AIDED DESIGN
draw line (100, 100, 50, 50) ;
}
void draw line (int x1, int y1, int x2 m int y2)
{
{
{
else
}
int dx, dy, inc, ix, iy, x, y, plot, plotx, ploty, i ;
int gd, gm ;
gd = DETECT ;
initgraph (&gd, &gm, “ “) ;
dx = x1 – x2 ;
dy = y1 – y2 ;
ix = abs (dx) ;
iy = abs (dy) ;
inc = max (ix, iy) ;
x = y = 0 ;
plot x = x1 ;
plot y = y1 ;
for (i = 0 ; i <inc ; i ++)
x + = ix ;
y + iy ;
plot = 0
if (x > inc)
plot = 1 ;
x – = inc ; if
(dx < 0) plot
x – = 1 ;
plotx + = 1 ;
if (y > inc)
M.SRINIVASSAN, ASST. PROF 3 RGCET
{
else
}
else
}
plot = 1 ;
y – = inc ;
if (dy)
ploty – = 1 ;
ploty + = 1 ;
if (plot)
putpixel (plotx, ploty, 1)
MET71 COMPUTER AIDED DESIGN
getch ( ) ;
closegraph ( ) ;
BRESENHAM’S CIRCLE ALGORITHM
An efficient algorithm for generating a circle has been developed by J. Bresenham.
Values of a circle centered at the origin are computed in the sector X = 0 to X = R /2 where R is
the radius of the circle. The symmetry of the circle is used to obtain the pixels corresponding to
other sectors.
Bresenham’s circle algorithm can be explained as follows:
Consider an origin-centred circle. The algorithm begins at X = 0 and Y = R. In the first
quadrant of the circle, Y is a monotonically decreasing function of X. Referring to Fig., (Xi, Yi) is a
point on the circle. For clockwise generation of the circle there are only three possible selections
of the next pixel, which represents the circle. These positions are also shown in Fig. 3.4. The
algorithm is designed to choose the pixel which minimizes the square of the distance between
one of these pixels and the true circle, i.e., the minimum of
H = [ (Xi + 1) 2 + (Yi)2 – R 2]
V = [ (Xi ) 2 + ( Yi – 1) 2 – R 2 ]
D = [ ( Xi + 1) 2 + ( Yi – 1 ) 2 – R 2 ]
Fig. First Quadrant Pixel Position
M.SRINIVASSAN, ASST. PROF 4 RGCET
MET71 COMPUTER AIDED DESIGN
A flow chart to obtain the pixel values for representing a circle is given in Fig. It is
sufficient to obtain the pixel values for 1/8th of a circle, the remaining obtained by
symmetry.
A program which implements Bresenham’s algorithm follows.
include <stdio.h>
include <graphics.h>
Void draw circle (int xc, int yc, int y) ;
Void symmetry (int x, int y, int xc, int yc) ;
double ratio ;
Void main (void)
{
draw circle (300, 150, 50) ;
}
int d, x ;
int gd, gm ;
gd = DETECT ;
initgraph (&gd, &gm, “ “) ;
d = 3 - 2 * y ;
ratio = 1.5
x = 0 ;
while (x < y)
{
symmetry (x, y, xc, yc) ;
if (d < 0)
d + = 4 * x + 6 ;
else
{
d + = 4 * (X – Y) + 10 ;
y - :
}
x++ ;
if (x = = y)
symmetry (x, y, xc, yc) ;
}
getch ( ) ;
closegraph ( ) ;
}
void symmetry (int x, int y, int xc, int yc)
{
M.SRINIVASSAN, ASST. PROF 5 RGCET
int x start, x end, x out ;
int y start, y end, y out ;
x start = x * ratio
x end = (x + 1) * ratio ;
y start = y * ratio ;
y end = (y + 1) * ratio
MET71 COMPUTER AIDED DESIGN
for (x out = x start ; x out < x end ; ++ x out)
{
put pixel (x out + xc, y + yc, 1 ) ;
put pixel (x out + xc, –y + yc, 1) ;
put pixel (-x out + xc, –y + yc, 1) ;
put pixel (-x out + xc, y + yc, 1) ;
}
(y out = y start ; y out < y end ; ++y out)
{
put pixel (y out + xc, x + yc, 1 ) ;
put pixel (y out + xc, –x + yc, 1) ;
put pixel (-y out + xc, –x + yc, 2) ;
put pixel (-y out + xc, x + yc, 1) ;
}
M.SRINIVASSAN, ASST. PROF 6 RGCET
MET71 COMPUTER AIDED DESIGN
Flow Chart to Determine Pixel Values
EXAMPLE OF BRESENHAM’S ALGORITHM
Consider a circle with centre at the origin and radius equal to 10 units. Because of
symmetry only the first octant is considered here.
X = 0
Y = 10
i = 2 (1–10) = –18
LIMIT = 0
PLOT (0, 10)
Y1 > LIMIT CONTINUE
i < 0 GOTO 2
M.SRINIVASSAN, ASST. PROF 7 RGCET
2 = 2 (–18) + 2 (10) –1 = –17 < 0 GOTO 10
10 X = 0 + 1 = 1
i = –17 + 2 + 1 = –14
GOTO 1
1 PLOT (1, 10)
Yi > LIMIT CONTINUE
i < 0
= 2 (–14) + 2 (10) –1
= –9 < 0 GOTO 10
X = 1 + 1
i = –14 + 2 (2) + 1
= –9
GOTO 1
PLOT (2, 10)
MET71 COMPUTER AIDED DESIGN
The procedure is to be continued till the required point is reached. The results are given in Table
and are plotted in Fig. shown below.
Pixel Values for Circle
Plot of First Octant of a Circle
M.SRINIVASSAN, ASST. PROF 8 RGCET
TRANSFORMATION IN GRAPHICS
MET71 COMPUTER AIDED DESIGN
Geometric transformations provide a means by which an image can be enlarged in size,
or reduced, rotated, or moved. These changes are brought about by changing the co-ordinates of
the picture to a new set of values depending upon the requirements.
CO-ORDINATE SYSTEMS USED IN GRAPHICS AND WINDOWING
Transformations can be carried out either in 2-dimensions or in 3-dimensions. The theory
of two-dimensional transformations is discussed first in this chapter. This is then extended to
three dimensions. When a design package is initiated, the display will have a set of co-ordinate
values. These are called default co-ordinates. A user co-ordinate system is one in which the
designer can specify his own co-ordinates for a specific design application. These screen
independent coordinates can have large or small numeric range, or even negative values, so that
the model can be represented in a natural way. It may, however, happen that the picture is too
crowded with several features to be viewed clearly on the display screen. Therefore, the designer
may want to view only a portion of the image, enclosed in a rectangular region called a window.
Different parts of the drawing can thus be selected for viewing by placing the windows. Portions
inside the window can be enlarged, reduced or edited depending upon the requirements. Figure
shows the use of windowing to enlarge the picture.
VIEW PORT
It may be sometimes desirable to display different portions or views of the drawing in
different regions of the screen. A portion of the screen where the contents of the window are
displayed is called a view port. Let the screen size be X = 0 to 200 and Y = 0 to 130. A view port
can be defined by the co-ordinates say X1 = 65, X2 = 130, Y1 = 50 and Y2 = 100. If we use the
same window as in Fig., the definition of this view port will display the image in the right hand top
quarter of the screen (Fig.) choosing different view ports multiple views can be placed on the
screen. Fig. shows four views of a component displayed using view port commands.
M.SRINIVASSAN, ASST. PROF 9 RGCET
MET71 COMPUTER AIDED DESIGN
CLIPPING
Clipping is the process of determining the visible portions of a drawing lying within a
window. In clipping each graphic element of the display is examined to determine whether or not
it is completely inside the window, completely outside the window or crosses a window boundary.
Portions outside the boundary are not drawn. If the element of a drawing crosses the boundary
the point of inter-section is determined and only portions which lie inside are drawn. Readers are
advised to refer to books on computer graphics for typical clipping algorithms like Cohen-
Sutherland clipping algorithm. Fig. Shows an example of clipping.
HIDDEN SURFACE REMOVAL
One of the difficult problems in computer graphics is the removal of hidden surfaces from
the images of solid objects. In Fig. (a) An opaque cube is shown in wire frame representation.
Edges 15, 48, 37, 14, 12, 23, 58 and 87 are visible whereas edges 56, 67 and 26 are not visible.
Correspondingly, surfaces 1265, 2673 and 5678 are not visible since the object is opaque. The
actual representation of the cube must be as shown in Fig. (b).
M.SRINIVASSAN, ASST. PROF 10 RGCET
MET71 COMPUTER AIDED DESIGN
There are a number of algorithms available for removal of hidden lines and hidden surfaces.
Table gives a list of algorithms for hidden line removal and hidden surface removal.
Table Algorithms for Hidden Line and Hidden Surface
There are two popular approaches to hidden surface removal. These are scan line based
systems and Z-buffer based systems. Other important approaches are area subdivision and
depth list schemes.
2-D & 3-D TRANSFORMATION
2-D TRANSFORMATIONS
In computer graphics, drawings are created by a series of primitives which are
represented by the co-ordinates of their end points. Certain changes in these drawings can be
made by performing some mathematical operations on these co-ordinates. The basic
transformations are scaling, translation and rotation.
ROTATION
Another useful transformation is the rotation of a drawing about a pivot point. Consider
Fig. Point P1 (40, 20) can be seen being rotated about the origin through an angle, =45°, in
the anti-clockwise direction to position P2. The co-ordinates of P2 can be obtained by multiplying
the co-ordinates of P1 by the matrix:
M.SRINIVASSAN, ASST. PROF 11 RGCET
MET71 COMPUTER AIDED DESIGN
SCALING
Changing the dimensions of window and view port, it is possible to alter the size of
drawings. This technique is not satisfactory in all cases. A drawing can be made bigger by
increasing the distance between the points of the drawing. In general, this can be done by
multiplying the co-ordinates of the drawing by an enlargement or reduction factor called scaling
factor and the operation is called scaling. Referring to Fig., P1 (30, 20) represents a point in the
XY plane. In matrix form, P1 can be represented as:P1 = [30, 20]
If we multiply this by a matrix
M.SRINIVASSAN, ASST. PROF 12 RGCET
MET71 COMPUTER AIDED DESIGN
An example of scaling in the case of a triangle is shown in Fig. Fig. (a) Shows the original picture
before scaling. Fig. (b) Shows the triangle after the co-ordinates are multiplied by the scaling
matrix.
TRANSLATION
Moving drawing or model across the screen is called translation. This is accomplished by adding
to the co-ordinates of each corner point the distance through which the drawing is to be moved
(translated). Fig. shows a rectangle (Fig.(a)) being moved to a new position (Fig.(b)) by adding 40
units to X co-ordinate values and 30 units to Y coordinate values. In general, in order to translate
drawing by (TX , TY ) every point X, Y will be replaced by a point X1 , Y1 where
X1 = X + TX
Y = Y + TY M.SRINIVASSAN, ASST. PROF 13 RGCET
MET71 COMPUTER AIDED DESIGN
REFLECTION
Shading is an important element in 3-D computer graphics, as it gives the necessary
realism to the representation of the object. Fig. shows what happens when light is incident on a
surface. Light gets partly reflected, partly scattered, partly absorbed and partly transmitted. The
relative magnitudes of these are influenced by many factors like the opaqueness of the solid,
surface texture etc. The intensity and wave length of light reflected from a surface depends on
the incident angle, the surface roughness, incident wave length and the electrical properties of
the surface. In computer graphics designer can model reflected light and transmitted light.
Reflected light could be categorized into two types:
Diffuse reflection: Diffuse light is scattered in all directions and is responsible for the color of the
object. The light is reflected from a surface due to molecular interaction between incident light
and the surface material. A yellow object for example, absorbs white light and reflects yellow
component of the light. This property is attributed to diffuse reflection. When light from a point
source is incident on a solid object, the diffuse reflection depends upon the angle of inclination of
the surface with that of the incident beam. More important source of illumination of the objects is
ambient light, which is the result of multiple reflections from the walls and other objects in the
vicinity and is incident on a surface in all directions.
M.SRINIVASSAN, ASST. PROF 14 RGCET
MET71 COMPUTER AIDED DESIGN
Specular reflection: A perfectly matt surface scatters light in all directions. Most of the surfaces
that we deal with, however, have different levels of glossiness. The specular deflection deals with
the reflection of the surface due to glossiness. Consider Fig. which shows the reflection of light
on a surface. If the surface is perfectly glossy the reflected light is in the direction of R. If the
surface becomes more and more matt, the reflection intensity varies as in a profile shown as the
shaded area of the figure.
A technique to model reflection from an object based on specular reflection has been proposed
by Phong. This model assumes that:
• Light sources are point sources.
• All geometry except the surface normal is ignored.
• Diffuse and specular components are modeled as local components
• The model to simulate the specular term is empirical.
• The color of specular reflection is that of the light source
• The ambient lighting is constant.
SHEARING
A shearing transformation produces distortion of an object or an entire image. There are two
types of shears: X-shear and Y-shear. A Y-shear transforms the point (X, Y) to the point (X1, Y1)
by a factor Sh1, where
X1 = X
Y1 = Sh1. X + Y
Fig. shows Y shear applied to a drawing.
M.SRINIVASSAN, ASST. PROF 15 RGCET
MET71 COMPUTER AIDED DESIGN
HOMOGENEOUS TRANSFORMATIONS
Each of the above transformations with the exception of translation can be represented as
a row vector X, Y and a 2 X 2 matrix. However, all the four transformations discussed above can
be represented as a product of a 1 X 3 row vector and an appropriate 3 X 3 matrix. The
conversion of a two-dimensional co-ordinate pair (X, Y) into a 3-dimensional vector can be
achieved by representing the point as [X Y 1]. After multiplying this vectorby a 3 X 3 matrix,
another homogeneous row vector is obtained [X1 Y1 1]. The first two terms in this vector are the
co-ordinate pair which is the transform of (X, Y). This three dimensional representation of a two
dimensional plane is called homogeneous coordinates and the transformation using the
homogeneous co-ordinates is called homogeneous transformation. The matrix representations of
the four basic transformations are given below.
M.SRINIVASSAN, ASST. PROF 16 RGCET
Translation: COMBINATION TRANSFORMATIONS
MET71 COMPUTER AIDED DESIGN
Sequences of transformations can be combined into a single transformation using the
concatenation process. For example, consider the rotation of a line about an arbitrary point. Line M.SRINIVASSAN, ASST. PROF 17 RGCET
MET71 COMPUTER AIDED DESIGN
AB is to be rotated through 45° in anticlockwise direction about point A (Fig (a)). Fig. (b) Shows
an inverse translation of AB to A1B1. A1B1 is then rotated through 45° to A2B2. The line A2B2 is
then translated to A3B3
Since matrix operations are not commutative, care must be taken to preserve the order in which
they are performed while combining the matrices.
3-D TRANSFORMATIONS
It is often necessary to display objects in 3-D on the graphics screen. The transformation
matrices developed for 2-dimensions can be extended to 3-D.
M.SRINIVASSAN, ASST. PROF 18 RGCET
MET71 COMPUTER AIDED DESIGN
PROJECTIONS
In drawing practice, a 3-dimensional object is represented on a plane paper. Similarly in
computer graphics a 3-dimensional object is viewed on a 2-dimensional display. A projection is a
transformation that performs this conversion. Three types of projections are commonly used in
engineering practice: parallel, perspective and isometric.
PARALLEL (ORTHOGONAL) PROJECTION
This is the simplest of the projection methods. Fig. shows the projection of a cube on to a
projection plane. The projectors, which are lines passing through the corners of the object are all
parallel to each other. It is only necessary to project the end points of a line in 3-D and then join
these projected points. This speeds up the transformation process. However a major
disadvantage of parallel projection is lack of depth information.
M.SRINIVASSAN, ASST. PROF 19 RGCET
PERSPECTIVE PROJECTION
MET71 COMPUTER AIDED DESIGN
The perspective projection enhances the realism of displayed image by providing the
viewer with a sense of depth. Portions of the object farther away from the viewer are drawn
smaller than those in the foreground. This is more realistic as it is the way we see an object. In
perspective projection the projections connect the eye with every point of the object and therefore
all projections converge to the eye.
As the display screen is a two-dimensional space, we cannot display three-dimensional
objects but only their projections. Computationally, projection transformations are in general quite
expensive. Since the generation of a perspective view of a given object may require the
projection transformation of a considerable number of points, the projection applied is usually
restricted to the central projection and sometimes to even simpler parallel or orthographic
projection in order to keep the execution time for the generation of a perspective view within
reasonable limits.
Figure explains the central projection as it is usually applied in computer graphics. The
problem is to determine the projection of an object point, located somewhere in a three-
dimensional space, onto a plane in that space, called the image plane. This projection is called
the image point of the corresponding object point. In a central projection, the center of projection,
also called the viewpoint, is located on one of the axes of the three dimensional orthogonal co-
ordinate systems. In Figure the viewpoint is arbitrarily located on the Z-axis. This fact can also be
expressed by saying that the optical axis is aligned with the Z-axis of the co-ordinate system. The
image plane is perpendicular to the optical axis; i.e., in figure it is parallel to the xy-plane of the
co-ordinate system. This fact accounts for the simplicity of a central projection. Let the co-
ordinates in the two-dimensional co-ordinate system of the image plane, which we may call the
image co-ordinate system, be denoted by X and Y. Let the distance of the image plane to the
origin of the spatial co-ordinate system be denoted by Z and the distance of the viewpoint to the
origin of the co-ordinate system by Z.
M.SRINIVASSAN, ASST. PROF 20 RGCET
MET71 COMPUTER AIDED DESIGN
This projection is called orthographic. The orthographic projection is a special form of the
parallel projection by which parallel lines of the three-dimensional object are transformed into
parallel lines of its image.
ISOMETRIC PROJECTION
In isometric projection the three orthogonal edges of an object are inclined equally to the
projection plane. Because of the relative ease of projection and the ability to give 3-D perception,
isometric projection is widely used in computer aided design. In computer aided design the co-
ordinates of the drawing are available in their natural co-ordinate system. These are transformed
suitably to enable the viewer different views or rotate the object in such away that all the faces of
the object are made visible continuously.
There are several uses for this technique in product design. Hence good design packages
incorporate several viewing transformation techniques. The viewing parameters depend upon the
system graphics standard followed in developing the graphics package. The algorithms for these
viewing transformations are available in literature.
21
M.SRINIVASSAN, ASST. PROF 42 RGCET