Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

271
Views
Ruby: how to calculate distance between two points in a 2d plane?

I am making some tests of developing simple games using Ruby. I have strong need of playing with 2D Vectors. For example how to easily calculate the distance between 2 points in 2D coordinates system?

I know I can just calculate the hypotenuse of the coordinates substraction but I am wondering if there is any out-of-the-box library or method to do this.

I found Math.distance but it is not working for me (undefined method 'distance' for Math:Module (NoMethodError)) even with require "facets"

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Google was not very helpful and it took me a while to find the proper way to do this, so I put here my own answer for future searchers:

  • https://docs.ruby-lang.org/en/3.0.0/Vector.html

For example:

v1 = Vector[1, 1]
v2 = Vector[2, 2]
distance = (v1 - v2).magnitude
=> 1.4142135623730951
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!