How to make the program more flexible

조회 수: 1 (최근 30일)
han han
han han 2020년 6월 26일
댓글: han han 2020년 6월 27일
I want to make a frame that can follow the pattern.
ex, M = 4,N = 4, I would got a 4 by 4 "x" (matrix)
However, this Mg by Ng frame will surround the M by N matrix
But my program is not flexible enough and the problem is in the frame settings, but I don’t know how to modify...
M = 4; % "\" pattern and "/" pattern (column)
N = 4; %"\" pattern and "/" pattern (Row)
Mg = 1; % Frame size (column)
Ng = 1; % Frame size (Row)
a = [1 0.5];
b = [0.5 1];
for q = 1:1:M*Mg
hold on;
for w = 1:1:N*Ng
hold on;
plot(a+q,b+w,'r') % "\" pattern
end
end
hold on;
a1 = [1 0.5];
b1 = [1 0.5];
for q = 1:1:M*Mg
hold on;
for w = 1:1:N*Ng
hold on;
plot(a1+q,b1+w,'b') % "/" pattern
end
end
%---------Frame size--------------
for M = 0 : M: M*Mg
hold on;
x=[1.25 1.25 1.25+M 1.25+M 1.25];
for N = 0 : N: N*Ng
y=[1.25 1.25+N 1.25+N 1.25 1.25];
plot(x,y,'k','lineWidth',2)
end
end
Hope to present the following fig, M = 4; N = 4; Mg = 2; Ng = 2;

채택된 답변

Tommy
Tommy 2020년 6월 26일
Here's a function that places an M x N grid of Xs within a rectangle defined by pos, as well as a few lines which use that function to create your frames.
M = 8; % "\" pattern and "/" pattern (column)
N = 8; %"\" pattern and "/" pattern (Row)
Mg = 2; % Frame size (column)
Ng = 2; % Frame size (Row)
ax = axes('NextPlot', 'add',...
'XTick', [],...
'YTick', []);
for ii = 1:Mg
for jj = 1:Ng
pos = [ii, jj, 1, 1];
fillSquareWithXs(ax, pos, M, N)
end
end
function fillSquareWithXs(ax, pos, M, N)
% ax - target axes
% pos - position of square, [llx, lly, width, height]
% M - # of rows of Xs
% N - # of cols of Xs
% find location, width, and height of each X
xco = linspace(pos(1), pos(1)+pos(3), M+1);
yco = linspace(pos(2), pos(2)+pos(4), N+1);
dx = diff(xco)/2;
dy = diff(yco)/2;
xco = xco(1:end-1) + dx;
yco = yco(1:end-1) + dy;
% plot Xs
for q = xco
for w = yco
plot(ax,q+[dx/2 -dx/2],w+[-dy/2 dy/2],'r') % "\" pattern
plot(ax,q+[dx/2 -dx/2],w+[dy/2 -dy/2],'b') % "/" pattern
end
end
% plot borders
plot([pos(1), pos(1)], [pos(2), pos(2)+pos(4)], 'k', 'LineWidth', 2);
plot([pos(1), pos(1)+pos(3)], [pos(2)+pos(4), pos(2)+pos(4)], 'k', 'LineWidth', 2);
plot([pos(1), pos(1)+pos(3)], [pos(2), pos(2)], 'k', 'LineWidth', 2);
plot([pos(1)+pos(3), pos(1)+pos(3)], [pos(2), pos(2)+pos(4)], 'k', 'LineWidth', 2);
end
  댓글 수: 4
Tommy
Tommy 2020년 6월 27일
Happy to help! Good point, try this:
M = 4; % "\" pattern and "/" pattern (column)
N = 8; %"\" pattern and "/" pattern (Row)
Mg = 2; % Frame size (column)
Ng = 2; % Frame size (Row)
ax = axes('NextPlot', 'add',...
'XTick', [],...
'YTick', []);
for ii = 1:Mg
for jj = 1:Ng
pos = [ii, jj, 1, 1];
fillSquareWithXs(ax, pos, M, N)
end
end
function fillSquareWithXs(ax, pos, M, N)
% ax - target axes
% pos - position of square, [llx, lly, width, height]
% M - # of rows of Xs
% N - # of cols of Xs
% find location, width, and height of each X
xco = linspace(pos(1), pos(1)+pos(3), M+1);
yco = linspace(pos(2), pos(2)+pos(4), N+1);
dx = (xco(2)-xco(1))/2; % <--- changed
dy = (yco(2)-yco(1))/2; % <--- lines
xco = xco(1:end-1) + dx;
yco = yco(1:end-1) + dy;
% plot Xs
for q = xco
for w = yco
plot(ax,q+[dx/2 -dx/2],w+[-dy/2 dy/2],'r') % "\" pattern
plot(ax,q+[dx/2 -dx/2],w+[dy/2 -dy/2],'b') % "/" pattern
end
end
% plot borders
plot([pos(1), pos(1)], [pos(2), pos(2)+pos(4)], 'k', 'LineWidth', 2);
plot([pos(1), pos(1)+pos(3)], [pos(2)+pos(4), pos(2)+pos(4)], 'k', 'LineWidth', 2);
plot([pos(1), pos(1)+pos(3)], [pos(2), pos(2)], 'k', 'LineWidth', 2);
plot([pos(1)+pos(3), pos(1)+pos(3)], [pos(2), pos(2)+pos(4)], 'k', 'LineWidth', 2);
end
han han
han han 2020년 6월 27일
I can't thank you enough.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 General Applications에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by