-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbresenham.m
54 lines (48 loc) · 1.32 KB
/
bresenham.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
%% Function description: Bresenham line algorithm (https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm)
%===============================================================================
% INPUT:
% @start_pnt start point of the line
% @end_pnt end point of the line
% OUTPUT:
% @l points on line
% DATE: 2018/12/23 wyq
%===============================================================================
function l = bresenham(start_pnt, end_pnt)
x0 = start_pnt(1);
x1 = end_pnt(1);
y0 = start_pnt(2);
y1 = end_pnt(2);
dx = x1-x0;
dy = y1-y0;
if abs(dx) >= abs(dy)
da = abs(dx);
db = abs(dy);
err = abs(db)/2;
dira = sign(dx);
dirb = sign(dy);
else
da = abs(dy);
db = abs(dx);
err = abs(db)/2;
dira = sign(dx);
dirb = sign(dy);
end
l = zeros(2,3000);
j = 0;
cnt = 1;
for i = 0:da-1
err = err + db;
if err >= da
j = j + 1;
err = err - da;
end
if abs(dx) >= abs(dy)
l(:, cnt) = start_pnt + [dira*i; dirb*j];
cnt = cnt + 1;
else
l(:, cnt) = start_pnt + [dira*j; dirb*i];
cnt = cnt + 1;
end
end
l(:, cnt:end) = [];
end