Bresenham Algorithm: Implementation and Analysis in Raster Shape
Bresenham Algorithm: Implementation and Analysis in Raster Shape
1, JANUARY 2013 69
Abstract—One of the most important aspect that have to spline curves and surfaces, and polygon color areas.
solve in raster objects is to describe the structure of the (Ammeraal 1987; Bresenham 1971; Bresenham 1977)
individual objects and their coordinate locations within the The rest of this paper organized as follows: Part 2 will
scene. We required to implement the graphics output discussed about Bresenham algorithm for line, circle, and
primitives. The Output primitives are very important since
the performance of the graphics depend on the Primitives.
polygon. next the implementation and conclusion.
Point positions and straight-line segments are the simplest
geometric primitives. We focus on these parts in this II. BRENSENHAM’S ALGORITHM FOR LINE, CIRCLE, AND
research. In this paper we will implement and analysis how POLYGON
accurate and efficient raster line-generating algorithm,
develop by Bresenham, that uses only incremental integer In this section, the accuracy and efficiency of raster-
calculations. The implementation will be expanded to line generating algorithm, developed by Bresenham, that
display circles and other curves. The analysis will be focus uses only incremental integer calculations will be
on numerical results, error produced, computation speed, elaborated. The Bresneham’s line algorithm will be
and display. The language that used in this implementation adapted to display circles and other curves. (Mersereau
is C++ with OpenGL. 1979; Rappoport 1991; Pitteway 1985; Klassen 1991).
To illustrate Bresenham’s approach, we first consider
Index Terms—Bresenham Algorithm, numerical result, the scan-conversion process for lines with positive slope
error produced, computation speed, display, OpenGL less than 1.0. Pixel positions along a line path are then
determined by sampling at unit x intervals. Starting from
the left endpoint (x0, y0) of a given line, we step to each
I. INTRODUCTION successive columns ( x position) and plot the pixel whose
A general software package for graphics applications, scan-line y value is closet to the line path. Assuming that
sometimes referred to as a computer-graphics application the pixel at (xk, yk) is to be displayed, next to be decide
programming interface (CG API), provides a library of which pixel to plot in column xk+1 = xk + 1. The choices
functions that we can use within a programming are the pixels at positions (xk+1, yk) and (xk+1, yk+1).
languages such as C++ to create pictures (Haque et al 2006; At sampling position xk+1, label the vertical pixel
Ring Che 2003] . One the first thing to do when creating a separations from the mathematical line path as dlower and
picture is to describe the component parts of the scene to dupper The y coordinate on the mathematical in at pixel
be displayed. Picture components could be trees and column position xk+1 is calculated as
terrain, furniture and walls, storefronts, and street scenes, y = m( x k + 1) + b (1)
automobiles and billboards, atoms and molecules, or stars Then
and galaxies. For each type of scene, we need to describe d lower = y − y k
the structure of the individual objects and their coordinate = m( xk + 1) + b − y k (2)
locations within the scene. Those functions in graphics
and
package that we use to describe the structure of the
d upper = ( y k + 1) − y
individual objects and their coordinate locations within
the scene (Prabukumar 2007; Ray, B.K 2006; Stucki 1991; = y k + 1 − m( xk + 1) − b
Staunton 1989). To determine which of the two pixels is closet to the line
These functions in a graphics package that we use to path, we can set up an efficient test that is based on the
describe the various picture components are called the difference between the two pixel separations:
graphics output primitives, or simply primitives. The d lower − d upper = 2m( xk + 1) − 2 y k + 2b − 1 (3)
output primitives describing the geometry of objects are
typically referred to as geometric primitives. Point
A decision parameter pk for the k-th step in the line
positions and straight-line segments are the simplest
algorithm can be obtained by rearranging (3) so that it
geometric primitives. Additional geometric primitives
involves only integer calculations. We accomplish this by
that can be available in a graphics package included Δy
circles, and other conics sections, quadratic surfaces, substituting m= , where Δy and Δx are the vertical and
Δx
horizontal separations of the endpoints positions, and screen center positions (xc, yc), we can first set up our
define the decisions parameter as algorithm to calculate pixel positions around a circle path
p k = Δx(d lower − d upper ) = 2Δy.xk − 2Δx. y k + c (4) centered at the coordinate (4) origin (0,0). Then each
The sign of pk is the same as the sign of dlower – dupper, calculated position (x, y) is moved to its proper screen
since Δx > 0. Parameter c is constant and has the value position by adding xc to x and yc to y. Along the circle
2Δy + Δx(2b − 1) , which is independent of the pixel
section from x =0 to x = y in the first quadrant, the slope
of the curve varies from 0 to –1.0. Therefore, we can take
positions and will be eliminated n the recursive
unit steps in the positive x direction over this octant and
calculations for pk. if the pixel at yk is “closer” to the line
use a decision parameter to determine which of the two
path than the pixel at yk+1 (that is dlower < dupper), the
possible pixel positions in any column is vertically closer
decision parameter pk is negative. In that case, we plot the
to the circle path. Positions in the other seven octants are
lower pixel; otherwise we plot upper pixel.
then obtained by symmetry.
Coordinate changes along the line occur in unit steps in
To apply the midpoint method, we define the circle
either the x or y directions. Therefore, we can obtain the
function as
values of successive decision parameters using
incremental integer calculations. At step k+1, the f circle = x 2 + y 2 − r 2 (8)
decision parameter is evaluated from (4) as
p k +1 = 2Δy.xk +1 − 2Δx. y k +1 + c (5) Any point (x,y) on the boundary (5)of the circle with
Subtracting (4) from the preceding equation, we have radius r satisfies the equation f circle ( x, y ) = 0 . If the
point is in the interior of the circle, the circle function is
p k +1 − p k = 2Δy ( x k +1 − x k ) − 2Δx ( y k +1 − y k ) (6) the circle
negative. An dif the point is outside the circle,
But xk+1 = xk + 1 so that function is positive. To summarize, the relative position
p k +1 = p k + 2Δy − 2Δx( y k +1 − y k ) (6) of any point (x,y) can be determined by
where the term yk+1 – yk is either 0 or 1, depending on
the sign of parameter pk. ⎧ < 0, if ( x, y ) is inside the circle boundary.
⎪
This recursive calculation of decision parameters is f circle (c, y ) = ⎨ = 0, if ( x, y) is on the cirle boundary (9)
⎪> 0, if ( x, y) is outside the circle boundary
performed at each integer x position, starting at the left ⎩
coordinate endpoints of the line. The first parameter, p0 is checking the sign of the circle function:
evaluated from (4) at the starting pixel position (x0, y0)
Δy The test on (9) are performed for the midpoints
and with m evaluated as :
Δx between pixels near the circle path at each sampling step.
p 0 = 2 Δy − Δx (7) Thus, the circle function is the decision parameter in the
Herewith the summarizing of Brensehma line drawing midpoint algorithm, and we can set up incremental
for a line with a positive slope less than 1 in the following calculations for this function as we did in the line
outline of the algorithm. The constant 2Δy and 2Δy − 2Δx algorithm.
Assuming that we have just plotted the pixel at (xk,
are calculated once for each line to be scan converted, so
yk), we next need to determine whether the pixel at
the arithmetic involves only integer addition and
position (xk+1, yk) or the one at position (xk +1, yk – 1) is
subtraction of these constants.
closer to the circle. Our decision parameter is the circle
function (8) evaluated at the midpoint between these two
Algorithm Bresenham’s Line-Drawing Algorithm for
pixels:
m < 1 .0 ⎛ 1⎞
p k = f circle( ⎜ x k + 1, y k − ⎟
1. Input the two-line endpoints and store the left ⎝ 2⎠
endpoint in (x0, y0). ⎛ 1⎞
2
2. Set the color for frame-buffer position (x0, y0); p k = ( x k + 1) 2 + ⎜ y k − ⎟ − r 2 (10)
⎝ 2⎠
i.e. plot the first point.
3. Calculate the constants Δx, Δy, 2Δy & 2Δy-2Δx,
and obtain the starting value for the decision
If pk < 0, this midpoint is inside the circle and the
parameter as: p0 = 2Δy - Δx pixel on the scan line yk is closer to the circle boundary.
4. At each xk along the line, starting at k = 0, Otherwise, the midposition is outside or on the circle
perform the following test. If pk < 0, the next boundary, and we select the pixel on the scan line yk –1.
point to plot is (xk + 1, yk) and pk+1 = pk + 2Δy. Successive decision parameters are obtained using
Otherwise, the next point to plot is (xk + 1, yk+1) incremental calculations. We obtain a recursive
and pk+1 = pk + 2Δy - 2Δx. expression for the next decision parameter by evaluating
5. Perform step 4 Δx –1 times. the circle function at sampling position xk+1+1 = xk + 2;
⎛ 1⎞
p k +1 = f circle( ⎜ x k +1 + 1, y k +1 − ⎟
As in the raster line algorithm, we sample at unit ⎝ 2⎠
intervals and determine the closet pixel position to the 2
⎛ 1⎞
specified circle path at each step. For a give radius r and = [( xk + 1) + 1)]2 + ⎜⎜ y k +1 − ⎟⎟ − r 2
⎝ 2⎠
Figure
Second Line1. Bresenham
(-25,-20)Implementation
to (-17,-16) for Line
Third Line (10, 15) to (20, 20)
First Line (20, 10) to (30, 18)
B. Circle
#include <GL/glut.h>
The implementation for Circle, with radius: r = 10, r = 25, #include <stdlib.h>
& r = 49. #include <math.h>
#include <iostream.h>
Program Modification :
{
struct scrPt setPixel (circCtr.x + circPt.x, circCtr.y + circPt.y);
{GLint x, y;}; setPixel (circCtr.x - circPt.x, circCtr.y + circPt.y);
setPixel (circCtr.x + circPt.x, circCtr.y - circPt.y);
GLsizei winWidth = 600, winHeight = 500; setPixel (circCtr.x - circPt.x, circCtr.y - circPt.y);
setPixel (circCtr.x + circPt.y, circCtr.y + circPt.x);
void init (void) setPixel (circCtr.x - circPt.y, circCtr.y + circPt.x);
{ setPixel (circCtr.x + circPt.y, circCtr.y - circPt.x);
glClearColor (1.0, 1.0, 1.0, 1.0); glMatrixMode setPixel (circCtr.x - circPt.y, circCtr.y - circPt.x);}
(GL_PROJECTION);
gluOrtho2D (0.0, 200.0, 0.0, 150.0);} void displayFcn (void)
{
void setPixel (GLint x, GLint y) scrPt circCtr; GLint radius;
{ glClear (GL_COLOR_BUFFER_BIT);
glBegin (GL_POINTS);
circCtr.x = 100; circCtr.y = 400; radius = 10;
glVertex2i (x, y); circleMidpoint (circCtr, radius);
circCtr.x = 280; circCtr.y = 400; radius = 25;
glEnd ( );} circleMidpoint (circCtr, radius);
circCtr.x = 500; circCtr.y = 400; radius = 49;
void circleMidpoint (scrPt circCtr, GLint radius) circleMidpoint (circCtr, radius);
{
scrPt circPt; glFlush ( );}
C. Ellipse //<<<<<<<<<<<<<<<<<<<<<<<<
main >>>>>>>>>>>>>>>>>>>>>>
Ellipse with parameters: (rx = 8 and ry = 6), (rx = 12 and void main(int argc, char** argv)
ry = 16) & (rx = 4 and ry = 6). {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
Program Modification : glutInitWindowSize(640,580);
glutInitWindowPosition(100, 50);
glutCreateWindow("Raster Ellipse");
#include <windows.h> glutDisplayFunc(myDisplay);
#include <gl/Gl.h> myInit();
#include <gl/glut.h> glutMainLoop();
}
//<<<<<<<<<<<<<<<<<<<<<<<
myInit >>>>>>>>>>>>>>>>>>>> inline int round (const float a) { return int
void myInit(void) (a + 0.5); }
{
glClearColor(1.0,1.0,1.0,0.0); void ellipseMidpoint (int xCenter, int yCenter,
glColor3f(0.0f, 0.0f, 0.0f); int Rx, int Ry)
glPointSize(2.0); {
glMatrixMode(GL_PROJECTION); int Rx2 = Rx * Rx; int Ry2 = Ry * Ry;
glLoadIdentity(); int twoRx2 = 2 * Rx2; int twoRy2 = 2 * Ry2;
gluOrtho2D(0.0, 640.0, 0.0, 480.0); int p; int x = 0;
} int y = Ry; int px = 0;
int py = twoRx2 * y;
void ellipseMidpoint (int xCenter, int yCenter, void ellipsePlotPoints (int, int, int, int);
int Rx, int Ry);
ellipsePlotPoints (xCenter, yCenter, x, y);
//<<<<<<<<<<<<<<<<<<<<<<<<
myDisplay >>>>>>>>>>>>>>>>> /* Region 1 */
void myDisplay(void) p = round (Ry2 - (Rx2 * Ry) + (0.25 * Rx2));
{ while (px < py) {
glClear(GL_COLOR_BUFFER_BIT); x++;
glBegin(GL_POINTS); px += twoRy2;
ellipseMidpoint (200, 200, 8, 6); // if (p < 0)
first ellipse p += Ry2 + px;
ellipseMidpoint (300, 300, 12, 16); // else {
second ellipse y--;
ellipseMidpoint (400, 400, 4, 6); // py -= twoRx2;
third ellipse p += Ry2 + px - py;
glEnd(); }
glFlush(); ellipsePlotPoints (xCenter, yCenter, x, y);
} }
/* Region 2 */ }
p = round (Ry2 * (x+0.5) * (x+0.5) + Rx2 * (y- }
1) * (y-1) - Rx2 * Ry2);
while (y > 0) { void ellipsePlotPoints (int xCenter, int yCenter,
y--; py -= twoRx2; if (p > 0) {p += Rx2 int x, int y)
- py;} {
else { glVertex2i (xCenter + x, yCenter + y);
x++; px += twoRy2; p += Rx2 - py + glVertex2i (xCenter - x, yCenter + y);
px;} glVertex2i (xCenter + x, yCenter - y);
glVertex2i (xCenter - x, yCenter - y);
ellipsePlotPoints (xCenter, yCenter, x, y);
}
1000
Models and Image Processing, 53(4), 324–339. (1991).
800 Line
Circle
[15] Bimal kumar Ray, An alternative approach to circle
600 Elipse
drawing, Journal.Indian Institude of Science, 86,617-
400 623(2006).
200
[16] M.Prabukumar. Article: Line Drawing Algorithm on an
Interleaved Grid. International Journal of Computer
Applications, 2007., 19(4):1-7,
0
(1000000, 0) (1000000, 10) (1000000, 100) (1000000, 1000) (100000, 10000) (1000000, 100000)
REFERENCES
[1] Ammeraal, L., 1987. Computer Graphics for IBM PC.
John Wiley & Sons, New York.
[2] Bresenham. J,. 1971. Algorithm for computer control of a
digital plotter. IBM Systems Journal, 4(1):25–30, 1965.
[3] Bresenham. J,. 1977. A linear algorithm for incremental
digital display of circular arcs. Communications of ACM,
20(2):100-106.
[4] Donald, E., 1986. METAFONT the Program. Addison-
Wesley, Reading, Massachusetts.
[5] Foley, J., Dam, A., Feiner, S., Hughes, J., 1990.Computer
Graphics: Principles and Practice. Addison-Wesley,
Reading, Massachusetts.
[6] Foley, J., Dam, A., Feiner, S., Hughes, J., 1993.
Introduction to: Computer Graphics. Addison-Wesley,
Reading, Massachusetts.
[7] Klassen, R., 1991. Integer forward differencing of cubic
polynomials: Analysis and algorithms. ACM Transactions
on Graphics, 10(2):152-181.
[8] Pitteway, M., 1985. Algorithms of Conic Generation.
Fundamental Algorithms for Computer Graphics, NATO
ASI Series, p.219-237.