SlideShare a Scribd company logo
ALGORITHM TO USED ROUTING USING MATLAB
Our online Tutors are available 24*7 to provide Help with Algorithm to Used Routing
Homework/Assignment or a long term Graduate/Undergraduate Algorithm to Used Routing
Project. Our Tutors being experienced and proficient in Algorithm to Used Routing ensure to
provide high quality Algorithm to Used Routing Homework Help. Upload your Algorithm to Used
Routing Assignment at ‘Submit Your Assignment’ button or email it
to info@assignmentpedia.com. You can use our ‘Live Chat’ option to schedule an Online
Tutoring session with our Algorithm to Used Routing Tutors.
MINIMAL NEAREST-NEIGHBOR
This sample assignments shows how to Connect Randomly Ordered 2D Points into a Minimal
Nearest-Neighbor Closed Contour Connects randomly ordered 2D points into a minimal nearest
neighbor contour.
[Xout,Yout,varargout]=points2contour(Xin,Yin,P,direction,varargin)
%points2contour
%Tristan Ursell
%July 2013
%
%[Xout,Yout]=points2contour(Xin,Yin,P,direction)
%[Xout,Yout]=points2contour(Xin,Yin,P,direction,dlim)
%[Xout,Yout,orphans]=points2contour(Xin,Yin,P,direction,dlim)
%[Xout,Yout,orphans,indout]=points2contour(Xin,Yin,P,direction,dlim)
%
%Given any list of 2D points (Xin,Yin), construct a singly connected
%nearest-neighbor path in either the 'cw' or 'ccw' directions. The code
%has been written to handle square and hexagon grid points, as well as any
%non-grid arrangement of points.
%
%'P' sets the point to begin looking for the contour from the original
%ordering of (Xin,Yin), and 'direction' sets the direction of the contour,
%with options 'cw' and 'ccw', specifying clockwise and counter-clockwise,
%respectively.
%
%The optional input parameter 'dlim' sets a distance limit, if the distance
%between a point and all other points is greater than or equal to 'dlim',
%the point is left out of the contour.
%
%The optional output 'orphans' gives the indices of the original (Xin,Yin)
%points that were not included in the contour.
%
%The optional output 'indout' is the order of indices that produces
%Xin(indout)=Xout and Yin(indout)=Yout.
%
%There are many (Inf) situations where there is no unique mapping of points
%into a connected contour -- e.g. any time there are more than 2 nearest
%neighbor points, or in situations where the nearest neighbor matrix is
%non-symmetric. Picking a different P will result in a different contour.
%Likewise, in cases where one point is far from its neighbors, it may be
%orphaned, and only connected into the path at the end, giving strange
%results.
%
%The input points can be of any numerical class.
%
%Note that this will *not* necessarily form the shortest path between all
%the points -- that is the NP-Hard Traveling Salesman Problem, for which
%there is no deterministic solution. This will, however, find the shortest
%path for points with a symmetric nearest neighbor matrix.
%
%see also: bwtraceboundary
%
%Example 1: continuous points
%N=200;
%P=1;
%theta=linspace(0,2*pi*(1-1/N),N);
%[~,I]=sort(rand(1,N));
%R=2+sin(5*theta(I))/3;
%
%Xin=R.*cos(theta(I));
%Yin=R.*sin(theta(I));
%
%[Xout,Yout]=points2contour(Xin,Yin,P,'cw');
%
%figure;
%hold on
%plot(Xin,Yin,'b-')
%plot(Xout,Yout,'r-','Linewidth',2)
%plot(Xout(2:end-1),Yout(2:end-1),'k.','Markersize',15)
%plot(Xout(1),Yout(1),'g.','Markersize',15)
%plot(Xout(end),Yout(end),'r.','Markersize',15)
%xlabel('X')
%ylabel('Y')
%axis equal tight
%title(['Black = original points, Blue = original ordering, Red = new ordering, Green = starting
points'])
%box on
%
%
%Example 2: square grid
%P=1;
%
%Xin=[1,2,3,4,4,4,4,3,2,1,1,1];
%Yin=[0,0,0,0,1,2,3,3,2,2,1,0];
%
%[Xout,Yout]=points2contour(Xin,Yin,P,'cw');
%
%figure;
%hold on
%plot(Xin,Yin,'b-')
%plot(Xout,Yout,'r-','Linewidth',2)
%plot(Xout(2:end-1),Yout(2:end-1),'k.','Markersize',15)
%plot(Xout(1),Yout(1),'g.','Markersize',15)
%plot(Xout(end),Yout(end),'r.','Markersize',15)
%xlabel('X')
%ylabel('Y')
%axis equal tight
%box on
%
%Example 3: continuous points, pathological case
%N=200;
%P=1;
%theta=linspace(0,2*pi*(1-1/N),N);
%[~,I]=sort(rand(1,N));
%R=2+sin(5*theta(I))/3;
%
%Xin=(1+rand(1,N)/2).*R.*cos(theta(I));
%Yin=(1+rand(1,N)/2).*R.*sin(theta(I));
%
%[Xout,Yout]=points2contour(Xin,Yin,P,'cw');
%
%figure;
%hold on
%plot(Xin,Yin,'b-')
%plot(Xout,Yout,'r-','Linewidth',2)
%plot(Xout(2:end-1),Yout(2:end-1),'k.','Markersize',15)
%plot(Xout(1),Yout(1),'g.','Markersize',15)
%plot(Xout(end),Yout(end),'r.','Markersize',15)
%xlabel('X')
%ylabel('Y')
%axis equal tight
%title(['Black = original points, Blue = original ordering, Red = new ordering, Green = starting
points'])
%box on
%
%Example 4: continuous points, distance limit applied
%N=200;
%P=1;
%theta=linspace(0,2*pi*(1-1/N),N);
%[~,I]=sort(rand(1,N));
%R=2+sin(5*theta(I))/3;
%R(2)=5; %the outlier
%
%Xin=(1+rand(1,N)/16).*R.*cos(theta(I));
%Yin=(1+rand(1,N)/16).*R.*sin(theta(I));
%
%[Xout,Yout,orphans,indout]=points2contour(Xin,Yin,P,'cw',1);
%
%figure;
%hold on
%plot(Xin,Yin,'b-')
%plot(Xin(orphans),Yin(orphans),'kx')
%plot(Xin(indout),Yin(indout),'r-','Linewidth',2)
%plot(Xout(2:end-1),Yout(2:end-1),'k.','Markersize',15)
%plot(Xout(1),Yout(1),'g.','Markersize',15)
%plot(Xout(end),Yout(end),'r.','Markersize',15)
%xlabel('X')
%ylabel('Y')
%axis equal tight
%title(['Black = original points, Blue = original ordering, Red = new ordering, Green = starting
points'])
%box on
%
function [Xout,Yout,varargout]=points2contour(Xin,Yin,P,direction,varargin)
%check to make sure the vectors are the same length
if length(Xin)~=length(Yin)
error('Input vectors must be the same length.')
end
%check to make sure point list is long enough
if length(Xin)<2
error('The point list must have more than two elements.')
end
%check distance limit
if ~isempty(varargin)
dlim=varargin{1};
if dlim<=0
error('The distance limit parameter must be greater than zero.')
end
else
dlim=-1;
end
%check direction input
if and(~strcmp(direction,'cw'),~strcmp(direction,'ccw'))
error(['Direction input: ' direction ' is not valid, must be either "cw" or "ccw".'])
end
%check to make sure P is in the right range
P=round(P);
npts=length(Xin);
if or(P<1,P>npts)
error('The starting point P is out of range.')
end
%adjust input vectors for starting point
if size(Xin,1)==1
Xin=circshift(Xin,[0,1-P]);
Yin=circshift(Yin,[0,1-P]);
else
Xin=circshift(Xin,[1-P,0]);
Yin=circshift(Yin,[1-P,0]);
end
%find distances between all points
D=zeros(npts,npts);
for q1=1:npts
D(q1,:)=sqrt((Xin(q1)-Xin).^2+(Yin(q1)-Yin).^2);
end
%max distance
maxD=max(D(:));
%avoid self-connections
D=D+eye(npts)*maxD;
%apply distance contraint by removing bad points and starting over
if dlim>0
D(D>=dlim)=-1;
%find bad points
bad_pts=sum(D,1)==-npts;
orphans=find(bad_pts);
%check starting point
if sum(orphans==P)>0
error('The starting point index is a distance outlier, choose a new starting point.')
end
%get good points
Xin=Xin(~bad_pts);
Yin=Yin(~bad_pts);
%number of good points
npts=length(Xin);
%find distances between all points
D=zeros(npts,npts);
for q1=1:npts
D(q1,:)=sqrt((Xin(q1)-Xin).^2+(Yin(q1)-Yin).^2);
end
%max distance
maxD=max(D(:));
%avoid self-connections
D=D+eye(npts)*maxD;
else
orphans=[];
end
%tracking vector (has this original index been put into the ordered list?)
track_vec=zeros(1,npts);
%construct directed graph
Xout=zeros(1,npts);
Yout=zeros(1,npts);
indout0=zeros(1,npts);
Xout(1)=Xin(1);
Yout(1)=Yin(1);
indout0(1)=1;
p_now=1;
track_vec(p_now)=1;
for q1=2:npts
%get current row of distance matrix
curr_vec=D(p_now,:);
%remove used points
curr_vec(track_vec==1)=maxD;
%find index of closest non-assigned point
p_temp=find(curr_vec==min(curr_vec),1,'first');
%reassign point
Xout(q1)=Xin(p_temp);
Yout(q1)=Yin(p_temp);
%move index
p_now=p_temp;
%update tracking
track_vec(p_now)=1;
%update index vector
indout0(q1)=p_now;
end
%undo the circshift
temp1=find(~bad_pts);
indout=circshift(temp1(indout0),[P,0]);
%%%%%%% SET CONTOUR DIRECTION %%%%%%%%%%%%
%contour direction is a *global* feature that cannot be determined until
%all the points have been sequentially ordered.
%calculate tangent vectors
tan_vec=zeros(npts,3);
for q1=1:npts
if q1==npts
tan_vec(q1,:)=[Xout(1)-Xout(q1),Yout(1)-Yout(q1),0];
tan_vec(q1,:)=tan_vec(q1,:)/norm(tan_vec(q1,:));
else
tan_vec(q1,:)=[Xout(q1+1)-Xout(q1),Yout(q1+1)-Yout(q1),0];
tan_vec(q1,:)=tan_vec(q1,:)/norm(tan_vec(q1,:));
end
end
%determine direction of contour
local_cross=zeros(1,npts);
for q1=1:npts
if q1==npts
cross1=cross(tan_vec(q1,:),tan_vec(1,:));
else
cross1=cross(tan_vec(q1,:),tan_vec(q1+1,:));
end
local_cross(q1)=asin(cross1(3));
end
%figure out current direction
if sum(local_cross)<0
curr_dir='cw';
else
curr_dir='ccw';
end
%set direction of the contour
if and(strcmp(curr_dir,'cw'),strcmp(direction,'ccw'))
Xout=fliplr(Xout);
Yout=fliplr(Yout);
end
%varargout
if nargout==3
varargout{1}=orphans;
end
if nargout==4
varargout{1}=orphans;
varargout{2}=indout;
end
visit us at www.assignmentpedia.com or email us at info@assignmentpedia.com or call us at +1 520 8371215

More Related Content

Similar to Algorithm To Build A Routing Project (20)

PDF
Matlab assignment
Rutvik
 
PDF
Intro to threp
Hong Wu
 
PDF
Orthogonal Range Searching
Benjamin Sach
 
PPTX
13-closest-pair (2)-algorithmic-programming.pptx
r6s0069
 
PDF
Seminar Report (Final)
Aruneel Das
 
PDF
Project2
?? ?
 
PDF
A simple study on computer algorithms by S. M. Risalat Hasan Chowdhury
S. M. Risalat Hasan Chowdhury
 
PPT
Chap05alg
Munkhchimeg
 
PPT
Chap05alg
Munhchimeg
 
DOCX
CSCI 2033 Elementary Computational Linear Algebra(Spring 20.docx
mydrynan
 
DOCX
Assignmnt 4
Sajid Khokhar
 
PDF
Beating the Spread: Time-Optimal Point Meshing
Don Sheehy
 
PDF
x=input(Enter the first sequence ); l1=input(Enter the lowe.pdf
aquadreammail
 
PDF
kdtrees.pdf
swapnilslide2019
 
PPTX
Oxford 05-oct-2012
Ted Dunning
 
PPT
Miniproject final group 14
Ashish Mundhra
 
PPTX
Computer Graphics
Sneha Chopra
 
PDF
ML_Unit_IV_Clustering in Machine Learning.pdf
rameshwarchintamani
 
DOCX
A 99 LINE TOPOLOGY OPTIMIZATION CODE BY OLE SIGMUND, JANUARY .docx
aryan532920
 
PPTX
POLYNOMIALS,CURVEFITTING, AND INTERPOLATION
Dr. Uday Saikia
 
Matlab assignment
Rutvik
 
Intro to threp
Hong Wu
 
Orthogonal Range Searching
Benjamin Sach
 
13-closest-pair (2)-algorithmic-programming.pptx
r6s0069
 
Seminar Report (Final)
Aruneel Das
 
Project2
?? ?
 
A simple study on computer algorithms by S. M. Risalat Hasan Chowdhury
S. M. Risalat Hasan Chowdhury
 
Chap05alg
Munkhchimeg
 
Chap05alg
Munhchimeg
 
CSCI 2033 Elementary Computational Linear Algebra(Spring 20.docx
mydrynan
 
Assignmnt 4
Sajid Khokhar
 
Beating the Spread: Time-Optimal Point Meshing
Don Sheehy
 
x=input(Enter the first sequence ); l1=input(Enter the lowe.pdf
aquadreammail
 
kdtrees.pdf
swapnilslide2019
 
Oxford 05-oct-2012
Ted Dunning
 
Miniproject final group 14
Ashish Mundhra
 
Computer Graphics
Sneha Chopra
 
ML_Unit_IV_Clustering in Machine Learning.pdf
rameshwarchintamani
 
A 99 LINE TOPOLOGY OPTIMIZATION CODE BY OLE SIGMUND, JANUARY .docx
aryan532920
 
POLYNOMIALS,CURVEFITTING, AND INTERPOLATION
Dr. Uday Saikia
 

More from Assignmentpedia (20)

PDF
Transmitter side components
Assignmentpedia
 
PDF
Single object range detection
Assignmentpedia
 
PDF
Sequential radar tracking
Assignmentpedia
 
PDF
Resolution project
Assignmentpedia
 
PDF
Radar cross section project
Assignmentpedia
 
PDF
Radar application project help
Assignmentpedia
 
PDF
Parallel computing homework help
Assignmentpedia
 
PDF
Network costing analysis
Assignmentpedia
 
PDF
Matlab simulation project
Assignmentpedia
 
PDF
Matlab programming project
Assignmentpedia
 
PDF
Links design
Assignmentpedia
 
PDF
Image processing project using matlab
Assignmentpedia
 
PDF
Help with root locus homework1
Assignmentpedia
 
PDF
Transmitter subsystem
Assignmentpedia
 
PDF
Theory of computation homework help
Assignmentpedia
 
PDF
Econometrics Homework Help
Assignmentpedia
 
PDF
Video Codec
Assignmentpedia
 
PDF
Radar Spectral Analysis
Assignmentpedia
 
PDF
Pi Controller
Assignmentpedia
 
PDF
Help With Digital Communication Project
Assignmentpedia
 
Transmitter side components
Assignmentpedia
 
Single object range detection
Assignmentpedia
 
Sequential radar tracking
Assignmentpedia
 
Resolution project
Assignmentpedia
 
Radar cross section project
Assignmentpedia
 
Radar application project help
Assignmentpedia
 
Parallel computing homework help
Assignmentpedia
 
Network costing analysis
Assignmentpedia
 
Matlab simulation project
Assignmentpedia
 
Matlab programming project
Assignmentpedia
 
Links design
Assignmentpedia
 
Image processing project using matlab
Assignmentpedia
 
Help with root locus homework1
Assignmentpedia
 
Transmitter subsystem
Assignmentpedia
 
Theory of computation homework help
Assignmentpedia
 
Econometrics Homework Help
Assignmentpedia
 
Video Codec
Assignmentpedia
 
Radar Spectral Analysis
Assignmentpedia
 
Pi Controller
Assignmentpedia
 
Help With Digital Communication Project
Assignmentpedia
 
Ad

Recently uploaded (20)

PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PDF
Mahidol_Change_Agent_Note_2025-06-27-29_MUSEF
Tassanee Lerksuthirat
 
PPTX
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
PPTX
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
PDF
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
PPTX
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
PPTX
Quarter 1_PPT_PE & HEALTH 8_WEEK 3-4.pptx
ronajadolpnhs
 
PPTX
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PPT
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
PPTX
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
Mahidol_Change_Agent_Note_2025-06-27-29_MUSEF
Tassanee Lerksuthirat
 
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
Quarter 1_PPT_PE & HEALTH 8_WEEK 3-4.pptx
ronajadolpnhs
 
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
Dimensions of Societal Planning in Commonism
StefanMz
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
Ad

Algorithm To Build A Routing Project

  • 1. ALGORITHM TO USED ROUTING USING MATLAB Our online Tutors are available 24*7 to provide Help with Algorithm to Used Routing Homework/Assignment or a long term Graduate/Undergraduate Algorithm to Used Routing Project. Our Tutors being experienced and proficient in Algorithm to Used Routing ensure to provide high quality Algorithm to Used Routing Homework Help. Upload your Algorithm to Used Routing Assignment at ‘Submit Your Assignment’ button or email it to [email protected]. You can use our ‘Live Chat’ option to schedule an Online Tutoring session with our Algorithm to Used Routing Tutors. MINIMAL NEAREST-NEIGHBOR This sample assignments shows how to Connect Randomly Ordered 2D Points into a Minimal Nearest-Neighbor Closed Contour Connects randomly ordered 2D points into a minimal nearest neighbor contour. [Xout,Yout,varargout]=points2contour(Xin,Yin,P,direction,varargin) %points2contour %Tristan Ursell %July 2013 % %[Xout,Yout]=points2contour(Xin,Yin,P,direction) %[Xout,Yout]=points2contour(Xin,Yin,P,direction,dlim) %[Xout,Yout,orphans]=points2contour(Xin,Yin,P,direction,dlim) %[Xout,Yout,orphans,indout]=points2contour(Xin,Yin,P,direction,dlim) % %Given any list of 2D points (Xin,Yin), construct a singly connected %nearest-neighbor path in either the 'cw' or 'ccw' directions. The code %has been written to handle square and hexagon grid points, as well as any %non-grid arrangement of points. % %'P' sets the point to begin looking for the contour from the original %ordering of (Xin,Yin), and 'direction' sets the direction of the contour, %with options 'cw' and 'ccw', specifying clockwise and counter-clockwise, %respectively. % %The optional input parameter 'dlim' sets a distance limit, if the distance %between a point and all other points is greater than or equal to 'dlim', %the point is left out of the contour. % %The optional output 'orphans' gives the indices of the original (Xin,Yin) %points that were not included in the contour. % %The optional output 'indout' is the order of indices that produces %Xin(indout)=Xout and Yin(indout)=Yout. %
  • 2. %There are many (Inf) situations where there is no unique mapping of points %into a connected contour -- e.g. any time there are more than 2 nearest %neighbor points, or in situations where the nearest neighbor matrix is %non-symmetric. Picking a different P will result in a different contour. %Likewise, in cases where one point is far from its neighbors, it may be %orphaned, and only connected into the path at the end, giving strange %results. % %The input points can be of any numerical class. % %Note that this will *not* necessarily form the shortest path between all %the points -- that is the NP-Hard Traveling Salesman Problem, for which %there is no deterministic solution. This will, however, find the shortest %path for points with a symmetric nearest neighbor matrix. % %see also: bwtraceboundary % %Example 1: continuous points %N=200; %P=1; %theta=linspace(0,2*pi*(1-1/N),N); %[~,I]=sort(rand(1,N)); %R=2+sin(5*theta(I))/3; % %Xin=R.*cos(theta(I)); %Yin=R.*sin(theta(I)); % %[Xout,Yout]=points2contour(Xin,Yin,P,'cw'); % %figure; %hold on %plot(Xin,Yin,'b-') %plot(Xout,Yout,'r-','Linewidth',2) %plot(Xout(2:end-1),Yout(2:end-1),'k.','Markersize',15) %plot(Xout(1),Yout(1),'g.','Markersize',15) %plot(Xout(end),Yout(end),'r.','Markersize',15) %xlabel('X') %ylabel('Y') %axis equal tight %title(['Black = original points, Blue = original ordering, Red = new ordering, Green = starting points']) %box on % % %Example 2: square grid %P=1; % %Xin=[1,2,3,4,4,4,4,3,2,1,1,1]; %Yin=[0,0,0,0,1,2,3,3,2,2,1,0];
  • 3. % %[Xout,Yout]=points2contour(Xin,Yin,P,'cw'); % %figure; %hold on %plot(Xin,Yin,'b-') %plot(Xout,Yout,'r-','Linewidth',2) %plot(Xout(2:end-1),Yout(2:end-1),'k.','Markersize',15) %plot(Xout(1),Yout(1),'g.','Markersize',15) %plot(Xout(end),Yout(end),'r.','Markersize',15) %xlabel('X') %ylabel('Y') %axis equal tight %box on % %Example 3: continuous points, pathological case %N=200; %P=1; %theta=linspace(0,2*pi*(1-1/N),N); %[~,I]=sort(rand(1,N)); %R=2+sin(5*theta(I))/3; % %Xin=(1+rand(1,N)/2).*R.*cos(theta(I)); %Yin=(1+rand(1,N)/2).*R.*sin(theta(I)); % %[Xout,Yout]=points2contour(Xin,Yin,P,'cw'); % %figure; %hold on %plot(Xin,Yin,'b-') %plot(Xout,Yout,'r-','Linewidth',2) %plot(Xout(2:end-1),Yout(2:end-1),'k.','Markersize',15) %plot(Xout(1),Yout(1),'g.','Markersize',15) %plot(Xout(end),Yout(end),'r.','Markersize',15) %xlabel('X') %ylabel('Y') %axis equal tight %title(['Black = original points, Blue = original ordering, Red = new ordering, Green = starting points']) %box on % %Example 4: continuous points, distance limit applied %N=200; %P=1; %theta=linspace(0,2*pi*(1-1/N),N); %[~,I]=sort(rand(1,N)); %R=2+sin(5*theta(I))/3; %R(2)=5; %the outlier %
  • 4. %Xin=(1+rand(1,N)/16).*R.*cos(theta(I)); %Yin=(1+rand(1,N)/16).*R.*sin(theta(I)); % %[Xout,Yout,orphans,indout]=points2contour(Xin,Yin,P,'cw',1); % %figure; %hold on %plot(Xin,Yin,'b-') %plot(Xin(orphans),Yin(orphans),'kx') %plot(Xin(indout),Yin(indout),'r-','Linewidth',2) %plot(Xout(2:end-1),Yout(2:end-1),'k.','Markersize',15) %plot(Xout(1),Yout(1),'g.','Markersize',15) %plot(Xout(end),Yout(end),'r.','Markersize',15) %xlabel('X') %ylabel('Y') %axis equal tight %title(['Black = original points, Blue = original ordering, Red = new ordering, Green = starting points']) %box on % function [Xout,Yout,varargout]=points2contour(Xin,Yin,P,direction,varargin) %check to make sure the vectors are the same length if length(Xin)~=length(Yin) error('Input vectors must be the same length.') end %check to make sure point list is long enough if length(Xin)<2 error('The point list must have more than two elements.') end %check distance limit if ~isempty(varargin) dlim=varargin{1}; if dlim<=0 error('The distance limit parameter must be greater than zero.') end else dlim=-1; end %check direction input if and(~strcmp(direction,'cw'),~strcmp(direction,'ccw')) error(['Direction input: ' direction ' is not valid, must be either "cw" or "ccw".']) end %check to make sure P is in the right range
  • 5. P=round(P); npts=length(Xin); if or(P<1,P>npts) error('The starting point P is out of range.') end %adjust input vectors for starting point if size(Xin,1)==1 Xin=circshift(Xin,[0,1-P]); Yin=circshift(Yin,[0,1-P]); else Xin=circshift(Xin,[1-P,0]); Yin=circshift(Yin,[1-P,0]); end %find distances between all points D=zeros(npts,npts); for q1=1:npts D(q1,:)=sqrt((Xin(q1)-Xin).^2+(Yin(q1)-Yin).^2); end %max distance maxD=max(D(:)); %avoid self-connections D=D+eye(npts)*maxD; %apply distance contraint by removing bad points and starting over if dlim>0 D(D>=dlim)=-1; %find bad points bad_pts=sum(D,1)==-npts; orphans=find(bad_pts); %check starting point if sum(orphans==P)>0 error('The starting point index is a distance outlier, choose a new starting point.') end %get good points Xin=Xin(~bad_pts); Yin=Yin(~bad_pts); %number of good points npts=length(Xin); %find distances between all points
  • 6. D=zeros(npts,npts); for q1=1:npts D(q1,:)=sqrt((Xin(q1)-Xin).^2+(Yin(q1)-Yin).^2); end %max distance maxD=max(D(:)); %avoid self-connections D=D+eye(npts)*maxD; else orphans=[]; end %tracking vector (has this original index been put into the ordered list?) track_vec=zeros(1,npts); %construct directed graph Xout=zeros(1,npts); Yout=zeros(1,npts); indout0=zeros(1,npts); Xout(1)=Xin(1); Yout(1)=Yin(1); indout0(1)=1; p_now=1; track_vec(p_now)=1; for q1=2:npts %get current row of distance matrix curr_vec=D(p_now,:); %remove used points curr_vec(track_vec==1)=maxD; %find index of closest non-assigned point p_temp=find(curr_vec==min(curr_vec),1,'first'); %reassign point Xout(q1)=Xin(p_temp); Yout(q1)=Yin(p_temp); %move index p_now=p_temp; %update tracking track_vec(p_now)=1; %update index vector
  • 7. indout0(q1)=p_now; end %undo the circshift temp1=find(~bad_pts); indout=circshift(temp1(indout0),[P,0]); %%%%%%% SET CONTOUR DIRECTION %%%%%%%%%%%% %contour direction is a *global* feature that cannot be determined until %all the points have been sequentially ordered. %calculate tangent vectors tan_vec=zeros(npts,3); for q1=1:npts if q1==npts tan_vec(q1,:)=[Xout(1)-Xout(q1),Yout(1)-Yout(q1),0]; tan_vec(q1,:)=tan_vec(q1,:)/norm(tan_vec(q1,:)); else tan_vec(q1,:)=[Xout(q1+1)-Xout(q1),Yout(q1+1)-Yout(q1),0]; tan_vec(q1,:)=tan_vec(q1,:)/norm(tan_vec(q1,:)); end end %determine direction of contour local_cross=zeros(1,npts); for q1=1:npts if q1==npts cross1=cross(tan_vec(q1,:),tan_vec(1,:)); else cross1=cross(tan_vec(q1,:),tan_vec(q1+1,:)); end local_cross(q1)=asin(cross1(3)); end %figure out current direction if sum(local_cross)<0 curr_dir='cw'; else curr_dir='ccw'; end %set direction of the contour if and(strcmp(curr_dir,'cw'),strcmp(direction,'ccw')) Xout=fliplr(Xout); Yout=fliplr(Yout); end %varargout if nargout==3