Sei sulla pagina 1di 7

EXPERIMENT: 2

Aim
Write a program to implement Bresenham Line Generation Algorithm.
Theory
Line:
A line connects two points. It is a basic element in graphics. To draw a
line, you need two points between which you can draw a line.
There are three-line drawing algorithms:
1. DDA
2. Bresenham’s
3. Mid-Point
Where,
We refer the one point of line as X0, Y0 and the second point of line as
X1, Y1. The lines appear continuous but they are actually made of
pixels. The lines are drawn by a process known as rasterization.
Some terms associated with line generation:
Rasterization: Process of determining which pixels provide the best
approximation to a desired line on the screen.
Scan Conversion: Combination of rasterization and generating the
picture in scan line order.
Following is the explanation of Bresenham algorithm.

Bresenham Algorithm:
Bresenham line algorithm is an algorithm that determines the points of
an n-dimensional raster that should be selected in order to form a close
approximation to a straight line between two points. It is commonly used
to draw line primitives in a bitmap image,as it uses only an integer
addition ,subtraction and bitshifting ,all of which are very cheap
operations in standard computer architectures.It is an incremental error
algorithm .It is one of the earliest algorithms developed in the field of
computer graphics.

Bresenham algorithm chooses the integer y corresponding to the pixel


center that is closest to ideal(fractional) y for same x; on successive
columns y can remain the same or increase by 1. The general equation of
the line through the endpoints is given by:
(y-y0/y1-y0) = (x-x0/x1-x0)
Since we know the column, x, the pixel's row, y, is given by rounding
this quantity to the nearest integer:
y = (y1-y0/x1-x0)*(x-x0) + y0

The slope depends on the endpoint coordinates only and can be


precomputed, and the ideal y for successive integer values of x can be
computed starting from and repeatedly adding the slope.

In practice, the algorithm does not keep track of the y coordinate, which
increases by m = ∆y/∆x each time the x increases by one; it keeps an
error bound at each stage, which represents the negative of the distance
from (a) the point where the line exits the pixel to (b) the top edge of the
pixel. This value is first set to m − 0.5 (due to using the pixel's center
coordinates), and is incremented by m each time the x coordinate is
incremented by one. If the error becomes greater than 0.5, we know that
the line has moved upwards one pixel, and that we must increment our y
coordinate and readjust the error to represent the distance from the top of
the new pixel – which is done by subtracting one from error.

Pseudocode
plotLine(x0,y0, x1,y1)
dx = x1 - x0
dy = y1 - y0
D = 2*dy - dx
y = y0

for x from x0 to x1
plot(x,y)
if D > 0
y=y+1
D = D - 2*dx
end if
D = D + 2*dy
Code:
#include<iostream.h>
#include<graphics.h>

void drawline(int x0, int y0, int x1, int y1)


{
int dx, dy, p, x, y;

dx=x1-x0;
dy=y1-y0;

x=x0;
y=y0;

p=2*dy-dx;

while(x<x1)
{
if(p>=0)
{
putpixel(x,y,7);
y=y+1;
p=p+2*dy-2*dx;
}
else
{
putpixel(x,y,7);
p=p+2*dy;
}
x=x+1;
}
}

int main()
{
int gdriver=DETECT, gmode, error, x0, y0, x1, y1;
initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");

cout<<"Enter co-ordinates of first point: ";


cin>>x0>>y0;

cout<<"Enter co-ordinates of second point: ";


cin>>x1>>y1;
drawline(x0, y0, x1, y1);

return 0;}
Output:
Discussion:
Advantages of Bresenham Algorithm:
1.It is fast incremental algorithm.
2.It requires only integercalculation.
3.It does’nt have rounding and floating point operation.
4.It provides high speed.

Disadvantages of Bresenham algorithm:


The disadvantage of such a simple algorithm is that it is meant for basic
line drawing. The “advanced ” topic of antialising isn’t part of
Bresenham algorithm , so to draw smooth lines you’d want to look into a
different algorithm.
Findings and learnings :
1.With this experiment we got to learn about Bresenham line generation
algorithm.
2.AC program was implemented which generated the line as expected.
3.We also got to know about the advantages and disadvantages of this
algorithm.

Potrebbero piacerti anche