0% found this document useful (0 votes)
190 views8 pages

Bresenham's Line Generation Algorithm

The document contains algorithms for drawing lines, circles, ellipses and other shapes using a digital graphics approach. It includes Bresenham's line algorithm, the digital differential analyzer (DDA) line algorithm, Bresenham's circle algorithm, the midpoint circle algorithm, and an ellipse generation algorithm using the midpoint method. It also includes a slope-based line drawing algorithm for comparison. Each algorithm is presented with description and MATLAB code implementation.

Uploaded by

DESI COMICS
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
190 views8 pages

Bresenham's Line Generation Algorithm

The document contains algorithms for drawing lines, circles, ellipses and other shapes using a digital graphics approach. It includes Bresenham's line algorithm, the digital differential analyzer (DDA) line algorithm, Bresenham's circle algorithm, the midpoint circle algorithm, and an ellipse generation algorithm using the midpoint method. It also includes a slope-based line drawing algorithm for comparison. Each algorithm is presented with description and MATLAB code implementation.

Uploaded by

DESI COMICS
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 8

BRESENHAMS LINE GENERATION ALGORITHM

function bresenham_line()
clc
clear all
point = input('Give coordinates[ x0 y0 x1 y1]: ');

if (abs(point(4)-point(2)) > abs(point(3)-point(1))) % m>1


x0 = point(2);y0 = point(1); x1 = point(4);y1=point(3);
token =1;

else
x0 = point(1);y0 = point(2); x1 = point(3);y1=point(4); % m<1
token = 0;
end

if(x0 >x1)
temp1 = x0; x0 = x1; x1 = temp1; % m>1
temp2 = y0; y0 = y1; y1 = temp2;
end

dx = abs(x1 - x0) ;
dy = abs(y1 - y0);
sx = sign(x1 - x0);
sy = sign(y1 - y0);

clf, subplot(2,1,1) ,hold on , grid on ,axis([x0-1 x1+1 y0-1 y1+1]);


title('Bresenham Line Generation Algorithm: Point form')

x = x0; y = y0; % Initialization of


line
p0 = 2*dy - dx ; % Initial value of
decision variable

for i = 0:dx-1 % FOR loop to travel


along X
x_coord(i+1) = x; % Saving in matrix form
for plot
y_coord(i+1) = y;
if (token ==0) % Plotting in point
form
plot(x,y,'r*'); % For steep line
coordinate is again
else % converted for actual
line drawing.
plot(y,x,'r*');
end

p0 = p0 + 2*dy; % next value of decision variable

if (p0 >0) % if parameter value is


exceeded
y = y +1*sy; % then y coordinate is
increased
p0 = p0 - 2*(dx ); % and parameter value is
decreased

end
x = x + 1*sx; % X-coordinate is
increased for next point
end
subplot(2,1,2) % Plotting in line
fragment ‘form
if (token ==0)
plot(x_coord,y_coord,'r-','LineWidth',2);
else
plot(y_coord,x_coord,'r-','LineWidth',2);
end
grid on
axis([x0-1 x1+1 y0-1 y1+1]);
title('Bresenham Line Generation Algorithm: Line fragment form')

DDA LINE GENERATION ALGORITHM

function draw(x1,y1)
clc
clear
point = input('Give coordinates[ x0 y0 x1 y1]: ');
x0 = point(1);y0 = point(2); x1 = point(3);y1=point(4);
dx = abs(x1-x0);
dy = abs(y1-y0);
sx = sign(x1-x0);
sy = sign(y1-y0);
if (dy > dx)
step = dy;
else
step = dx;
end
x(1) = x0; y(1) = y0; j = 1;
for i= 0:1:step
if (x1 == x)&(y1 == y)
break;
end
j = j+1;
x(j) = x(j-1) + (dx/step)*sx;
y(j) = y(j-1) + (dy/step)*sy;
end
plot(round(x),round(y),'rs');
grid on ,hold on
plot(x,y,'b*');
title('Digital Differential Line Drawing Algorithm')
MID POINT CIRCLE GENERATION ALGORITHM

octant_size = floor((sqrt(2)*(radius-1)+4)/2);
n_points = 8 * octant_size;

% Iterate a second time, and this time retrieve coordinates.


% We "zig-zag" through indices, so that we reconstruct a continuous
% set of of x,y coordinates, starting from the top of the circle.
xc = NaN(n_points, 1);
yc = NaN(n_points, 1);

x = 0;
y = radius;
f = 1 - radius;
dx = 1;
dy = - 2 * radius;

% Store

% 1 octant
xc(1) = x0 + x;
yc(1) = y0 + y;

% 2nd octant
xc(8 * octant_size) = x0 - x;
yc(8 * octant_size) = y0 + y;

% 3rd octant
xc(4 * octant_size) = x0 + x;
yc(4 * octant_size) = y0 - y;

% 4th octant
xc(4 * octant_size + 1) = x0 - x;
yc(4 * octant_size + 1) = y0 - y;

% 5th octant
xc(2 * octant_size) = x0 + y;
yc(2 * octant_size) = y0 + x;

