Find theta given point on circle

anglecirclesrotations

I am having trouble visualizing and understanding how you might obtain an angle given a point on a circle. I have a $(x, y)$ point where the values range between $0,1$ for both $x,y$. How would I calculate the angle theta?

My confusion comes from a piece of code which is taking a random point and calculating theta and then using this theta to produce a rotation matrix to rotate a given direction.

I have a disk which is divided into $N$ directions. In this instance we have divided into $8$.
enter image description here

A single direction angle can be obtained by looping through the amount of directions and doing $ i * disk$ as shown in the code below. This will be the direction we would like to rotate. Below is implementation in GLSL

// Rotate direction
vec2 RotateDirectionAngle(vec2 direction, vec2 noise)
{
    float theta = noise.y * (2.0 * PI);
    float costheta = cos(theta);
    float sintheta = sin(theta);
    mat2 rotationMatrix = mat2(vec2(costheta, -sintheta), vec2(sintheta, costheta));

    return rotationMatrix * direction;
} 

int directions = 8;
disk = 2 * pi / directions

for(int i = 0; i < directions; i++)
{
    float samplingDirectionAngle = i * disk;
    vec2 rotatedDirection = RotateDirectionAngle(vec2(cos(samplingDirectionAngle), sin(samplingDirectionAngle)), noise.xy);
    
}

Sorry if this question is super basic but I'm finding it hard to visualize the calculations. Would appreciate any insight to help me better understand

Best Answer

In many languages, this is implemented as the atan2 function https://en.wikipedia.org/wiki/Atan2. Note that the arguments are swapped: atan2(y, x) returns the angle between the positive $x$ axis and the half line joining $(0,0)$ to $(x, y)$.

Mathematically, you can also use these formulas \begin{equation} r = \sqrt{x^2 + y^2}\qquad \theta = 2 \arctan\left(\frac{y}{r + x}\right) \quad\text{if }x\not= -r \end{equation} and $\theta=\pi$ when $x = -r$. This formula gives an angle in the interval $(-\pi, \pi]$.

It is not necessary to compute the angle to get the rotation matrix because $\cos\theta = \frac{x}{r}$ and $\sin\theta = \frac{y}{r}$.

Related Question