当前位置: 首页 > news >正文

优化MATLAB中quiver函数绘制箭头图或矢量图(2)-MATLAB开发

MATLAB自建函数quiver3_Rendering

  • quiver3_Rendering
    • 更新内容
    • 新的quiver_Refine函数
    • quiver3_Rendering函数
    • 使用方法
    • 结果展示

quiver3_Rendering

之前利用MATLAB绘制更好看的箭头自建函数quiver_Refine可以输入箭头的位置(a,b,c)与朝向(alpha,beta,gamma),在坐标区中返回三维箭头;现进一步加强其可调节功能以及进一步打包至最终函数quiver3_Rendering。

更新内容

对quiver_Refine函数进行了一些功能拓展,除了标准的控制箭头位置与朝向功能,增加了如下几点新功能:

  1. 可选择箭头的旋转中心;控制箭头在旋转时以整个箭头的中点为旋转点或者以箭头的圆柱的中点为旋转点。
  2. 以HSL方式渲染箭头时色环可顺逆时针旋转且旋转的角度可输入
  3. 增加了箭头渲染模式可选功能,可以选择不同的箭头渲染模式甚至可自定义箭头渲染模式。
  4. 全新的箭头参数输入功能;箭头中圆锥的半径/高,圆柱的半径/高可作为参数输入。
  5. 增加了箭头透明度作为参数输入。
  6. quiver_Refine函数打包至quiver3_Rendering函数中,将位置矩阵,方向矩阵,以及色环角度/颜色渲染模式作为输入,与MATLAB内部函数quiver3类似。

新的quiver_Refine函数

function[s1,s2,s3,s4]=quiver_Refine(s,x,V,theta,color,r1,h1,r2,h2,al)%quiver_Refine(rotation_center,position,direction,color,Cone_r,Cone_h,...%Cylinder_r,Cylinder_h,alpha)%s=1rotate along the arrow center%s=0rotate along the cylinder center%color control the colormap%1~HSL%2~RWB%3~Customized%read data a=x(1,1);b=x(1,2);c=x(1,3);alpha=V(1,1);beta=V(1,2);gamma=V(1,3);r1=0:0.01:r1;%Cone%The cone top position a1=a;b1=b;%Generate cone data[u,v,w]=cylinder(r1,50);u=u+a1;v=v+b1;w=-w*h1+c+(h1+h2)/2+h1/2-h1/2*s;%Cone botton t1=(0:0.04:2)*pi;r=max(r1(:));xc=a1;yc=b1;zc=-h1+c+(h1+h2)/2+h1/2-h1/2*s;%Generate cone botton data x1=xc+cos(t1)*r;y1=yc+sin(t1)*r;[m,n]=size(x1);z1=repmat(zc,m,n);%Cylinder%The cylinder top position a2=a;b2=b;c2=c-h1;%Generate cylinder data[x,y,z]=cylinder(r2,50);x=x+a2;y=y+b2;z=-z*h2+c2+(h1+h2)/2+h1/2-h1/2*s;%Cylinder botton t2=(0:0.04:2)*pi;r=r2;xc=a2;yc=b2;zc=c2-h2+(h1+h2)/2+h1/2-h1/2*s;%Generate cylinder botton data x2=xc+cos(t2)*r;y2=yc+sin(t2)*r;[m,n]=size(x2);z2=repmat(zc,m,n);%Rotate arrow hold on origin=[a b c];theta2=acos(gamma./sqrt(alpha.^2+beta.^2+gamma.^2));ifbeta^2+alpha^2==0direct=[010];elsedirect=[-beta,alpha,0];endswitchcolorcase1%control vector color using HSL%Hue:in-plane magnetization%Lightness:out-of-plane magnetization H=atan2(-beta,alpha);%-beta due to the y direction reverse%Get the atan2 space in0~2*piifH<0H=2*pi+H;end%rotate clockwisely H=H+theta/180*pi;ifH<0H=H+2*pi;elseif H>2*pi H=H-2*pi;end S=sqrt(alpha.^2+beta.^2);I=(gamma+1)/2;%contrast=1;%S=min(1,contrast*(1-gamma));%I=min(1,contrast*(1+gamma));%change HSL to RGB idx=find((0<=H)&(H<2*pi/3));B(idx)=I(idx).*(1-S(idx));R(idx)=I(idx).*(1+S(idx).*cos(H(idx))./cos(pi/3-H(idx)));G(idx)=3*I(idx)-(B(idx)+R(idx));idx=find((2*pi/3<=H)&(H<4*pi/3));R(idx)=I(idx).*(1-S(idx));G(idx)=I(idx).*(1+S(idx).*cos(H(idx)-2*pi/3)./cos(pi-H(idx)));B(idx)=3*I(idx)-(G(idx)+R(idx));idx=find((4*pi/3<=H)&(H<=2*pi));G(idx)=I(idx).*(1-S(idx));B(idx)=I(idx).*(1+S(idx).*cos(H(idx)-4*pi/3)./cos(5*pi/3-H(idx)));R(idx)=3*I(idx)-(G(idx)+B(idx));case2%control vector color using RWB%White:in-plane magnetization%Red or blue:out-of-plane magnetizationifgamma<0R=1;elseR=1-gamma;endifgamma>0B=1;elseB=1+gamma;end G=1-abs(gamma);case3%control vector color using shades of green color R=0;B=0;G=1;otherwise R=0;B=0;G=0;end c=[R G B];c(isnan(c))=0;%Get c in the right sectionfori=1:3c(i)=max(min(c(i),1),0);end%plot s1=surf(u,v,w,'Facecolor',c,'Edgecolor','none','FaceAlpha',al);s2=surf(x,y,z,'Facecolor',c,'Edgecolor','none','FaceAlpha',al);s3=fill3(x1,y1,z1,c,'Edgecolor','none','FaceAlpha',al);s4=fill3(x2,y2,z2,c,'Edgecolor','none','FaceAlpha',al);iftheta2~=0rotate(s1,direct,rad2deg(theta2),origin);rotate(s2,direct,rad2deg(theta2),origin);rotate(s3,direct,rad2deg(theta2),origin);rotate(s4,direct,rad2deg(theta2),origin);end%must be enabled to get right axis axis equal end

quiver3_Rendering函数

function[]=quiver3_Rendering(X,Y,Z,U,V,W,theta,color)[N1,N2]=size(X);fori=1:N1forj=1:N2 a0=X(i,j);b0=Y(i,j);c0=Z(i,j);p=[a0,b0,c0];alpha0=U(i,j);beta0=V(i,j);gamma0=W(i,j);d=[alpha0,beta0,gamma0];%0.2,0.65,0.08,0.35quiver_Refine(1,p,d,theta,color,0.2,0.65,0.08,0.35,1);hold on end end end

使用方法

quiver3_Rendering(X,Y,Z,U,V,W,theta,color)
与MATLAB内部函数quiver3类似,(X,Y,Z)为输入的位置矩阵;(U,V,W)为相应的方向矩阵,theta为色环的旋转角度,color为箭头渲染的模式,1为经典的HSL渲染,2为经典的RWB三色渲染,3提供了可自定义的渲染方式。
quiver_Refine(s,x,V,theta,color,r1,h1,r2,h2,al)
s选择旋转中心,r1,h1,r2,h2控制箭头的具体形状,al控制箭头透明度

结果展示


http://www.jsqmd.com/news/507463/

相关文章:

  • Ansys Fluent 多相流模拟,核心供应商推荐 - 品牌2025
  • Blender PSK/PSA插件:游戏开发者的3D资产桥梁
  • Qwen-Image-2512-SDNQ与GitHub Actions集成:自动化图片生成流水线
  • Linux操作系统:进程间关系
  • Qwen3-ForcedAligner-0.6B GPU部署实战:解决CUDA out of memory常见报错
  • 为什么是你来做?面试中犀利问题的底层逻辑是什么和标准回答模版
  • 便携式生理信号采集系统开发小结
  • C++map容器
  • GitHub_Trending/we/WeChatMsg架构解析:核心组件设计与交互逻辑
  • Qwen3-32B-Chat开源模型对比评测:Llama3-70B/Qwen3-32B/DeepSeek-V3推理效率PK
  • C++ stack 容器适配器-栈
  • FPGA动态部分重配置技术的三大实现方案对比
  • Rancher容器网络深度剖析:从基础概念到高级配置
  • 别再傻傻分不清了!从摄像头RAW到屏幕RGB,图像格式转换保姆级指南
  • 大小端的计算公式
  • Linux网络编程:TCP初体验
  • Qt 线程
  • CosyVoice 实战部署全攻略:从云端实例到本地服务,5步打造专属语音克隆应用
  • python中class与C++class的区别和联系
  • 终极指南:MS-DOS批处理变量使用与早期脚本参数传递技巧
  • 基频检测算法总结
  • Zig核心特性深度解析:为何它能替代C成为系统编程新宠
  • 如何轻松实现微信聊天记录从JSON到PDF的完整转换:GitHub_Trending/we/WeChatMsg终极指南
  • 深入解析Python的glob.glob()函数:高效递归匹配文件与目录的实战技巧
  • 海康威视DS-2CD2T2HY-LP1刷机固件包|含专用刷机工具+通用版固件|支持强刷救砖|终身可重复使用
  • Navicat Premium连接Oracle 11g保姆级教程(附instantclient配置避坑指南)
  • BackInTime 开源项目安装与使用指南
  • UR5机械臂实战:不依赖MoveIt的直接ROS控制方法(Python示例)
  • 100套前端可视化模板合集:支持HTML与Vue双架构,集成高德地图+百度ECharts图表
  • TF-IDF vs Word2Vec:如何根据你的项目需求选择合适的文本表示方法?