Repository navigation
Expand file tree
/
Copy pathplotMaze.m
More file actions
70 lines (60 loc) · 2.1 KB
/
Copy pathplotMaze.m
File metadata and controls
70 lines (60 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
function fig = plotMaze(lineSegments, lineSpec, lineWidth)
%plotMaze Plot 2D line segments with equal aspect ratio and margins.
%
% plotMaze(lineSegments, lineSpec, lineWidth) takes a .NET list of
% LinFloat64Vector2D or Float64LineSegment2D objects
%
% The function plots all line segments in a single figure using a 1:1
% aspect ratio, automatically adjusts the axes to fit all segments, and
% adds a small margin around the data for better visualization.
%
% Author: Ahmad H. Eid
% Date: 2025-11-10
% Version: 1.1
%---------------------------%
% Validate input arguments %
%---------------------------%
if nargin < 3
lineWidth = 1;
end
if nargin < 2
lineSpec = 'b-';
end
if nargin < 1
error('plotMaze:MissingInput', ...
'Input argument "lineSegments" is required.');
end
%---------------------------------------%
% Prepare data for vectorized plotting %
%---------------------------------------%
% Create X and Y arrays with NaN separators
[X, Y] = getXyForPlot(lineSegments);
%---------------------------------------%
% Plot all line segments efficiently %
%---------------------------------------%
fig = figure();
hold on;
axis equal;
plot(X(:), Y(:), lineSpec, 'LineWidth', lineWidth);
%---------------------------------------%
% Compute and apply plot margins %
%---------------------------------------%
x_all = [X(1,:), X(2,:)]
y_all = [Y(1,:), Y(2,:)]
% Use 5% margin of the range (can be adjusted)
x_margin = 0.05 * range(x_all);
y_margin = 0.05 * range(y_all);
% Handle degenerate cases (e.g., all x or y are equal)
if x_margin == 0, x_margin = 1; end
if y_margin == 0, y_margin = 1; end
xlim([min(x_all)-x_margin, max(x_all)+x_margin]);
ylim([min(y_all)-y_margin, max(y_all)+y_margin]);
%---------------------------------------%
% Enhance plot appearance %
%---------------------------------------%
xlabel('X');
ylabel('Y');
title('2D Line Segments');
grid on;
box on;
end