Plot angle between vectors

前端 未结 3 1174
别跟我提以往
别跟我提以往 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:06

    Thanks bgoldst for your effort. I would like to contribute with a rather easy solution that not necessarily reproduces the picture above, but is maybe helpful for less experienced programmers like me:

    library(plotrix)
    
    # Creates empty plot and variables
    plot(1,type="n",axes=F,xlim=c(-0.1,1.2),ylim=c(-0.1,1.2),xlab="",ylab="")
    x1 = c(1,0)
    x2 = c(0.4,0.7)
    
    # X1
    arrows(0,0,x1[1],x1[2])
    text(1,-0.05,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_angle= acos((x2%*%x1)/(sqrt(x2%*%x2)*sqrt(x1%*%x1)))
    
    draw.arc(0,0,0.15,angle2=alpha_angle)
    
    text(0.07,0.05,expression(alpha),cex=1.5)
    

    In the end this gives the following plot:

提交回复
热议问题