绘制2个相互垂直的线 [英] Draw 2 imline to be perpendicular to each other matlab
问题描述
有什么方法可以将imline约束为始终垂直于在同一对象上绘制的另一个imline.对于前.我现在使用"imline"绘制第一条线,我想在第一条线上绘制第二条线以使其垂直.如果有一种方法可以迫使第二条折线垂直于第一条折线,并保持扩展长度的灵活性,那么它将在某种程度上解决我的问题. 我想要在图像上放置诸如可弯曲的十字线(可以沿轴旋转并具有灵活的侧面)之类的东西来测量特定对象的高度和宽度.
Is there any way to constraint the the imline to be always perpendicular to the other imline drawn on the same object. for ex. I draw a first line using "imline" now I want to draw second line across the first line to be perpendicular to it. if there is a way to force the second imline to be perpendicular to the first line keeping the flexibility of extending the length it will solve my problem some extent. I want something like a flexible cross hair(which can rotate along the axis and have flexible sides) on my image to measure the height and width of the certain object.
推荐答案
代码:
function perpline()
imshow(rand(200),[]);
line1 = imline(gca,[50 50; 150 150]);
setColor(line1,'r');
line2 = imline(gca,[50 150; 150 50]);
setColor(line2,'g');
addNewPositionCallback(line2,@(pos)callback_line(pos));
function callback_line(pos)
% Must update line1 based on line2's position
pos_line1 = getPosition(line1);
pos_line2 = getPosition(line2);
% Get middle
pos_center = [(pos_line2(1,1)+pos_line2(2,1))/2 (pos_line2(1,2)+pos_line2(2,2))/2];
% Find displacement
vec_disp = [pos_line2(2,1)-pos_line2(1,1) pos_line2(2,2)-pos_line2(1,2)];
% Get normal unit vector
vec_perp = [-vec_disp(2) vec_disp(1)]/norm(vec_disp);
% Preserve length of line2
length_line1 = norm([pos_line1(2,1)-pos_line1(1,1) pos_line1(2,2)-pos_line1(1,2)]);
pos_line1_update = [-vec_perp*length_line1/2+pos_center;
vec_perp*length_line1/2+pos_center];
% Set position
setPosition(line1,pos_line1_update);
end
end
将其另存为函数,然后调用它.您可以在周围拖动绿线,而红线保持垂直.请注意,您必须定义想要如何保持垂直度.我选择保留红线的长度并将其保持在绿线的中心.
Save it as a function then call it. You can drag the green line around and the red line remains perpendicular. Note that you have to define how you want it to preserve the perpendicularity. I chose to preserve the length of the red line and keep it in the center of the green line.
这篇关于绘制2个相互垂直的线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!