Find corner radius of parallelogram based on mouse position. Some context: I'm making a round-corner parallelogram tool for FreeCAD.
The parallelogram rounded corners need to follow the mouse movement. i.e. its radius needs to update based on mouse position. So we need to calculate the radius $r$.

We have the distances $AB$ (=$x$), $AC$ (=$y$), angle $BAC$ (=$\alpha$)..
The circle is tangent to both lines.
The point $D$ is on the circle (on the arc of circle that is in the corner more precisely).
I have the formula r = x + y + sqrt(2 * x * y) which works only if the angle $BAC$ is 90° (i.e. if we have a rectangle (ps: sqrt is square root in c++)). But I can't find a general formula that works for any angle between 0° and 180°
Long story short, how do I to calculate the radius $r$?
Thanks
 A: We reproduce the figure given in the problem statement with the following extensions. Line $CD$ is extended to meet the radius $OE$ at $G$. The angle bisector $AO$ of the $\angle CAB$, the radius $OD$ and the perpendicular $DF$ from the point $D$ to the line $AB$ were also added to facilitate the derivation of a formula for the sought radius.

In the right-angled triangle $AEO$, we have,
$$AE=OE\cot\left(\dfrac{\alpha}{2}\right)= r\cot\left(\dfrac{\alpha}{2}\right).\tag{1}$$
Since $DFEG$ is a rectangle, using (1), we shall write,
$$DG=FE=AE-AF= r\cot\left(\dfrac{\alpha}{2}\right)-AB –BF= r\cot\left(\dfrac{\alpha}{2}\right)-x –y\cos\left(\alpha\right) \space\text{and} \tag{2}$$
$$GO=EO-EG=r-y\sin\left(\alpha\right).\qquad\qquad\qquad\qquad\qquad\qquad \qquad\qquad\qquad\quad\enspace\space\tag{3}$$
Using the expressions (2) and (3) derived above, we apply Pythagoras theorem to the right-angled triangle $DGO$ as shown below.
$$OD^2=r^2=DG^2+GO^2=\left[r\cot\left(\dfrac{\alpha}{2}\right)-x –y\cos\left(\alpha\right)\right]^2+\left(r-y\sin\left(\alpha\right)\right)^2.$$
This can be simplified to obtain the following quadratic equation in $r$.
$$r^2\cot\left(\dfrac{\alpha}{2}\right)-2r\left(x+y\right)+\left(x-y\right)^2\tan\left(\dfrac{\alpha}{2}\right)+2xy\sin\left(\alpha\right)=0\tag{4}$$
Its roots can be expressed as,
$$r=\dfrac{2\left(x+y\right)\pm 2\sqrt{\left(x+y\right)^2-\left[\left(x-y\right)^2\tan\left(\dfrac{\alpha}{2}\right)+2xy \sin\left(\alpha\right)\right] \cot\left(\dfrac{\alpha}{2}\right)}}{2\cot\left(\dfrac{\alpha}{2}\right)}.$$
When simplified, we have,
$$r_\text{4S}=\left[\left(x+y\right)-2\sqrt{xy}\sin\left(\dfrac{\alpha}{2}\right)\right]\tan\left(\dfrac{\alpha}{2}\right)\quad\text{and}\tag{4S}$$
$$r_\text{4L}=\left[\left(x+y\right)+ 2\sqrt{xy}\sin\left(\dfrac{\alpha}{2}\right)\right]\tan\left(\dfrac{\alpha}{2}\right).\qquad\space\tag{4L}$$
Note that we are using letters $\bf{S}$ and $\bf{L}$ to distinguish between the smaller and the larger root respectively in our formulae.
Using AM-GM inequality, $\left(x+y)\right)\ge 2\sqrt{xy}$, we can show that both the roots are positive. This puts it to our discretion whether we us the smaller or the larger root as the rounding-off radius. Please note that these values can be used only for rounding the two opposite corners, where the angle is $\alpha$.
We need to consider the roots of the following quadratic equation to determine the rounding-off radii of the other two corners, where the angle is $180^o-\alpha$.
$$r^2\cot\left(\dfrac{\alpha}{2}\right)-2r\left(x+y\right)+\left(x-y\right)^2\cot\left(\dfrac{\alpha}{2}\right)+2xy\sin\left(\alpha\right)=0\tag{5}$$
The two roots of this equation are,
$$r_\text{5S}=\left[\left(x+y\right)-2\sqrt{xy}\cos\left(\dfrac{\alpha}{2}\right)\right]\cot\left(\dfrac{\alpha}{2}\right)\quad\text{and}\tag{5S}$$
$$r_\text{5L} =\left[\left(x+y\right)+ 2\sqrt{xy}\cos\left(\dfrac{\alpha}{2}\right)\right]\cot\left(\dfrac{\alpha}{2}\right).\qquad\space\tag{5L}$$
For a given parallelogram, we have four choices of radii pairs, i.e. $\{r_{4\text{S}}, r_{5\text{S}}\}$, $\{r_{4\text{S}}, r_{5\text{L}}\}$, $\{r_{4\text{L}}, r_{5\text{S}}\}$, and $\{r_{4\text{L}}, r_{5\text{L}}\}$. It is difficult to state a guideline for the selection of the most appropriate radii pair.  However, the two pairs $\{r_{4\text{S}}, r_{5\text{S}}\}$ and $\{r_{4\text{L}}, r_{5\text{L}}\}$ are aesthetically pleasing. It can also be shown that each radii pair must comply with the following constrain to achieve a meaning full rounding-off of corners.
$$r_4\cot\left(\dfrac{\alpha}{2}\right)+r_5\tan\left(\dfrac{\alpha}{2}\right)\lt\space \text{Length of the shorter side of the parallelogram}$$
This constrain can also be expressed as,
$$\left(x+y\right)\pm\sqrt{xy}\left[\cos\left(\dfrac{\alpha}{2}\right)\pm\sin\left(\dfrac{\alpha}{2}\right)\right]\lt\space \dfrac{\text{Length of the shorter side of the parallelogram}}{2}.$$


A: Here is a hint and because it is too long, I write it as an answer. I am assuming your problem is 2D and you are trying to construct a round corner for a parallelogram. I am also assuming that you have access to a number of modules for calculations concerning perpendiculars as well as for solving systems of equations.
In addition to $r$ , you also need the coordinates of the center of the circle (let's call it $O$). Note that $O$ lies on the bisector of $\widehat{A}$ . You can find these 3 unknowns ($r$ and the 2 coordinates of $O$) by solving 3 independent equations:
(1) The distance between $O$ and one of the lines is $r$
(2) The distance between $O$ and the other line is $r$
(3) The distance between $O$ and $D$ is $r$.
