draw topology in matlab

figi = figure(1);
seed = 42;  % select a seed
rng(seed, 'twister');

router_num = 5;
host_num = 0;     % zero means using the default value
edge_spds_r = [1e6 2e6 4e6];  % edge rates for routers
% dropprobs_r = 0.1;
dropprobs_r = 0.1;    % 原来是0

edge_spds_rh = Inf;
dropprobs_rh = 0.2;     % 原来是0

attmt_assumpt = 'evenly'; % attachment assumption
topotype = 'grid25';
% topotype = 'twohop';
[A, Aspds, Adrps, ur, vh, topoinfo] = genA(topotype, edge_spds_r, dropprobs_r, router_num, ...
    host_num, attmt_assumpt, edge_spds_rh, dropprobs_rh);

router_num = topoinfo.router_num;
A = triu(A(1:router_num, 1:router_num));
[u, v] = find(sparse(A));
edges = [u, v];

Aspds = triu(Aspds(1:router_num, 1:router_num));

xy = [topoinfo.x_r(:) topoinfo.y_r(:)];

x = xy(:, 1);

y = xy(:, 2);

G = graph(edges(:, 1), edges(:, 2));

idxOut = findedge(G, edges(:, 1), edges(:, 2));

Ghdl = plot(G, 'XData', x, 'YData', y, 'NodeLabel', {}, 'EdgeColor', 'k', ...
    'MarkerSize', 12, 'NodeColor', 'yellow');
% scatter(x, y, 20, 'MarkerEdgeColor', 'k',...
%               'MarkerFaceColor', 'yellow',...
%               'LineWidth', .5);
% labeledge(Ghdl, [3 12 7 16], [3 12 7 16]);
% Ghdl.EdgeFontSize = 14;


[u, v] = find(sparse(Aspds == edge_spds_r(1)));
edges_lspds = [u, v];

idxOut_lspds = findedge(G, edges_lspds(:, 1), edges_lspds(:, 2));

[u, v] = find(sparse(Aspds == edge_spds_r(2)));
edges_mspds  = [u, v];

idxOut_mspds = findedge(G, edges_mspds(:, 1), edges_mspds(:, 2));

idxOut_hspds = setdiff(idxOut, [idxOut_lspds; idxOut_mspds]);



highlight(Ghdl, edges_lspds(:, 1), edges_lspds(:, 2), 'EdgeColor', 'g');
highlight(Ghdl, edges_mspds(:, 1), edges_mspds(:, 2), 'EdgeColor', 'b', 'LineWidth', 1.5);

[sOut,tOut] = findedge(G, idxOut_hspds);

highlight(Ghdl, sOut, tOut, 'LineWidth', 2);

circles(x, y, 3, 'edgecolor','k','facecolor','none', 'facealpha', 1);

axis equal


% highlight(Ghdl, 1:numel(x), 'MarkerSize', 10);
text(x, y, num2cell(1:numnodes(G)), 'FontSize', 8, 'HorizontalAlignment', 'center', 'VerticalAlignment', 'middle');
axis equal
axis tight
axis('off');

fname = topotype;
hold on

L(1) = plot(nan, nan, 'g-');
L(2) = plot(nan, nan, 'b-', 'LineWidth', 1.5);
L(3) = plot(nan, nan, 'k-', 'LineWidth', 2);

legend(L, {'1 Mbps', '2 Mbps', '4 Mbps'}, 'location', 'eastoutside', 'FontSize', 8)


% plot(rand(3))
% lgd = legend('a','b','c');
% c = lgd.TextColor;
% lgd.TextColor = 'red';



exportgraphics(figi, [fname '.png'],'Resolution', 600);
posted @ 2026-02-06 21:27  ChrainY  阅读(3)  评论(0)    收藏  举报