Project

General

Profile

Actions

Feature #10442

closed

Helper methods to Vector class

Added by Clamoris (Egunov Dmitriy) over 9 years ago. Updated over 9 years ago.

Status:
Closed
Target version:
-
[ruby-core:65947]

Description

Vector class missed some very helpful methods like angle between another vector and vector projection.

Patch attached. Please advice if more unit tests are needed.


Files

vector_helpers.patch (1.44 KB) vector_helpers.patch Clamoris (Egunov Dmitriy), 10/28/2014 02:05 PM
angle_with.patch (1.12 KB) angle_with.patch Clamoris (Egunov Dmitriy), 10/29/2014 06:58 PM
angle_with.patch (1.59 KB) angle_with.patch Clamoris (Egunov Dmitriy), 10/30/2014 09:21 PM

Updated by marcandre (Marc-Andre Lafortune) over 9 years ago

  • Category set to lib
  • Assignee set to marcandre (Marc-Andre Lafortune)

Updated by marcandre (Marc-Andre Lafortune) over 9 years ago

Thanks for the feature request(s).

I think angle_between could be useful. The patch needs work though:

  • should check that given argument is a Vector
  • should calling self when not necessary
  • should avoid create intermediary vectors when not necessary for performance reasons
  • the doc should specify the return to be in [0...Math::PI]
  • I feel that angle_with would be a more appropriate name

I am less confident about the projection_to. It's really the norm of the projection, so maybe a name like projected_norm would be clearer, but I'm curious if this would be used much. Are there applications that use this? If so, please open a separate feature request with details on what kind of applications would use this.

I'll give you a couple of days if you'd like to fix the patch for angle_with, otherwise I'll fix it myself and commit it, up to you :-)

Thanks

Actions #3

Updated by marcandre (Marc-Andre Lafortune) over 9 years ago

Marc-Andre Lafortune wrote:

Thanks for the feature request(s).

I think angle_between could be useful. The patch needs work though:

  • should check that given argument is a Vector
  • should avoid calling self when not necessary
  • should avoid create intermediary vectors when not necessary for performance reasons
  • the doc should specify the return to be in [0...Math::PI]
  • I feel that angle_with would be a more appropriate name

I am less confident about the projection_to. It's really the norm of the projection, so maybe a name like projected_norm would be clearer, but I'm curious if this would be used much. Are there applications that use this? If so, please open a separate feature request with details on what kind of applications would use this.

I'll give you a couple of days if you'd like to fix the patch for angle_with, otherwise I'll fix it myself and commit it, up to you :-)

Thanks

Updated by Clamoris (Egunov Dmitriy) over 9 years ago

Thanks, Marc-Andre Lafortune.

Patch updated (in attach), please take a look.

Updated by gogotanaka (Kazuki Tanaka) over 9 years ago

@Egunov Dmitriy

I like this idea, and it looks fine for me.

It may too trivial things but I want more tests and to make it more gracefully.
(1.5707963267948966 is little bit not clear)

Like that.

 assert_in_epsilon(Math::PI/2, Vector[1, 0].angle_with(Vector[0, 1]))
 assert_in_epsilon(Math::PI/4, Vector[1, 1].angle_with(Vector[0, 1]))

Anyway, I respect for your work. thanks.

Updated by marcandre (Marc-Andre Lafortune) over 9 years ago

  • Status changed from Open to Closed

Thanks for the revised patch!

I've committed it almost as is (I changed the constant in the doc for Math::PI / 2) and made a slight optimization

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0