Hi Roddy
1.- the map
clear all;close all;clc
S1=30;S2=30;
emap = true(S1,S2);
emap = padarray(emap,[1 1],false,'both');
emap(1:15,16) = false;
2.-
obstacle definition
K=[16*ones(15,1) [1:15]'];
3.-
the spotter
nx_spoint=9;
ny_spoint=6;
spoint=[nx_spoint,ny_spoint];
figure(1);imshow(emap);ax1=gca
4.- map points free of obstacles
[nx,ny]=find(emap==true);
5.- checking the start area free of obstacles has the correct points
A=zeros(size(emap));
hf2=figure(2);imshow(A);ax2=hf2.CurrentAxes
plot(ax2,ny,nx,'r*');
axis(ax2,'equal');
6.- spotter on the map
hold(ax1,'all');plot(ax1,spoint(1),spoint(2),'g.');
hold(ax2,'all');plot(ax2,spoint(1),spoint(2),'g*');
P=[nx ny];
7.-
remove spotter point from area to sweep
L1=[];
for k=1:1:size(P,1)
if P(k,1)==nx_spoint && P(k,2)==ny_spoint
L1=[L1 k];
end
end
P(L1,:)=[];
8.-
defining outer perimeter: the fence along which the spotter is going to sweep along
left_side=[ones(1,S1)' [1:1:S1]']
top_side=[[2:1:S2-1]' S1*ones(1,S2-2)']
right_side=[S2*ones(1,S1)' [S1:-1:1]']
bottom_side=[[S2-1:-1:2]' ones(1,S2-2)']
Fence=[top_side;right_side;bottom_side;left_side];
9.-
check obstacle points correctly on map
plot(ax2,K(:,1),K(:,2),'c*')
for k2=1:1:size(Fence,1)
P0=Fence(k2,:);
nxP0=P0(1);nyP0=P0(2);
plot(ax2,nxP0,nyP0,'b*');
10.-
define one ray points, spotter - fence
Lx=floor(linspace(nx_spoint,nxP0,max(abs(nxP0-nx_spoint),abs(nyP0-ny_spoint))));
Ly=floor(linspace(ny_spoint,nyP0,max(abs(nxP0-nx_spoint),abs(nyP0-ny_spoint))));
L1=[Lx' Ly'];
11.-
intersect finds, if any, intersect points for 2D vectors, intersect.m attached along with this script.
[U,n1,n2]=intersectN(L1,K)
12.-
have to decide whether truncate ray or not
if isempty(U)
plot(ax2,Lx,Ly,'y.');drawnow;
else
if size(U,2)>1
U=U(1,:);
end
13.-
Following bank of ifs to make sure rays cover from all 4 quadrants
if spoint(1)<U(1)
Lx2obst_=[spoint(1):1:U(1)]';
end
if spoint(1)>U(1)
Lx2obst_=[spoint(1):-1:U(1)]';
end
if spoint(1)==U(1)
Lx2obst=spoint(1);
end
if spoint(2)<U(2)
Ly2obst_=[spoint(2):1:U(2)]';
end
if spoint(2)>U(2)
Ly2obst_=[spoint(2):-1:U(2)]';
end
if spoint(2)==U(2)
Ly2obst=spoint(2);
end
Lx2obst=floor(linspace(spoint(1),U(1),max(numel(Lx2obst_),numel(Ly2obst_))))
Ly2obst=floor(linspace(spoint(2),U(2),max(numel(Lx2obst_),numel(Ly2obst_))))
plot(ax2,Lx2obst',Ly2obst','y.')
end
end
.
.
.
Note that the plot function shows the image reversed compared to the imshow of logical 2D maps that you start with, but the coordinates are all the same.
Roddy
f you find this answer useful would you please be so kind to consider marking my answer as Accepted Answer?
To any other reader, if you find this answer useful please consider clicking on the thumbs-up vote link
thanks in advance for time and attention
John BG