Find theta given point on circle

348 Views Asked by At

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

2

There are 2 best solutions below

6
On 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}$.

1
On

On the circle below, you have $A \equiv (x, y), \ B \equiv (0, 0), \ C \equiv (x, 0)$:

Circle on geogebra

You see the triangle $ABC$ is a right triangle since $\hat{ACB} = 90$°. You have thus those relations: \begin{equation} \sin \theta := \frac{|AC|}{|AB|} \end{equation} \begin{equation} \cos \theta := \frac{|BC|}{|AB|} \end{equation} \begin{equation} \tan \theta := \frac{\sin \theta}{\cos \theta} = \frac{|AC|}{|BC|} = \frac{y}{x} \end{equation} Your angle $\theta$ is thus: \begin{equation} \theta = \arctan{\left( \frac{y}{x} \right)} \ (x \neq 0) \end{equation} for $x = 0, \theta = 90$°.