Skip to content

Implementation of PVector.setHeading() #193

Description

@processing-bot

Created by: arijit4

There's an method named PVector.setHeading() in p5.Vector class of p5.js to set the direction of an vector to any given angle. But there's no such option in Processing. It can be settled down easily. But the function seems more self explanatory and easy to understand. It also goes well with the PVector.heading() method already present in Processing.

An implementation might look like the following :

void setHeading(float angle) {
    this.rotate(angle - this.heading());
}

Activity

  1. processing-bot commented on Jun 14, 2021

    @processing-bot
    CollaboratorAuthor

    Created by: benfry

    Adding it for parity with p5.js… Here's the full implementation from there:

    _main.default.Vector.prototype.setHeading = function setHeading(a) {
      var m = this.mag();
      this.x = m * Math.cos(a);
      this.y = m * Math.sin(a);
      return this;
    };
  2. processing-bot commented on Jun 18, 2021

    @processing-bot
    CollaboratorAuthor

    Created by: arijit4

    @benfry In 673a19e, you added

    public PVector setHeading(float angle) { 
        float m = mag();
        x = m * Math.cos(a);
        y = m * Math.sin(a);
        return this;
     }

    shouldn't it be the following?

    public PVector setHeading(float angle) { 
        float m = mag();
        x = m * Math.cos(angle);
        y = m * Math.sin(angle);
        return this;
     }
  3. processing-bot commented on Jun 18, 2021

    @processing-bot
    CollaboratorAuthor

    Created by: benfry

    Yes, and it was immediately fixed in the next commit.

  4. processing-bot commented on Jun 18, 2021

    @processing-bot
    CollaboratorAuthor

    Created by: arijit4

    @benfry PVector.setHeding() isn't highlighted as other methods in PVector. But it works perfectly though...

  5. processing-bot commented on Jul 19, 2021

    @processing-bot
    CollaboratorAuthor

    Created by: github-actions[bot]

    This issue has been automatically locked. To avoid confusion with reports that have already been resolved, closed issues are automatically locked 30 days after the last comment. Please open a new issue for related bugs.

  6. locked as resolved and limited conversation to collaborators on Sep 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions