在一条线的边缘绘制一条垂直线-Matlab

时间:2020-07-16 07:21:33

标签: matlab

如何绘制垂直线的一半(总长度10)在一条线的边缘(起点和终点)之上和之下一半?因此,它看起来像是旋转的错误栏。

代码:

$(window).scroll(() => { 
  // Distance from top of document to top of footer.
  topOfFooter = $('#shopify-section-product-recommendations').position().top;
  // Distance user has scrolled from top, adjusted to take in height of sidebar (570 pixels inc. padding).
  scrollDistanceFromTopOfDoc = $(document).scrollTop() + 800;
  // Difference between the two.
  scrollDistanceFromTopOfFooter = scrollDistanceFromTopOfDoc - topOfFooter;

  // If user has scrolled further than footer,
  // pull sidebar up using a negative margin.

  if (scrollDistanceFromTopOfDoc > topOfFooter) {
    $('.product-menu').css('margin-top',  0 - scrollDistanceFromTopOfFooter);
  } else  {
    $('.product-menu').css('margin-top', 0);
  }

});

1 个答案:

答案 0 :(得分:1)

您可以计算线的方向dir的向量,将其旋转90°orthDir,然后将此正交线添加/减去到线的端点。

clc;
clear all;
close all;
 
x1 =0;
y1 = 10;
x2 = 2;
y2 = 15;

p1 = [x1;y1];
p2 = [x2;y2];
 
plot([x1,x2],[y1,y2]);

lineLength = 10; 
dir = [x1-x2;y1-y2];
dirNormalized = dir./norm(dir);

orthDir = [0,-1;1,0]*dirNormalized;

hold on;
plot([p1(1), p1(1)+orthDir(1)*5],[p1(2), p1(2)+orthDir(2)*5],'g-')
plot([p1(1), p1(1)-orthDir(1)*5],[p1(2), p1(2)-orthDir(2)*5],'g-')
plot([p2(1), p2(1)+orthDir(1)*5],[p2(2), p2(2)+orthDir(2)*5],'g-')
plot([p2(1), p2(1)-orthDir(1)*5],[p2(2), p2(2)-orthDir(2)*5],'g-')
axis equal

enter image description here