Open In App

Ruby | Vector angle_with() function

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

The angle_with() is an inbuilt method in Ruby returns the angle between two vectors. The angle is always between 0 and π

Syntax: vec1.angle_with(vec2)

Parameters: The function accepts vector as parameter

Return Value: It returns the angle between two vectors

Example 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)


Output:

0.46364760900080615

Example 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)


Output:

1.3452829208967654

Last Updated : 07 Jan, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads