Open In App

Plot a line along 2 points in MATLAB

Our objective is to plot a line along 2 points in MATLAB without using inbuilt functions for plotting. A black and white image can be represented as a 2 order matrix. The first order is for the rows and the second order is for the columns, the pixel value will determine the color of the pixel based on the grayscale color format.

Approach :



Implementation:




% MATLAB code to plot line through 2 points
  
% create a white image of size 400X400
im = uint8(zeros(400, 400)) + 255;
  
% coordinates of point 1 
x1 = 100;
y1 = 100;
  
% coordinates of point 2
x2 = 200;
y2 = 200;
  
% slope of points 1 and 2
m = (y2-y1)/(x2-x1);
  
% accessing every pixel
for i = 1:400
    for j = 1:400
        m2 = (y2-j)/(x2-i);
        if m == m2
            im(i, j) = 0;
        end
    end 
end
  
% display the image
imshow(im);

Output :



Article Tags :