已知圆心和半径获取圆与折线的交点C++算法实现

该博客介绍了如何使用C++来计算已知圆心和半径的情况下,圆与折线的交点。算法核心是遍历折线的每个线段,找出与圆相交的点。
摘要由CSDN通过智能技术生成

算法逻辑很简单循环遍历折线线段,获取圆与线段的交点。

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
本程序是两相交求交点,输入信息为两圆心坐标半径值。如有更好的算法,欢迎交流!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
以下是用 C++ 实现已知上三点求圆心半径的代码: ```cpp #include <iostream> #include <cmath> using namespace std; struct Point { double x, y; }; // 计算两点之间的距离 double distance(Point p1, Point p2) { return sqrt(pow(p1.x - p2.x, 2) + pow(p1.y - p2.y, 2)); } // 已知上三点求圆心半径 void circleFromPoints(Point p1, Point p2, Point p3, Point& center, double& radius) { double a = distance(p1, p2); double b = distance(p2, p3); double c = distance(p3, p1); double s = (a + b + c) / 2; double area = sqrt(s * (s - a) * (s - b) * (s - c)); double A = asin((p2.y - p1.y) / a); double B = asin((p3.y - p1.y) / c); double x = area * (cos(A) + cos(B)) / (sin(A) + sin(B)) / 2 + p1.x; double y = (p2.y - p1.y) / (p2.x - p1.x) * (x - (p1.x + p2.x) / 2) + (p1.y + p2.y) / 2; center.x = x; center.y = y; radius = distance(center, p1); } int main() { Point p1 = { 0, 0 }; Point p2 = { 1, 1 }; Point p3 = { 2, 0 }; Point center; double radius; circleFromPoints(p1, p2, p3, center, radius); cout << "Center: (" << center.x << ", " << center.y << ")" << endl; cout << "Radius: " << radius << endl; return 0; } ``` 在本例中,我们定义了一个 `Point` 结构体来表示点的坐标,并定义了一个 `distance` 函数来计算两点之间的距离。`circleFromPoints` 函数接收三个点作为参数,并计算圆心半径,结果保存在 `center` 和 `radius` 变量中。主函数中调用 `circleFromPoints` 函数,并输出圆心半径的值。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值