MATLAB: How to draw a perpendicular line to another

anglesperpendicular lines

Hello,
Suppose i have a segment S1 defined by 2 points (x01,z01) and (x02, z02). The center of the segment is defines as (xm0, zm0). I want to plot the perpendicular line PL1 to S1 at (xm0, zm0) and find the intersection point of PL1 and the axis Y=0. The code is detailed below
a1=(z01-z02)/(x01-x02); %z1=a1*x1+b1 b1=z01-a1*x01; a2=-1/a1; % z2 perp to z1–> a2=-1/a1 b2=zm0-a2*xm0; %z2=a2*x2+b2
x=xm0:0.1:10; z2=a2*x+b2; xi=-b2/a2; %solve 0=a2*xi+b2 zi=a2*xi+b2;
h3=line([xm0,xi],[zm0,zi]); %trace du rayon principal
However, when i check the angle between my 2 perpendicular lines, it appears that it's not pi/2
O=(a2-a1)/(1-a1*a2); alpha=atan(O)*180/pi;
Any ideas please?
Fatzo

Best Answer

xm0 = (x01+x02)/2; ym0 = (y01+y02)/2
m = (y2 - y1) / (x2-x1);
PLm = tan( atan(m)+pi/2);
PLc = ym0 - PLm*xm0;
%PL line eqn = y_PL = PLm * x_PL + PLc
x_at_yequalto0 = -PLc/PLm;