# matlab画立体仓库库位图_Matlab 画向量图和平面图

1. 画向量图函数
function plotv3(v_start, v_end, v_size, v_color)
% This function is used to plot 3d vectors.
%
% Parameters:
%   v_start: the start point of a vector
%   v_end: the end point of a vector
%   v_size: the vector line width, optional
%   v_color: the vector's color, optional
%
% Examples:
%   plotv3([0,0,0],[1,1,1]), plot a vector
%   plotv3(p1, p2, 2), plot a vector,line width is 2
%   plotv3(p1, p2, 2, 'g'), plot a vector, line width is 2, color is green
%
% @Author: ERYUESANHI
% @Date: 2020-11-03 20:25:48

v_style = quiver3(v_start(1),v_start(2),v_start(3), ...
v_end(1), v_end(2), v_end(3));
v_style.LineWidth = 1.5;
v_style.AutoScale = 'off';
if nargin > 2
v_style.LineWidth = v_size;
end
if nargin >3
v_style.Color = v_color;
end
if nargin > 4
fprintf("Most 4 inputsn");
end

end


2. 画3xn矩阵的列向量图

function plotmv3(m)
% This function is used to plot 3d column vectors of a 3xn matrix.
%
% Parameters:
%   m: a 3xn matrix.
%
% Examples:
%   plotmv3(m);
%
% @Author: ERYUESANHI
% @Date: 2020-11-03 21:25:28

[row, col] = size(m);

if row ~= 3
fprintf("error! not 3 dimention");
end

for i=1:col
v_style = quiver3(0,0,0, m(1,i), m(2,i), m(3,i));
v_style.LineWidth = 1.5;
hold on;
end
end



3. 画两个向量确定的平面

function plotvp(v1, v2, p_color, p_alpha, p_edge_color)
% This function is used to plot a plane formed by to vectors.
%
% Parameter:
%   v1: vector 1
%   v2: vector 2
%   p_color: plane color, optional
%   p_alpha: plane transparency, optional
%   p_edge_color: plane grid color, optional
%
% Examples:
%   plotvp([1,2,3],[2,3,4]), plot a plane formed by [1,2,3]' and [2,3,4];
%   plotvp(v1,v2,'r'), the plane is red;
%   plotvp(v1,v2,'r','0.5'), the red plane's alpha is 0.5;
%   plotvp(v1,v2,'r','0.5','g'), the plane with green grids.
%
% @Author: ERYUESANHI
% @Date: 2020-11-03 20:25:48

syms x y z;
n = cross(v1,v2);
if n(3) ~= 0
z = (-n(1)*x - n(2)*y)/n(3);
else
z = 0;
end
fs = fsurf(x,y,z);
fs.FaceColor = '#cdcdcd';
fs.FaceAlpha = 0.5;
fs.EdgeColor = 'none';

if nargin > 2
fs.FaceColor = p_color;
end
if nargin > 3
fs.FaceAlpha = p_alpha;
end
if nargin > 4
fs.EdgeColor = p_edge_color;
end
if nargin > 5
fprintf("Most 5 inputsn");
end
end

clear; clc; close all;

O = [0,0,0];

e1 = [1,0,0]';
e2 = [0,1,0]';
e3 = [0,0,1]';

plotvp(e1,e2,'g',0.3);  % 画e1,e2张成的平面
hold on
plotvp((e1+e2+e3),[2,4,0],'r',0.3) % 画e1+e2+e3和[2 4 0]'所张成的平面
plotv3(O,[2 4 0]) % 画[2 4 0]向量
plotmv3([e1,e2,e3]) % 画三个基向量
plotv3(O,(e1+e2+e3)) % 三个基向量的和向量
plotv3(O,(e1+e2)) % e1和e2的和向量， e1+e2+e3在e1,e2所在平面的投影
plotv3((e1+e2),[1 1 1]-[e1+e2]) % e1+e2的正交向量
axis([-1.5,1.5,-1.5,1.5,-0.5,3])


08-27 2万+
03-21 2143
08-14 4075
03-21 4092
02-21 2万+
04-18 2050
04-22 253
04-04 888