clc;
clear all;
nargin=2;
if (nargin < 3) % SETUP
% (GENERATE NODES AND SEGMENTS IF NOT GIVEN AS INPUTS)
% Create a random set of nodes/vertices, and connect some of them with
% edges/segments. Then graph the resulting map.
num_nodes = 20; L = 100; max_seg_length = 30; ids = (1:num_nodes)';
nodes = [ids L*rand(num_nodes, 2)]; % create random nodes
h = figure(1); plot(nodes(:, 2), nodes(:, 3), 'k.') % plot the nodes
text(nodes(num_nodes, 2), nodes(num_nodes, 3), ...
[' ' num2str(ids(num_nodes))], 'Color', 'b', 'FontWeight', 'b')
hold on
c = 1; segments = [];
for i = 1:num_nodes-1 % create edges between some of the nodes
text(nodes(i, 2), nodes(i, 3), ...
[' ' num2str(ids(i))], 'Color', 'b', 'FontWeight', 'b')
for j = i+1:num_nodes
d = sqrt(sum((nodes(i, 2:end) - nodes(j, 2:end)).^2));
if and(d < max_seg_length, rand < 0.6)
plot([nodes(i, 2) nodes(j, 2)], [nodes(i, 3) nodes(j, 3)], 'k.-')
% add this link to the segments list
segments = [segments; c nodes(i, 1) nodes(j, 1)];
c = c + 1;
end
评论0