Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a makeOrthogonal method #27

Open
ttmarek opened this issue Mar 29, 2016 · 0 comments
Open

Adding a makeOrthogonal method #27

ttmarek opened this issue Mar 29, 2016 · 0 comments

Comments

@ttmarek
Copy link

ttmarek commented Mar 29, 2016

I've got a need for finding an orthogonal (perpendicular) vector of a certain magnitude in one of my projects.

I have a function that does the job, but I'd prefer to make it a part of Victor.js. Here's the function:

  /**
   * Returns a new vector thats orthogonal/perpendicular to the given
   * vector.
   * Mathematical rules:
   * - the dot product of two orthogonal vectors is zero.
   * - the magnitude of a 2d vector is the square root of the sum of
   *   its components.
   * - ^ two equations. components = the two unknowns.
   */
  getOrthogonal(vector, magnitude) {
    magnitude = magnitude || 1
    if (vector.x === 0) {
      return new Victor(magnitude, 0)
    }
    if (vector.y === 0) {
      return new Victor(0, magnitude)
    }
    const j = Math.sqrt((Math.pow(magnitude, 2) /
                         (1 + (Math.pow(vector.y, 2) /
                               Math.pow(vector.x, 2)))))
    const i = -(vector.y / vector.x) * j
    return new Victor(i, j)
  }

I was wondering if you would accept a merge request for a method that would work as follows:

var vec1 = new Victor(100, 200);
var vec2 = vec1.makeOrthogonal(10);
// vec 2 is a vector with a magnitude of 10 that's orthogonal to vec1 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant