算法逻辑很简单循环遍历折线线段,获取圆与线段的交点。
typedef CArray<CPoint, CPoint> POINT3DLIST;
// 获取圆与折线交点
// 输入参数 CenterPt 圆心,r 半径,line 折线
// 输出参数 CrossPs 圆与折线交点
// 返回交点个数
int GetCircLineCross(const CPoint& CenterPt, double r, POINT3DLIST& line, POINT3DLIST& CrossPs)
{
if(r<0.01)
return 0;
for(int i=1; i<line.GetSize(); i++)
{
CPoint sp0=line[i-1];
CPoint sp1=line[i];
double dist0=Distance(CenterPt, sp0);
double dist1=Distance(CenterPt, sp1);
if(dist0>dist1)
{
swap(dist0, dist1);
swap(sp0, sp1);
}
// 线段在圆内
if(dist1<r)
continue;
CPoint PerpendPt;
int nResRelation;
double dist=DistPerpend(CenterPt, sp0, sp1, &PerpendPt, &nResRelation);
// 线段超出圆的范围
if(dist>r || (dist0>r && nResRelation))
continue;
double dExtended=sqrt(pow(r,2)-pow(dist,2));
CPoint crossPt;
if(dist0>r)
{
crossPt