-
Notifications
You must be signed in to change notification settings - Fork 1
/
BresenhamLineFourQuadrant.m
105 lines (99 loc) · 3.15 KB
/
BresenhamLineFourQuadrant.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%% Bresenham's Line For Four Quadrant %%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Date : 25 January 2022
% Author : Abdurrahman Yilmaz
% Version : v01
% Info : Bresenham's line algorithm is a line drawing algorithm for
% bitmap images. The occupancy grid maps in ROS can be considered as
% bitmap images since they include free, occupied and unknown cells. This
% code is used to determine which occupied cell on the map a laser scanner
% beam hit.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Special note: This code is written based on Bresenham-s-line-algorithm-
% for-all-quadrants by ashiagarwal73
% https://github.com/ashiagarwal73/Bresenham-s-line-algorithm-for-all-quadrants
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [LineElements] = BresenhamLineFourQuadrant(start_xy,end_xy)
x1 = start_xy(1,1);
y1 = start_xy(1,2);
x2 = end_xy(1,1);
y2 = end_xy(1,2);
LineElements = [];
dx = x2 - x1;
dy = y2 - y1;
if dy <= dx && dy > 0
dx = abs(dx);
dy = abs(dy);
Po = (2*dy) - dx;
LineElements = [LineElements;x1,y1];
xk = x1;
yk = y1;
for k = x1:1:x2
if(Po<0)
xk = xk + 1;
Po=Po+(2*dy);
else
xk = xk + 1;
yk = yk + 1;
Po = Po + 2*dy - 2*dx;
end
LineElements = [LineElements;xk,yk];
end
elseif dy > dx && dy > 0
dx = abs(dx);
dy = abs(dy);
Po = (2*dx) - dy;
LineElements = [LineElements;x1,y1];
xk = x1;
yk = y1;
for k = y1:1:y2
if(Po<0)
yk = yk + 1;
Po=Po+(2*dx);
else
xk = xk + 1;
yk = yk + 1;
Po = Po + 2*dx - 2*dy;
end
LineElements = [LineElements;xk,yk];
end
elseif dy >= -dx
dx = abs(dx);
dy = abs(dy);
Po = (2*dy) - dx;
LineElements = [LineElements;x1,y1];
xk = x1;
yk = y1;
for k = x1:1:x2
if(Po<0)
xk = xk + 1;
Po=Po+(2*dy);
else
xk = xk + 1;
yk = yk - 1;
Po = Po + 2*dy - 2*dx;
end
LineElements = [LineElements;xk,yk];
end
elseif dy < -dx
dx = abs(dx);
dy = abs(dy);
Po = (2*dy) - dx;
LineElements = [LineElements;x1,y1];
xk = x1;
yk = y1;
for k = y1:-1:y2
if(Po<0)
yk = yk - 1;
Po=Po+(2*dx);
else
xk = xk + 1;
yk = yk - 1;
Po = Po + 2*dx - 2*dy;
end
LineElements = [LineElements;xk,yk];
end
end
LineElements = LineElements(1:end-1,:);
end