一图胜千言
基础知识,三角函数,反三角函数,点绕点旋转,可以看到斜率为0的情况也是进行了处理的,在运动中也没有任何BUG。当然我会放上一张图帮助大家理解。
核心代码
PointF r2_low_point = new PointF();
PointF r2_up_point = new PointF();
PointF r1_up_point = new PointF();
PointF r1_low_point = new PointF();
public void getTangentPoint(double a, double b, double r1, double c, double d, double r2)
{
double α =Math.Atan2((b - d) , (a - c));
double centerLine;
if (d == b)
{
if (a < c)
{
centerLine = a - c;
}
else
{
centerLine = c-a;
}
}
else
{
centerLine = (d - b) / Math.Sin(α);
}
double β = Math.Acos((r1 + r2) / centerLine);
double θ = β - α;
double r2LowX = r2 * Math.Cos(θ);
double r2LowY = r2 * Math.Sin(θ);
double r1UpX = r1 * Math.Cos(θ);
double r1UpY = r1 * Math.Sin(θ);
r2_low_point.X = centerOfCircle2.X - (float)r2LowX;
r2_low_point.Y = centerOfCircle2.Y + (float)r2LowY;
r1_up_point.X = centerOfCircle1.X + (float)r1UpX;
r1_up_point.Y = centerOfCircle1.Y - (float)r1UpY;
PointF r2_tempoint= Transition( r2_low_point.X- centerOfCircle2.X, r2_low_point.Y-centerOfCircle2.Y, 2 * β, 1);
r2_up_point.X = centerOfCircle2.X + r2_tempoint.X;
r2_up_point.Y = centerOfCircle2.Y + r2_tempoint.Y;
PointF r1_tempoint = Transition(r1_up_point.X - centerOfCircle1.X, r1_up_point.Y-centerOfCircle1.Y, 2 * β, 1);
r1_low_point.X = centerOfCircle1.X + r1_tempoint.X;
r1_low_point.Y = centerOfCircle1.Y + r1_tempoint.Y;
}
private PointF Transition(float _x, float _y, double _angle, int fx)
{
double newX = 0;
double newY = 0;
if (fx == 1)
{
newX = _x * Math.Cos(_angle) - _y * Math.Sin(_angle);
newY = _x * Math.Sin(_angle) + _y * Math.Cos(_angle);
}
else
{
newX = _x * Math.Cos(_angle) + _y * Math.Sin(_angle);
newY = _y * Math.Cos(_angle) - _x * Math.Sin(_angle);
}
return new PointF((float)newX, (float)newY);
}