Mxed

Version 1.0.0 (1.23 KB) by Shaman
Mxed
0 Downloads
Updated 23 Nov 2023

View License

clc
clear all;
% Line generating algorithm (Bresenham's)
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 + twody;
else
x = x + 1;
p = p + twodydx;
end
A(i, 1) = x;
A(i, 2) = y;
A(i, 3) = p;
end
end
% Translation
tx = input('Enter X-axis Translation ');
ty = input('Enter Y-axis Translation ');
translation = [1 0 tx; 0 1 ty; 0 0 1];
A_translated = A * translation;
% Plotting
plot(A(:, 1), A(:, 2), 'o-', 'DisplayName', 'Initial Line');
hold on
plot(A_translated(:, 1), A_translated(:, 2), 's-', 'DisplayName', 'Translated Line');
title('Line Translation')
legend('Location', 'Best')
xlabel('X-Axis')
ylabel('Y-Axis')
grid on

Cite As

Shaman (2026). Mxed (https://www.mathworks.com/matlabcentral/fileexchange/155442-mxed), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2023b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Tags Add Tags
Version Published Release Notes
1.0.0