Plot angle between vectors

前端 未结 3 1173
别跟我提以往
别跟我提以往 2020-12-17 19:27

I am searching for quite a while already now for a function that plots the angle between two arrows / line segments. e.g.:


(source: matrix44.net)

3条回答
  •  眼角桃花
    2020-12-17 20:03

    Just a follow-up to pthesling's answer: This one only works if one of the vectors is a multiple of (1,0). A more general approach to plot the angle between any pair of vectors would work as follows:

    library(plotrix)
    # Creates empty plot and variables
    plot(1,type="n",axes=F,xlim=c(-0.1,0.9),ylim=c(-0.1,0.9),xlab="",ylab="")
    x0 = c(1,0)
    x1 = c(0.6,0.2)
    x2 = c(0.4,0.7)
    
    # X1
    arrows(0,0,x1[1],x1[2])
    text(0.62,0.18,expression(x[1]),cex=1.5)
    
    # X2
    arrows(0,0,x2[1],x2[2])
    text(0.4,0.75,expression(x[2]),cex=1.5)
    
    
    alpha_angle1= acos((x1%*%x0)/(sqrt(x1%*%x1)*sqrt(x0%*%x0)))
    alpha_angle2= acos((x2%*%x0)/(sqrt(x2%*%x2)*sqrt(x0%*%x0)))
    
    
    draw.arc(0,0,0.15,angle1=alpha_angle1,angle2=alpha_angle2)
    
    text(0.07,0.06,expression(alpha),cex=1.5)
    

    Link to picture

提交回复
热议问题