Skip to content

Instantly share code, notes, and snippets.

@Fred-Barclay
Created June 25, 2017 20:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fred-Barclay/0c4cfbed20bbe81cf2d0a059f1221ea9 to your computer and use it in GitHub Desktop.
Save Fred-Barclay/0c4cfbed20bbe81cf2d0a059f1221ea9 to your computer and use it in GitHub Desktop.
function [ distance ] = GetDistanceTesting( subPT, vertPT, deg_Increment )
%UNTITLED2 Summary of this function goes here
% Detailed explanation goes here
tic
theta=(0:deg_Increment:(360-deg_Increment));
%Sets the value of theta from 0 degrees to the largest angle that is not
%coterminal with 0 degrees and <360 degrees.
numVert=(numel(vertPT))/2;
m_rays=zeros(1,length(theta));
b_rays=zeros(1,length(theta));
for a=1:length(theta)
m_rays(a)=tand(theta(a));
b_rays(a)=subPT(1,2)-m_rays(a)*(subPT(1,1));
end
vertPT_2=[vertPT;vertPT(1,:)];
for b=1:(numel(vertPT_2)/2)-1
m_line(b)=(vertPT_2(b+1,2)-vertPT_2(b,2))/(vertPT_2(b+1,1)-vertPT_2(b,1));
b_line(b)=vertPT_2(b+1,2)-(m_line(b)*vertPT_2(b+1,1));
end
for c=1:length(theta)
clear angle
clear x_Int y_Int
for d=1:(numel(vertPT_2)/2)-1
% If slope of ray is vertical (undefined, and therefore Inf)
if abs(m_rays(c))==Inf && abs(m_rays(c))~=abs(m_line(d))
x_Int(d)= subPT(1,1);
y_Int(d)=(m_line(d)*x_Int(d))+b_line(d);
angle(d) = atan2d(y_Int(d)-subPT(1,2),x_Int(d)-subPT(1,1));
% If slope of line is vertical (undefined, and therefore Inf)
elseif abs(m_line(d))==Inf && abs(m_rays(c))~=abs(m_line(d))
x_Int(d)=vertPT(d,1);
y_Int(d)=(m_rays(c)*x_Int(d))+b_rays(c);
angle(d) = atan2d(y_Int(d)-subPT(1,2),x_Int(d)-subPT(1,1));
% If ray and line have equal slope (parallel)
elseif abs(m_rays(c))==abs(m_line(d))
x_Int(d)=NaN;
y_Int(d)=NaN;
angle(d) = NaN;
% Otherwise, use the normal calculation method
else
x_Int(d)=(b_rays(c)-b_line(d))/(m_line(d)-m_rays(c));
y_Int(d)=(m_line(d)*x_Int(d))+b_line(d);
angle(d) = atan2d(y_Int(d)-subPT(1,2),x_Int(d)-subPT(1,1));
end
end
angle = round(angle) + (round(angle)<0)*360;
correct_angle=find(round(angle)==theta(c));
testdistances = sqrt((x_Int(correct_angle)-repmat(subPT(1),1,length(correct_angle))).^2 + (y_Int(correct_angle)-repmat(subPT(2),1,length(correct_angle))).^2);
distance(c) = min(testdistances);
plotpoint(c)=find(testdistances==min(testdistances));
x_Int_plot(c)=x_Int(correct_angle(plotpoint(c)));
y_Int_plot(c)=y_Int(correct_angle(plotpoint(c)));
hold on
plot(x_Int_plot,y_Int_plot,'g*')
end
xlswrite('Distance',distance)
open Distance.xls
hold on
plot(vertPT_2(:,1),vertPT_2(:,2))
plot(subPT(1),subPT(2),'b+')
axis([min(vertPT(:,1))-3 max(vertPT(:,1))+3 min(vertPT(:,2))-3 max(vertPT(:,2))+3])
axis square
toc
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment