如何在 MATLAB 中显示位于特定角度的箭头?

发布于 2024-08-12 10:43:45 字数 224 浏览 6 评论 0原文

我正在 MATLAB 中工作,遇到一个非常简单的问题:我有一个由其位置 (x,y)theta (角度,以度为单位)。我想绘制该点并添加一个箭头,从该点开始并指向角度定义的方向。实际上,它甚至不必是箭头,任何以图形方式显示角度值的东西都可以!

这是一张显示我正在尝试绘制的东西的图片:

删除了无效的 ImageShack 链接

I am working in MATLAB and I'm stuck on a very simple problem: I've got an object defined by its position (x,y) and theta (an angle, in degrees). I would like to plot the point and add an arrow, starting from the point and pointing toward the direction defined by the angle. It actually doesn't even have to be an arrow, anything graphically showing the value of the angle will do!

Here's a picture showing the kind of thing I'm trying to draw:

removed dead ImageShack link

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鸠书 2024-08-19 10:43:45

quiver() 绘图函数可以像这样绘制箭头。获取您的 theta 值并将其转换为 (x,y) 笛卡尔坐标,表示您想要绘制为箭头的向量,并将它们用作 quiver() 的 (u,v) 参数。

theta = pi/9;
r = 3; % magnitude (length) of arrow to plot
x = 4; y = 5;
u = r * cos(theta); % convert polar (theta,r) to cartesian
v = r * sin(theta);
h = quiver(x,y,u,v);
set(gca, 'XLim', [1 10], 'YLim', [1 10]);

在线浏览 Matlab 文档以了解其他绘图类型;有很多,包括几个放射状图。它们位于 MATLAB > 中功能>>图形>专业绘图部分。在命令行执行“doc quiver”并浏览。

The quiver() plotting function plots arrows like this. Take your theta value and convert it to (x,y) cartesian coordinates representing the vector you want to plot as an arrow and use those as the (u,v) parameters to quiver().

theta = pi/9;
r = 3; % magnitude (length) of arrow to plot
x = 4; y = 5;
u = r * cos(theta); % convert polar (theta,r) to cartesian
v = r * sin(theta);
h = quiver(x,y,u,v);
set(gca, 'XLim', [1 10], 'YLim', [1 10]);

Take a look through online the Matlab documentation to see other plot types; there's a lot, including several radial plots. They're in the MATLAB > Functions > Graphics > Specialized Plotting section. Do "doc quiver" at the command line and browse around.

浮华 2024-08-19 10:43:45

如果您想尝试制作看起来像您链接到的图像的东西,这里有一些代码可以帮助您做到这一点(注意:您首先必须下载提交的内容arrow.m 作者:Erik Johnson 关于 MathWorks 文件Exchange,我总是喜欢用它来生成任何形状和大小的箭头):

x = 1;                          % X coordinate of arrow start
y = 2;                          % Y coordinate of arrow start
theta = pi/4;                   % Angle of arrow, from x-axis
L = 2;                          % Length of arrow
xEnd = x+L*cos(theta);          % X coordinate of arrow end
yEnd = y+L*sin(theta);          % Y coordinate of arrow end
points = linspace(0, theta);    % 100 points from 0 to theta
xCurve = x+(L/2).*cos(points);  % X coordinates of curve
yCurve = y+(L/2).*sin(points);  % Y coordinates of curve
plot(x+[-L L], [y y], '--k');   % Plot dashed line
hold on;                        % Add subsequent plots to the current axes
axis([x+[-L L] y+[-L L]]);      % Set axis limits
axis equal;                     % Make tick increments of each axis equal
arrow([x y], [xEnd yEnd]);      % Plot arrow
plot(xCurve, yCurve, '-k');     % Plot curve
plot(x, y, 'o', 'MarkerEdgeColor', 'k', 'MarkerFaceColor', 'w');  % Plot point

它看起来像这样:

在此处输入图像描述

然后您可以向绘图添加文本(对于角度和坐标值)使用 text 功能。

If you want to try and make something that looks like the image you linked to, here's some code to help you do it (NOTE: you would first have to download the submission arrow.m by Erik Johnson on the MathWorks File Exchange, which I always like to use for generating arrows of any shape and size):

x = 1;                          % X coordinate of arrow start
y = 2;                          % Y coordinate of arrow start
theta = pi/4;                   % Angle of arrow, from x-axis
L = 2;                          % Length of arrow
xEnd = x+L*cos(theta);          % X coordinate of arrow end
yEnd = y+L*sin(theta);          % Y coordinate of arrow end
points = linspace(0, theta);    % 100 points from 0 to theta
xCurve = x+(L/2).*cos(points);  % X coordinates of curve
yCurve = y+(L/2).*sin(points);  % Y coordinates of curve
plot(x+[-L L], [y y], '--k');   % Plot dashed line
hold on;                        % Add subsequent plots to the current axes
axis([x+[-L L] y+[-L L]]);      % Set axis limits
axis equal;                     % Make tick increments of each axis equal
arrow([x y], [xEnd yEnd]);      % Plot arrow
plot(xCurve, yCurve, '-k');     % Plot curve
plot(x, y, 'o', 'MarkerEdgeColor', 'k', 'MarkerFaceColor', 'w');  % Plot point

And here's what it would look like:

enter image description here

You can then add text to the plot (for the angle and the coordinate values) using the text function.

药祭#氼 2024-08-19 10:43:45

这是部分答案,希望您能弄清楚其余部分。我启动了图形编辑器并打开了绘图工具。我将一个箭头从调色板拖到我的人物上。然后我生成了一个m文件。这包括这一行:

annotation(figure1,'arrow',[0.1489 0.2945],[0.5793 0.6481]);

因此,第一对坐标是箭头的起点。您必须使用一点三角学来找出尖端(第二对坐标)。如果你多用一些绘图工具,你甚至可能能够得到小弧线。

如果三角函数打败了您,请告诉我们。哦,我忘记了这一点,但我想你能明白吗?

Here's a partial answer, I expect you can figure out the rest. I fired up the Figures editor and opened the plot tools. I dragged an arrow from the palette onto my figure. Then I generated an m-file. This included the line:

annotation(figure1,'arrow',[0.1489 0.2945],[0.5793 0.6481]);

So, the first pair of coordinates is the start of the arrow. You're going to have to figure out the pointy end (second pair of coordinates) using a little bit of trigonometry. You might even be able to get the little arc if you do some more fiddling around with plot tools.

Let us know if the trig defeats you. Oh, and I forgot to plot the point, but I guess you can figure that out ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文