Comments and answers for "Finding a tangent vector from a given point and circle"
http://answers.unity.com/questions/1617078/finding-a-tangent-vector-from-a-given-point-and-ci.html
The latest comments and answers for the question "Finding a tangent vector from a given point and circle"Comment by elenzil on elenzil's answer
http://answers.unity.com/comments/1617321/view.html
bonus: tangents to a parabola are also in there.Fri, 29 Mar 2019 19:37:37 GMTelenzilAnswer by elenzil
http://answers.unity.com/answers/1617317/view.html
oh hey, i know this.
[live unity webgl demo here][1].
[source code here][2].
core routine:
// this approach is more geometrical and less algebraic than approach 1,
// and far more stable. thanks to Mike Plotz for suggesting this direction.
bool CircleTangents_2(Vector2 center, float r, Vector2 p, ref Vector2 tanPosA, ref Vector2 tanPosB) {
p -= center;
float P = p.magnitude;
// if p is inside the circle, there ain't no tangents.
if (P <= r) {
return false;
}
float a = r * r / P;
float q = r * (float)System.Math.Sqrt((P * P) - (r * r)) / P;
Vector2 pN = p / P;
Vector2 pNP = new Vector2(-pN.y, pN.x);
Vector2 va = pN * a;
tanPosA = va + pNP * q;
tanPosB = va - pNP * q;
tanPosA += center;
tanPosB += center;
return true;
}
and finally a picture:
![alt text][3]
[1]: https://elenzil.github.io/tangents/
[2]: https://github.com/elenzil/tangents/blob/master/tangents/Assets/Scripts/TangentCtlr.cs
[3]: /storage/temp/135547-circle-tangent-elenzil.jpgFri, 29 Mar 2019 19:21:50 GMTelenzil