Ruby Vector normalize()函数
normalize() 是Ruby中的一个内置方法,它返回一个方向相同但规范等于1的新向量。
语法 :vec1.normalize()
参数 :该函数不接受任何参数
返回值 :它返回一个方向相同但规范值等于1的新向量。
例子 1 :
# Ruby program for normalize() method in Vector
# Include matrix
require "matrix"
# Initialize the vector
vec1 = Vector[1, 2, 3]
# Prints vector with the same direction
puts vec1.normalize()
输出:
Vector[0.2672612419124244, 0.5345224838248488, 0.8017837257372732]
例2 :
# Ruby program for normalize() method in Vector
# Include matrix
require "matrix"
# Initialize the vector
vec1 = Vector[1, 1, 1]
# Prints vector with the same direction
puts vec1.normalize()
输出:
Vector[0.5773502691896258, 0.5773502691896258, 0.5773502691896258]