%% Get Grid Cells Along A Ray
% Use the raycast method to generate cell indices for all cells traversed by a ray.
% Create an empty map. A low resolution map is used to illustrate the affect of grid locations.

map = robotics.OccupancyGrid(10,10,1);
show(map)

%
% Get the grid indices of the midpoints and end points of a ray from p1 to p2. Set occupancy values for these grid indices. Midpoints are treated as open space. Endpoints are updated with an occupied observation.

p1 = [2 3];
p2 = [8.5 8];
[endPts,midPts] = raycast(map,p1,p2);
setOccupancy(map,midPts,zeros(length(midPts),1),'grid');
setOccupancy(map,endPts,ones(length(endPts),1),'grid');

%
% Plot the original ray over the map. Notice that each grid cell touched by the line is updated. The starting point overlaps multiple cells and the line touches the edge of certain cells, but all the cells are still updated.

show(map)
hold on
plot([p1(1) p2(1)],[p1(2) p2(2)],'-b','LineWidth',2)
plot(p2(1),p2(2),'or')
grid on

%
% Copyright 2012 The MathWorks, Inc.