SGU 414 圆的正交

本文介绍了一种计算两个正交圆圆心的方法,并通过构造数学直线来确定唯一圆心,同时讨论了特殊情况下的处理方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
const int maxn = 1E5 + 10;
int kase,  T, n;
struct Point
{
	double x, y;
	Point(double x = 0, double y = 0): x(x), y(y) {}
};
typedef Point Vector;
typedef vector<Point> Polygon;
Vector operator +(Vector A, Vector B)//
{
	return Vector(A.x + B.x, A.y + B.y);
}
Vector operator -(Point A, Point B)//
{
	return Vector(A.x - B.x , A.y - B.y);
}
Vector operator *(Vector A, double p)//
{
	return Vector(A.x * p, A.y * p);
}
Vector operator /(Vector A, double p)//
{
	return Vector(A.x / p, A.y / p);
}
bool operator <(const Point &a, const Point &b)//
{
	return a.x < b.x || (a.x == b.x && a.y < b.y);
}
const double eps = 1e-4;
int dcmp(double x)//
{
	if (fabs(x) < eps) return 0;
	else return x < 0 ? -1 : 1;
}
bool operator ==(const Point &a, const Point &b)//
{
	return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;
}
double Dot(Vector A, Vector B)//
{
	return A.x * B.x + A.y * B.y;
}
double Length(Vector A)//
{
	return sqrt(Dot(A, A));
}
double Cross(Vector A, Vector B)//
{
	return A.x * B.y - A.y * B.x;
}
double Angle(Vector A, Vector B)//
{
	return acos(Dot(A, B) / Length(A) / Length(B));
}
double Distance(Point A, Point B)
{
	return sqrt((A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y));
}
double Distance2(Point A, Point B)
{
	return (A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y);
}
struct Circle
{
	Point c;
	double r;
	Circle() {};
	Circle(Point c, double r): c(c), r(r) {}
	bool operator < (const Circle &other) const
	{
		if (dcmp(c.x - other.c.x) != 0) return c.x < other.c.x;
		else if (dcmp(c.y - other.c.y) != 0) return c.y < other.c.y;
		else return r < other.r;
	}
	bool operator == (const Circle &other) const
	{
		if (dcmp(c.x - other.c.x) == 0 && dcmp(c.y - other.c.y) == 0 && dcmp(r - other.r) == 0) return 1;
		else return 0;
	}
	Point point(double a)
	{
		return Point(c.x + cos(a) * r, c.y + sin(a) * r);
	}
	bool OnCircle(Point A)
	{
		if (dcmp(Distance(c, A) - r) == 0) return 1;
		else return 0;
	}
};
Circle c[maxn];
Circle read_circle()
{
	double X, Y, R;
	scanf("%lf%lf%lf", &X, &Y, &R);
	return Circle(Point(X, Y), R);
}
struct Math_Line
{
	double A, B, C;
	Math_Line(Point a, Point b)
	{
		if (dcmp(a.x - b.x) == 0) {A = 1, B = 0, C = -1 * a.x;}
		else
		{
			A = b.y - a.y;
			B = a.x - b.x;
			C = a.x * (a.y - b.y) + a.y;
		}
	}
	Math_Line(Circle a, Circle b)
	{
		A = (b.c.x - a.c.x) * 2;
		B = (b.c.y - a.c.y) * 2;
		C = a.c.x * a.c.x - b.c.x * b.c.x + a.c.y * a.c.y - b.c.y * b.c.y - a.r * a.r + b.r * b.r;
	}
	Math_Line(double A = 0, double B = 0, double C = 0): A(A), B(B), C(C) {}
	int Line_State(const Math_Line &other) const
	{
		if (dcmp(A * other.B - B * other.A) == 0)
		{
			if (dcmp(C * other.B - B * other.C) == 0 && dcmp(C * other.A - A * other.C) == 0)
				return 2;// 重合
			else return 0;//平行
		}
		else return 1;//相交
	}
	Point GetLineIntersection(const Math_Line &other) const
	{
		if (dcmp(B) == 0) return Point(-C / A, (other.A * (-C / A) + other.C) / (-other.B));
		else if (dcmp(other.B) == 0) return Point(-other.C / other.A, (A * (-other.C / other.A) + C) / (-B));
		else return Point((other.C * B - C * other.B) / (A * other.B - other.A * B), (A * ((other.C * B - C * other.B) / (A * other.B - other.A * B)) + C) / (-B));
	}
};
Circle Center;
int solve()
{
	int ans = 0;
	for (int i = 2; i <= n; i++)
		if (c[i].c == c[i - 1].c) return -1;
	if (n <= 2) return -2;
	else
	{
		Math_Line l1(c[1], c[2]);
		bool first = 1; Point pre;
		for (int i = 2; i < n; i++)
		{
			int k = l1.Line_State(Math_Line(c[i], c[i + 1]));
			if (k == 0) return -1;
			else if (k == 1)
				if (first)
				{
					first = 0;
					pre = l1.GetLineIntersection(Math_Line(c[i], c[i + 1]));
					double R = (Distance2(pre, c[1].c) - c[1].r * c[1].r);
					if (dcmp(R) <= 0) return -1;
					Center = Circle(pre, sqrt(R));
				}
				else if (!(l1.GetLineIntersection(Math_Line(c[i], c[i + 1])) == pre)) return -1;
		}
		if (first == 0) return 1;
		else return -2;
	}
}
int main(int argc, char const *argv[])
{
	while (~scanf("%d", &n) && n)
	{
		for (int i = 1; i <= n; i++)
			c[i] = read_circle();
		sort(c + 1, c + n + 1);
		n = unique(c + 1, c + n + 1) - (c + 1);
		int ans = solve();
		if (ans < 0) printf("%d\n", ans);
		else printf("%.10lf %.10lf %.10lf\n", Center.c.x, Center.c.y, Center.r);
	}
	return 0;
}


两个圆的正交圆圆心在直线上,满足到两圆心距离之差等于半径之差。

特殊情况:两圆重合,只计算一个即可,两元同心,必然无解。

那么两条不重合直线就可以确定唯一的圆心,去计算R是否相同即可。

你问我所有直线都重合怎么办?实际只有一条直线,那么就是有多解。

对负数开方居然导致presentation error,令我十分吃惊。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值