x1= input('Enter the initial X-coordinate'); %initial X-coordinate
y1= input('Enter the initial Y-coordinate'); %initial Y-coordinate
x2= input('Enter the final X-coordinate'); %Final X-coordinate
y2= input('Enter the final Y-coordinate'); %Final Y-coordinate
dx=abs(x2-x1); %difference in x coordinate
dy=abs(y2-y1); %difference in y coordinate
m=dy/dx;
i=1;
if m<1
p=(2*dy)-dx;
twody=2*dy;
twodydx=2*(dy-dx);
if x1>x2
x=x2;
y=y2;
xEnd=x1;
else
x=x1;
y=y1;
xEnd=x2;
end
A(1,1)=x;
A(1,2)=y;
A(1,3)=p;
while (x<xEnd)
i=i+1;
x=x+1;
if p<0
p=p+twody;
else
y=y+1;
p=p+twodydx;
end
A(i,1)=x;
A(i,2)=y;
A(i,3)=p;
end
else
p=(2*dy)-dx;
twody=2*dy;
twodydx=2*(dy-dx);
if y1>y2
x=x2;
y=y2;
yEnd=y1;
else
x=x1;
y=y1;
yEnd=y2;
end
A(1,1)=x;
A(1,2)=y;
A(1,3)=p;
while (y<yEnd)
i=i+1;
y=y+1;
if p<0
p=p+twodx;
else
x=x+1;
p=p+twodydx;
end
A(1,1)=x;
A(1,2)=y;
A(1,3)=p;
end
end
plot(A(:,1),A(:,2))
title('Bresenhams Line Drawing Algorithm')
legend('Bresenham Line')
xlabel('X-Axis')
ylabel('Y-Axis')
Cite As
Jash (2024). BBresenham (https://www.mathworks.com/matlabcentral/fileexchange/155397-bbresenham), MATLAB Central File Exchange. Retrieved .
MATLAB Release Compatibility
Created with
R2023b
Compatible with any release
Platform Compatibility
Windows macOS LinuxTags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Discover Live Editor
Create scripts with code, output, and formatted text in a single executable document.
Version | Published | Release Notes | |
---|---|---|---|
1.0.0 |