北太天元画矩阵非零元的脚本

%draw_matrix.m 北太天元脚本函数
% 北太天元把矩阵中的非零元的位置画出来
% A [input] 布尔矩阵或者double 矩阵,如果|A(i,j)| > eps 则填充红色
% 非零元使用 |A(i,j)|>eps 来判定的
%例如
%clf
%close all
%clear all
%m = 3;
%n = 3;
%A = zeros(m,n);
%A(2,2) = 1.0;
%draw_matrix(A)
%
function ok = draw_matrix(A)
clf
close all
hold on;
if isa(A,'double')
B = abs(A) > eps;
elseif islogical(A)
B = A
else
ok = false;
error("A需要是布尔矩阵或者double矩阵");
end
[m,n] = size(A);
for i=1:m
for j=1:n
if(B(i,j))
tiao_rectangle('Position',[i-1 j-1 1 1], 'Curvature', 0, 'FaceColor', [1,0,0], 'EdgeColor','m', 'LineWidth', 1)
else
tiao_rectangle('Position',[i-1 j-1 1 1], 'Curvature', 0, 'FaceColor', [0.9,0.9,0.9], 'EdgeColor','m', 'LineWidth', 1)
end
end
end
hold off
ok = true;
end
%Position[input] 总是写成"Position"
%pos[input] 是一个1x4的矩阵 pos =[ 左下x, 左下y, 宽度, 高度]
%cur[input] 0 表示直角举行
%fc[input] 填充的颜色
%lw[input] 边的宽度
function ok = tiao_rectangle(Position, pos, Curvature, curv, FaceColor, fc, EdgeColor, ec, LineWidth, lw)
len = length(curv);
if (len >= 2)
horz_curv = curv(1);
vert_curv = curv(2);
elseif (len == 1)
horz_curv = curv(1);
vert_curv = curv(1);
else
horz_curv = 0;
vert_curv = 0;
end
lux = pos(1); luy =pos(2);
width = pos(3); height = pos(4);
if ( abs(horz_curv)<0.1 && abs(vert_curv) <0.1 )
fx = [lux, lux+width, lux+width ,lux];
fy = [luy, luy, luy+height, luy+height];
else
n = 15;
p = pi / 2 * [0 : n] / n;
c = curv .* pos(3:4) / 2;
cx = c(1) * sin (p) - c(1);
cy = c(2) * cos (p) - c(2);
fx = [pos(1) - cx(n+1:-1:1), pos(1) + pos(3) + cx, ...
pos(1) + pos(3) + cx(n+1:-1:1), pos(1) - cx, pos(1)];
fy = [pos(2) - cy(n+1:-1:1), pos(2) - cy, ...
pos(2) + pos(4) + cy(n+1:-1:1), ...
pos(2) + pos(4) + cy, pos(2) + c(2)];
end
fill(fx, fy, fc, EdgeColor, ec, LineWidth, lw)
ok = true;
end