从圆心点出发的射线上的一点,计算射线与圆的交点
已知圆心点、圆的半径与圆心点出发的射线上的一点,求射线与圆的交点(根据比例计算与圆的交点):
typedef struct
{
double x, y;
} Point;
//计算两点间的距离
double CalcPointDistance(const double &startX, const double &startY, const double &endX, const double &endY)
{
double dx = endX - startX;
double dy = endY - startY;
return sqrt(dx * dx + dy * dy);
}
//计算交点:圆半径、圆中心点坐标,射线上一点的坐标
Point IntersectionPoint(double dRadius, Point CentrePoint, Point RayPoint)
{
//计算射线上的点到圆心的长度
double dTotalLength = CalcPointDistance(RayPoint.x, RayPoint.y, CentrePoint.x, CentrePoint.y);
//根据比例计算与圆的交点 (相似三角形)
Point TarPoint;
TarPoint.x = (RayPoint.x - CentrePoint.x) * (dRadius / dTotalLength) + CentrePoint.x;
TarPoint.y = (RayPoint.y - CentrePoint.y) * (dRadius / dTotalLength) + CentrePoint.y;
return TarPoint;
}