Question: Triangle Orthocenter division by zero error

I am trying to find the orthocenter in the x,y plane, given three coordinates  (x1,y1) (x2,y2) (x3,y3).

I am trying to avoid division by zero.

orthocenter.mw
 

restart:
orthocenter:=proc(x1,y1,x2,y2,x3,y3)
local m1,m2,m3,L1,L2,L3,slope;
slope:=(a,b,c,d)->(d-b)/(c-a);

if x1=x2 then L1:=x=x1;
elif y1=y2 then L1:=y=y1;
else m1:=-1/slope(x2,y2,x3,y3); L1:=y-y1=m1*(x-x1);
end if;

if x2=x3 then L2:=x=x2;
elif y2=y3 then L2:=y=y2;
else m2:=-1/slope(x1,y1,x3,y3);L2:=y-y2=m2*(x-x2);
end if;

if x1=x3 then L3:=x=x1;
elif y1=y3 then L3:=y=y1;
else m3:=-1/slope(x1,y1,x2,y2);L3:= y-y3= m3*(x-x3);
end if;
print(solve({L1,L2,L3},{x,y}));
end proc:

orthocenter(1,1,3,4,5,3);

{x = 11/4, y = 9/2}

(1)

orthocenter(0,0,0,3,4,1)

Error, (in slope) numeric exception: division by zero

 

 


 

Download orthocenter.mw

 

Please Wait...