How to define an origin for atan2()

The p5js page for atan2 states that it works in the following way:

Calculates the angle (in radians) from a specified point to the coordinate origin as measured from the positive x-axis.

However, I would like to calculate the angle between a set of points other than the origin. Is there any way to do this, and if so, can I get a pointer towards the right track? Thanks in advance! :)

Answers

  • Answer ✓

    I figured out solving my problem rather quickly. Reading the documentation all the way really helps :P

  • can you share your result briefly please?

  • edited March 2018

    I realized that I could use atan2() in the form of atan2(y2 - y1, x2 - x1). I then applied that to a sprite rotation (p5.play.js) by converting the radians angle to degrees (since atan2() returns an angle in radians, and p5.play's rotation method needs it in degrees). This got me my final code:

    p.rotation = degrees(atan2(camera.mouseY - p.position.y, camera.mouseX - p.position.x));
    

    I probably would have ended up asking another pointless question had I not noticed that p5.play takes the rotation in degrees, while atan2 returns an angle in radians :P

Sign In or Register to comment.