我正在尝试在Matlab中模拟目标的移动,该目标的初始x和y坐标、真实方位角和速度(以m/s表示)已经指定。我想知道是否有一种方法可以简单地绘制一条直线,以指定方位角展示目标所采取的路径(如下图所示)。谢谢!
您最好依靠内置的极坐标绘图函数来完成此操作。我认为最符合您需求的是compass
。它基本上会在极坐标图上绘制一个箭头,从中心指向笛卡尔坐标系中的一个点。
theta = deg2rad(130);
% Your speed in m/s
speed = 5;
hax = axes();
c = compass(hax, speed * cos(theta), speed * sin(theta));
% Change the view to orient the axes the way you've drawn
view([90 -90])
然后,要更改方向和速度,只需再次调用compass
函数,并输入您的新方向/速度即可。
new_theta = deg2rad(new_angle_degrees);
c = compass(hax, new_speed * cos(new_theta), new_speed * sin(new_theta));
polar
和polarplot
,它们接受极坐标但没有箭头。如果您不喜欢极坐标图,可以在笛卡尔坐标轴上使用quiver
(确保指定相同的坐标轴)。
编辑
根据您的反馈和请求,以下是一个距离旅行的极坐标图示例。% Speed in m/s
speed = 5;
% Time in seconds
time = 1.5;
% Bearing in degrees
theta = 130;
hax = axes();
% Specify polar line from origin (0,0) to target position (bearing, distance)
hpolar = polar(hax, [0 deg2rad(theta)], [0 speed * time], '-o');
% Ensure the axis looks as you mentioned in your question
view([90 -90]);
现在,如果您想使用新的方位、速度和时间更新此图表,只需再次调用polar
并指定坐标轴即可。
hpolar = polar(hax, [0 theta], [0 speed], '-o');
我不确定我是否理解正确,这是我的解决方案:
figure;hold on; % Create figure
x_start = 10;% Starting position
y_start = 20;
plot(x_start+[-1 1],[y_start y_start],'k');% Plot crosshairs
plot([x_start x_start],y_start+[-1 1],'k');
angle = -(130-90)*pi/180; % Bearing angle 130° like in your graph
x_target = x_start+10*cos(angle); % Calculation of target position
y_target = y_start+10*sin(angle);
plot(x_target+[-1 1],[y_target y_target],'k');% Plot crosshairs
plot([x_target x_target],y_target+[-1 1],'k');
% Draw line between start and target
plot([x_start x_target],[y_start y_target],'g');
set(gca,'xlim',[0 30],'ylim',[0 30]); % Adjust axes
text(x_start+1,y_start,'Start'); % Write text to points
text(x_target+1,y_target,'End');
polar
,您可以像使用plot
一样绘制任何内容,只不过您使用的是极坐标而不是笛卡尔坐标。 - Suever