Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
每个线段点处的垂直线-Matlab_Matlab - Fatal编程技术网

每个线段点处的垂直线-Matlab

每个线段点处的垂直线-Matlab,matlab,Matlab,在这段代码中,我将一条线分段到不同的部分,如何在每个分段点绘制一条长度为+/-5(该点上方和下方)的垂直线,并获得每条线的端点坐标 代码: 这最终是一个代数问题。您知道所需线条的坡度,perpSlope=-1/m;你知道你想要的每条线段的“原点,xy=(1-t)*xy1+t*xy2 首先,通过原点(0,0)绘制所需的线。使用毕达哥拉斯定理,计算直线的终点至所需长度: 5^2 = x^2 + y^2 -- Pythagorean theorem, length of your line is +

在这段代码中,我将一条线分段到不同的部分,如何在每个分段点绘制一条长度为+/-5(该点上方和下方)的垂直线,并获得每条线的端点坐标

代码:


这最终是一个代数问题。您知道所需线条的坡度,
perpSlope=-1/m
;你知道你想要的每条线段的“原点,
xy=(1-t)*xy1+t*xy2


首先,通过原点(0,0)绘制所需的线。使用毕达哥拉斯定理,计算直线的终点至所需长度:

5^2 = x^2 + y^2 -- Pythagorean theorem, length of your line is +-5
y = sqrt( 5^2 - x^2 ) -- Solved for y

y = mx + b -- Equation of a straight line
sqrt( 5^2 - x^2 ) = mx + 0 -- Substitute the above into y, and solve for x | b==0 for the origin
您将获得:
new_x=+-5/sqrt(m^2-1)
,这两个
new_x
的坐标是新斜率的函数

然后,根据两个
new\ux
的值求解
new\uy
值:
new\uy=m*new\ux
。现在,您有了一组通过原点绘制的线的坐标

最后,要获得通过每个点绘制的坐标集,只需将这些点的值
xy
添加到新坐标

for i = 1:numOfPoints
    % This assumes xy is in the format: [x1, y1; x2, y2; ...]
    % i.e. The first column are x coordinates, the second column are y coordinates
    line_x = new_x + xy(i,1) 
    line_y = new_y + xy(i,2)
    line(line_x, line_y)
end

重述一下:

当直线位于(0,0)时,计算新的x:
new_x=[5/sqrt(每坡^2-1),-5/sqrt(每坡^2-1)]

计算新x的新y:
new_y=perpSlope*new_x

根据新坐标计算并绘制新线:

for i = 1:length(xy)
    line( new_x + xy(i,1), new_y + xy(i,2) )
end
注意:这将在旧图形的顶部绘制,因此首先使用命令
hold on


编辑:此方法不适用于垂直/水平线,因为其中的坡度分别为无限和零。但这些情况应该非常容易编程。例如:

if isinf(m) % Original line is vertical
    line_x = [xy(i,1)+5, xy(i,1)-5]; % +- 5 to x axis
    line_y = [xy(i,2)  , xy(i,2)];   % No change to y axis
    line(line_x, line_y)
end

if m == 0 % Original line is horizontal
    line_x = [xy(i,1)  , xy(i,1)];   % No change to x axis
    line_y = [xy(i,2)+5, xy(i,2)-5]; % +- 5 to y axis
    line(line_x, line_y)
end

谢谢。我感谢你的帮助。似乎如果线条是水平或垂直的,它将不起作用。@Iroca,我真的忘记了这些情况,但它们应该很容易修复(请参见编辑)。现在,如果你要寻找一个单一的“整体”方程来解决所有问题,而不是使用if-else语句,那么你必须研究线性变换。(即旋转直线,绘制垂直线,然后将它们全部向后旋转)。祝你好运
if isinf(m) % Original line is vertical
    line_x = [xy(i,1)+5, xy(i,1)-5]; % +- 5 to x axis
    line_y = [xy(i,2)  , xy(i,2)];   % No change to y axis
    line(line_x, line_y)
end

if m == 0 % Original line is horizontal
    line_x = [xy(i,1)  , xy(i,1)];   % No change to x axis
    line_y = [xy(i,2)+5, xy(i,2)-5]; % +- 5 to y axis
    line(line_x, line_y)
end