Constrain coordinates of a point into a circle

438 Views Asked by At

I have a point A, which has coordinates (x, y), and a circle which center C coordinates are (x0, y0) and radius is R.

So I want to calculate the coordinates (a,b) of a point B that would be point A if A is in the circle, and else, it would be the intersection between the circle and the line from C to A. scheme

I'm currently calculating the signed angle $\alpha$ between the CA vector and the (1,0) vector, and if $cos(\alpha)*(x-x0) > R$ then $a = cos(\alpha)*R$ ; same for y : if $sin(\alpha)*(y-y0) > R$ then $b = sin(\alpha)*R$

But I intend to code this later, so this formula can be realy heavy (and I'm not even sure it works, I didn't try yet), so is there a simpler way to calculate B coordinates?

1

There are 1 best solutions below

1
On BEST ANSWER

If $C=0$, you can first verify if $|A| \leq R$, in which case you set $B=A$, and otherwise you set $B=A \cdot \frac{R}{|A|}$. For $C \neq 0$, it should not be difficult to generalize.