Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

272
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda