红宝石 |矢量 angle_with()函数
angle_with()是 Ruby 中的一个内置方法,用于返回两个向量之间的角度。角度始终介于 0 和 π 之间
Syntax: vec1.angle_with(vec2)
Parameters: The function accepts vector as parameter
Return Value: It returns the angle between two vectors
示例 1 :
#Ruby program for angle_with() method in Vector
#Include matrix
require "matrix"
#Initialize the vector
vec1
= Vector[1, 2] vec2 = Vector[0, 1]
#Prints angle between two vectors
puts vec1.angle_with(vec2)
输出:
0.46364760900080615
示例 2 :
#Ruby program for angle_with() method in Vector
#Include matrix
require "matrix"
#Initialize the vector
vec1
= Vector[1, 1, 0] vec2 = Vector[0, 1, 3]
#Prints angle between two vectors
puts vec1.angle_with(vec2)
输出:
1.3452829208967654