以用户在 MATLAB 中测量的指定角度始终如一地生成线

Consistently generate line at at a specified angle as measured by user in MATLAB

考虑下面在 MATLAB 中生成直线向下倾斜的简单代码。

clear, clc, close all

t = 0:0.1:1;
y = -t+1;


plot(t,y)
ax = gca

这是一条斜率为-1的线,所以水平轴和线之间的(锐)角为45度。除了当你用显示器上的量角器测量时不是这样。

在不改变 x 和 y 轴上显示的值的范围或图形的高度 window 的情况下,如果我拿着一个量角器到屏幕?

我目前的做法是改变图形的宽度window。在图window无限细的情况下,x线是一条竖线。相反,如果图形 window 被拉伸到显示器的边缘,它就会变平。在中间的某个地方,这条线有我想要的角度。我只是找不到从数学上找到这一点并在代码中实例化它的好方法。

编辑:任何锐角的更通用的解决方案。 (我没有测试钝角。)

clear, clc, close all

ang_deg = 70;
slope = tand(ang_deg);


t = 0:0.1:1;
y = -slope*t+1;

f = figure;
f.Position(3) = f.Position(3)*1.5;
plot(t,y)

% For a given height, change the width
ax = gca;
ax.Units = 'pixels';
ax.Position(3) = ax.Position(4)/slope;

在你的命令后面声明你将以像素为单位工作,然后将宽度设置为高度:

ax.Units = 'pixels';
ax.Position(3) = ax.Position(4);

您可以通过

实现
axis equal

其中,根据 documentation

uses the same length for the data units along each axis.

您可能还想使用

axis tight

哪个

fits the axes box tightly around the data by setting the axis limits equal to the range of the data