The code is as follows:
function [x, y] = cooline (x1, x2, y1, y2)
%cooline integer-coordinate line drawing algorithm.
% [X, Y] = Cooline (X1, X2, Y1, Y2) computes an
% approximation to the line segment joining (X1, Y1) and
% (X2, Y2) with integer coordinates. X1, X2, Y1, and Y2
DX = ABS (X2-X1);
DY = ABS (Y2-Y1);
% Check for degenerate case.
if (dx = = 0) && (dy = = 0))
x = x1;
y = y1;
Return
End
Flip = 0;
if (dx >= dy)
if (X1 > x2)
% always "draw" from left to right.
t = x1; x1 = x2; x2 = t;
t = y1; y1 = y2; y2 = t;
Flip = 1;
End
m = (y2-y1)/(X2-X1);
x = (x1:x2). ';
y = round (y1 + m* (x-x1));
Else
if (y1 > y2)
% always "draw" from bottom to top.
t = x1; x1 = x2; x2 = t;
t = y1; y1 = y2; y2 = t;
Flip = 1;
End
m = (x2-x1)/(Y2-Y1);
y = (y1:y2). ';
x = Round (x1 + m* (y-y1));
End
if (flip)
x = Flipud (x);
y = flipud (y);
End
End
[Matlab] Gets a method for a point set on a line between a given two points