forked from uw-loci/curvelets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetSegPixels.m
58 lines (46 loc) · 1.33 KB
/
GetSegPixels.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
function [ seg_pts abs_ang ] = GetSegPixels( pt1, pt2 )
%GetSegPixels - Get all the pixels between two points on a cartesian grid
% Step pixel by pixel and collect each point
% pt1 and pt2 are [row, col]
seg_pts = pt1;
abs_ang = NaN;
pt1 = round(pt1);
pt2 = round(pt2);
%get slope
rise = pt2(1) - pt1(1);
run = pt2(2) - pt1(2);
%check if points are same
maxrr = max(abs(rise),abs(run));
if (maxrr == 0)
return;
end
%return slope
abs_ang = atan(rise/run); %range -pi to pi
%walk this distance each iteration (sub pixel!)
frac_rise = 0.25*rise/maxrr; %rise/maxrr is <= 1.0 and is in pixels
frac_run = 0.25*run/maxrr;
spt = pt1;
y = spt(1);
x = spt(2);
i = 2; %index into output array
%initialize output (this will grow in memory, but seg should be short)
seg_pts = spt;
while (1)
if (spt(1) == pt2(1) && spt(2) == pt2(2))
break;
end
%fractional accumulators
y = y + frac_rise;
x = x + frac_run;
%round to pixel values
ry = round(y);
rx = round(x);
if (ry ~= spt(1) || rx ~= spt(2))
spt = [ry rx];
seg_pts(i,:) = spt;
seg_pts(i+1,:) = [spt(1)+1 spt(2)]; %fill in a little more space (avoid missed intersections on diagonals)
seg_pts(i+2,:) = [spt(1)-1 spt(2)]; %fill in a little more space (avoid missed intersections on diagonals)
i = i+3;
end
end
end