Point::getAngleToPoint bug?

    /** Returns the angle from this point to another one.

        Taking this point to be the centre of a circle, and the other point being a position on
        the circumference, the return value is the number of radians clockwise from the 12 o'clock
        So 12 o'clock = 0, 3 o'clock = Pi/2, 6 o'clock = Pi, 9 o'clock = -Pi/2
    FloatType getAngleToPoint (Point other) const noexcept
        return static_cast<FloatType> (std::atan2 (static_cast<FloatType> (other.x - x),
                                                   static_cast<FloatType> (y - other.y)));

yet the docs for std::atan2 show that it wants (y, x):

Am I missing something??

It’s intended that way. In math you usually use the x axis for an angle of zero, y for pi/2 and so on. However, JUCE chose another convention which is perfectly fine. Especially as it is documented :slight_smile:

I must not be getting it. I’m feeding it the following coordinates: start (5, 3.66) end (8, 1) and I’m getting back an angle of 48 degrees or so.

why am I not getting back a value between 90 and 180, like the drawing clearly shows?

It depends what coordinate system you’re working with. Points are usually associated with positions of things you are drawing to the screen, and getAngleToPoint would work as expected if you were calculating an angle between two Points in a paint() callback.

so not for something like the angles of the green and orange segments:


If you were drawing that to the screen using Lines then that would you give the correct answer - your start and end points (in screen positions) would be something like (5, 1.2), (8, 3.86).

Or, if you wanted to use that function with regular Cartesian axes you can simply flip the y-axis of all your points. So rather than 0 -> 4 you go 0 -> -4 (but you can still treat it as positive when you’re drawing it on paper).

1 Like