垂直二等分線の方程式を返すRの関数をRで自作

perpendicular_bisector
function(A,B,print=TRUE){
'%&%' <- function(x,y) paste0(x,y)
if(is.complex(A)|is.complex(B)){
a1=Re(A)
a2=Im(A)
b1=Re(B)
b2=Im(B)
M=A/2+B/2
}else{
a1=A[1]
a2=A[2]
b1=B[1]
b2=B[2]
M=(a1+b1)/2+1i*(a2+b2)/2
}
# (a1-b1)*(x-Re(M)+(a2-b2)*(y-Im(M)=0 inner product=0 M-(x,y) perpendicular to A-B
# cat( '(' %&% (a1-b1) %&% ')*(x-(' %&% Re(M) %&% ')) + (' %&%
# (a2-b2) %&% ')*(y-(' %&% Im(M) %&% ')) = 0 \n')
if(a2==b2){ # vertical line
if(print) cat('x = ' %&% Re(M) %&% '\n')
invisible(Re(M))
}else{ # y=f(x)
if(a1==b1){
if(print) cat('y = ' %&% ((a1*Re(M)+a2*Im(M)-b1*Re(M)-b2*Im(M))/(a2-b2)) %&% '\n')
}else{
slope=(b1-a1)/(a2-b2)
if(slope==-1) slope='-'
if(slope==1) slope=NULL else slope=as.character(slope)
intercept=(a1*Re(M)+a2*Im(M)-b1*Re(M)-b2*Im(M))/(a2-b2)
if(intercept==0) intercept=NULL else intercept=paste(' +',as.character(intercept))
if(print) cat('y = ' %&% slope %&% 'x' %&% intercept %&% '\n')
}
invisible(function(x) ((b1-a1)*x+a1*Re(M)+a2*Im(M)-b1*Re(M)-b2*Im(M))/(a2-b2))
}
}
これをWolframに移植するのは大変だなぁ。
既に関数化されていた。
https://reference.wolfram.com/language/ref/PerpendicularBisector.html?q=PerpendicularBisector