已知圆上两点坐标和半径求圆心

牛客题目链接

已知两点坐标可求出直线方程,当然也包括斜率了,已知斜率tan a,最近刚学的高数上,可根据三角函数的反函数(C++ atan2(y,x))求出角度(小心为double类型),用到圆心到两点中点这条边可求出圆心。

/**遇到此类型的题目就是先确定圆心再去判断其他店是否在圆上或者圆内**/
  
#include <map>
#include <queue>
#include <string>
#include<iostream>
#include<stdio.h>
#include<string.h>
#include <algorithm>
#include <math.h>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
typedef pair<ll,ll> pii;
const int maxn=2e5+1010;
#define inf 0x3f3f3f3f
#define sf scanf
#define pf printf
const int mod=998244353;
const int MOD=10007;
const double eps=1e-6;

ll r,num;

struct Point
{
	double x,y;
	Point(){}
	Point(double tx,double ty){x=tx;y=ty;}
}p[200];

double dist(Point p1,Point p2)
{
	return sqrt(pow(p1.x-p2.x,2)+pow(p1.y-p2.y,2));///求出两点距离
}

Point Center(Point p1,Point p2)
{
	Point mid = Point((p1.x+p2.x)/2,(p1.y+p2.y)/2);///求得中点坐标
	double angle = atan2(p1.x-p2.x,p2.y-p1.y); ///求出极坐标
	double d = sqrt(r*r-pow(dist(p1,mid),2));///求出侧边也就是圆心到其中点的距离
	return Point(mid.x-d*cos(angle),mid.y-d*sin(angle));///求出圆心坐标
}


int main()
{
		cin>>r;
		cin>>num;
		for(int i=0;i<num;i++)
			cin>>p[i].x>>p[i].y;
		ll ans = 1;
		for(int i=0;i<num;i++)
		{
			for(int j=i+1;j<num;j++)
			{
				if(dist(p[i],p[j]) > 2.0*r) continue;///两点距离比直径大直接退出
				Point center = Center(p[i],p[j]);///求出圆心的
				ll cnt = 0;
				for(int k=0;k<num;k++)
					if(dist(center,p[k]) < 1.0*r+eps) cnt++;///求这个求出来的圆心点到个点的距离是否瞒住条件
				ans = max(ans,cnt);///求出最大值
			}
		}
		cout<<ans<<endl;
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是用 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` 函数,并输出圆心半径的值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值