-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrawLineXY.m
28 lines (24 loc) · 849 Bytes
/
drawLineXY.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
function [indexes pic] = drawLineXY(picture, x1, y1, x2, y2, color)
if ~ exist('color','var') || isempty(color)
display "Color undefined, then I'll pick 'black':";
color = 0;
end
% distances according to both axes
xn = abs(x2-x1);
yn = abs(y2-y1);
% interpolate against axis with greater distance between points;
% this guarantees statement in the under the first point!
if (xn > yn)
xc = x1 : sign(x2-x1) : x2;
yc = round( interp1([x1 x2], [y1 y2], xc, 'linear') );
else
yc = y1 : sign(y2-y1) : y2;
xc = round( interp1([y1 y2], [x1 x2], yc, 'linear') );
end
% 2-D indexes of line are saved in (xc, yc), and
% 1-D indexes are calculated here:
indexes = sub2ind( size(picture), yc, xc );
% draw line on the image (change value of '255' to one that you need)
picture(indexes) = color;
pic = picture;
imshow(picture);