% 6th octant
xc(6 * octant_size + 1) = x0 - y;
yc(6 * octant_size + 1) = y0 + x;

% 7th octant
xc(2 * octant_size + 1) = x0 + y;
yc(2 * octant_size + 1) = y0 - x;

% 8th octant
xc(6 * octant_size) = x0 - y;
yc(6 * octant_size) = y0 - x;
for i = 2 : n_points/8

% We update x & y
if f > 0
y = y - 1;
dy = dy + 2;
f = f + dy;
end
x = x + 1;
dx = dx + 2;
f = f + dx;

% 1 octant
xc(i) = x0 + x;
yc(i) = y0 + y;

% 2nd octant
xc(8 * octant_size - i + 1) = x0 - x;
yc(8 * octant_size - i + 1) = y0 + y;

% 3rd octant
xc(4 * octant_size - i + 1) = x0 + x;
yc(4 * octant_size - i + 1) = y0 - y;

% 4th octant
xc(4 * octant_size + i) = x0 - x;
yc(4 * octant_size + i) = y0 - y;

% 5th octant
xc(2 * octant_size - i + 1) = x0 + y;
yc(2 * octant_size - i + 1) = y0 + x;

% 6th octant
xc(6 * octant_size + i) = x0 - y;
yc(6 * octant_size + i) = y0 + x;

% 7th octant
xc(2 * octant_size + i) = x0 + y;
yc(2 * octant_size + i) = y0 - x;

% 8th octant
xc(6 * octant_size - i + 1) = x0 - y;
yc(6 * octant_size - i + 1) = y0 - x;

end

BRESENHAMS CIRCLE GENERATION ALGORITHM

function bresenham_circle(a,b,r)
clc, clear all, clf,close
a = input('Give the centre X coord : '); % X coord of centre
b = input('Give the centre Y coord : '); % Y coord of centre
r = input('Give the radius of circle : '); % Radius of the circle to be drawn
inc = input('Give the increment step for coordinate[Default:1]: '); % Use the
value
if isempty(inc) % of inc greater than 1
inc =1; % usually less than radius
End

x = 0;
y = r;
d= 3-2*r;
while(x < y)
funplot(a,b,x,y,r);

if d>=0
d = d +4*(x-y)+10;
x = x + 1/inc;
y = y - 1/inc;

else
d = d + 4*x +6;
x = x+1/inc;
end

End

function funplot(a,b,p,q,r)
for i= -1:2:1
for j= -1:2:1
plot(a+i*p,b+j*q,'r.');
plot(a+i*q,b+j*p,'r.');
grid off
hold on
end
end
title('Bresenham circle')
axis([a-r-1 a+r+1 b-r-1 b+r+1])
axis equal;

ELLIPSE GENERATION ALGORITHM

function ellipse()
clc,clear all
c = input('Give the centre coord[x,y]: '); %INTERACTIVE INPUT OF
CENTRE AND RADIUS
a = input('Give the value of major/minor axis[a,b]: ');
x = 0;
y = a(2);
p1= a(2)*a(2) - a(1)*a(1)*a(2) + 0.25*a(1)*a(1);
p2 = a(2)*a(2)*(x + 0.5)*(x+0.5) + a(1)*a(1)*(y-1)*(y-1) -
a(1)*a(1)*a(2)*a(2);
while (a(2)*a(2)*x) < (a(1)*a(1)*y)
for i= -1:2:1
for j= -1:2:1
plot(c(1)+i*x,c(2)+j*y,'r.');
hold on
end
end
if(p1<0)
p1 = p1 + 2*a(2)*a(2)*x + 3*a(2)*a(2);
x = x+1;
else
p1 = p1 - 2*a(1)*a(1)*y + 2*a(2)*a(2)*x + 2*a(1)*a(1) +
3*a(2)*a(2);
x = x + 1;
y = y - 1;
end
end
while y > 0
for i= -1:2:1
for j= -1:2:1
plot(c(1)+i*x,c(2)+j*y,'r.');
end
end
if(p2 < 0)
p2 = p2 + 2*a(2)*a(2)*x - 2*a(1)*a(1)*y + 3*a(1)*a(1) +
2*a(2)*a(2);
x = x + 1;
y = y - 1;
else
p1 = p1 - 2*a(1)*a(1)*y + 3*a(1)*a(1);
y = y - 1;
end
end
grid on , axis square
axis([c(1)-a(1)-5 c(1)+a(1)+5 c(2)-a(2)-5 c(2)+a(2)+5]);
title('Ellipse generation by Midpoint generation algorithm')
1. SLOPE ALGORITHM

function draw(x,y)
clc
clear
point = input('Give coord[ x1 y1 x2 y2]: ');
x1 = point(1);y1 = point(2); x2 =
point(3);y2=point(4);
dx = x2-x1;
step=abs(dx);
dy = y2-y1;
m=(dy/dx);
c=y1-(m*x1);
if(x2>x1)
x=x1;xend=x2;
else
x=x2;xend=x1;
end
for i=0:1:step
y=(m*x)+c;
plot(x,y,'rs');
hold on
grid on
plot(round(x),round(y),'b*');
x=x+1;
end
end

You might also like