I have a matrix m = zeros(1000, 1000). Within this matrix I want to draw an estimate of the line which passes through 2 points from my matrix. Let’s say x = [122 455]; and y = [500 500];.
How can I do this in Matlab? Are there any predefined functions to do this? I am using Matlab 2012b.
I’ll denote the two endpoints as
p1andp2because I’m planning to usexandyfor something else. I’m also assuming that the first coordinate ofp1andp2is x and the second is y. So here’s a rather simple way to do it:Obtain the equation of the line y = ax + b. In MATLAB, this can be done by:
Convert the values of
xandyto indices of elements in the matrix, and set those elements to 1.Example
Here’s an example for a small 10-by-10 matrix:
The result is:
Update: I have patched this algorithm to work when dy > dx by treating the dimension with the largest displacement as if it were the x-dimension, and then transposing the result if necessary.