poj3675 求圆与多边形相交的面积_matlab 直线和多边形的交点 intersectwithline-程序员宅基地

技术标签: 计算几何  二维点线面  

每条线段与原点连起来求在圆内的有向面积,然后输出之前绝对值一下。

求三角形在圆内的面积,都在圆内,直接三角形面积,都在圆外且无交点,直接扇形面积,如果一点在圆内一点在圆外的话,求一个扇形一个三角形,两点在圆外但有2个交点,则求2个扇形中间一个三角形的面积。

使用点积解方程求线段与圆的交点过不了这题,不知道为何,但用上交小红书的解方程模板就可以

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#define maxl 510
#define eps 1e-10
const double pi=acos(-1.0);
using namespace std;

inline int sgn(double x)
{
	if(x>-eps && x<eps) return 0;
	if(x>0) return 1;
	else 	return -1;
}
inline double sqr(double x)
{
	return x*x;
}

struct point
{
	double x,y;
	point(double a=0,double b=0)
	{
		x=a;y=b;
	}
	point operator + (const point &b)const
	{
		return point(x+b.x,y+b.y);
	}
	point operator - (const point &b)const
	{
		return point(x-b.x,y-b.y);
	}
	friend point operator * (const double t,const point &p)
	{
		return point(t*p.x,t*p.y);
	}
	inline double norm()
	{
		return sqrt(x*x+y*y);
	}
	inline void transxy(double b)//逆时针旋转b弧度
	{
		double tx=x,ty=y;
		x=tx*cos(b)-ty*sin(b);
		y=tx*sin(b)+ty*cos(b);
	}
	inline void transxy(double cosb,double sinb)
	{
		double tx=x,ty=y;
		x=tx*cosb-ty*sinb;
		y=tx*sinb+ty*cosb;
	}
};
inline double dot(const point &a,const point &b)
{
	return a.x*b.x+a.y*b.y;
}
inline double det(const point &a,const point &b)
{
	return a.x*b.y-a.y*b.x;
}
inline double dist(const point &a,const point &b)
{
	return (b-a).norm();
}
struct line
{
	point s,e;
	double k;
	line(point a=point(),point b=point())
	{
		s=a;e=b;
		k=atan2(e.y-s.y,e.x-s.x);
	}
};
inline bool point_on_seg(point &p,line &a)
{
	return sgn(det(p-a.s,p-a.e))==0 && sgn(dot(p-a.s,p-a.e))<=0;
}
inline double mysqrt(double x)
{
	return sqrt(max(x,0.0));
}
/*inline void circle_cross_line(line L,point o,double r,point ret[],int &num)
{
	point v=L.e-L.s;
	double t=dot(o-L.s,v)/dot(v,v);
	point rt=L.s+t*v;
	double dis=dist(rt,o);
	num=0; 
	if(sgn(dis-r)>0) 
		return;
	if(sgn(dis-r)==0)
	{
		if(point_on_seg(rt,L)) ret[num++]=rt;
		return;
	}
	double cosval=dis/r,sinval=mysqrt(1-cosval*cosval),sita;
	if(cosval<-1.0)
		cosval+=eps;
	if(cosval>1.0)
		cosval-=eps; 
	sita=acos(cosval);
	point p1=rt,p2=rt;
	//p1.transxy(cosval,sinval);p2.transxy(cosval,-sinval);
	p1.transxy(sita);p2.transxy(-sita);
	p1=(r/dis)*p1;p2=(r/dis)*p2;
	if(point_on_seg(p1,L)) 
		ret[num++]=p1;
	if(point_on_seg(p2,L))
		ret[num++]=p2;
}*/

inline void circle_cross_line(line L,point o,double r,point ret[],int &num)
{
	point a=L.s;point b=L.e;
	double x0=o.x,y0=o.y;
	double x1=a.x,y1=a.y;
	double x2=b.x,y2=b.y;
	double dx=x2-x1,dy=y2-y1;
	double A=dx*dx+dy*dy;
	double B=2*dx*(x1-x0)+2*dy*(y1-y0);
	double C=sqr(x1-x0)+sqr(y1-y0)-sqr(r);
	double delta=B*B-4*A*C;
	num=0;
 	if(sgn(delta)>=0)
    {
		double t1=(-B-mysqrt(delta))/(2*A);
		double t2=(-B+mysqrt(delta))/(2*A);
     	if(sgn(t1-1)<=0&&sgn(t1)>=0)
        	ret[num++]=point(x1+t1*dx,y1+t1*dy);
    	if(sgn(t2-1)<=0&&sgn(t2)>=0)
        	ret[num++]=point(x1+t2*dx,y1+t2*dy);
    }
}

double R,ans;
int n;
point a[maxl];

inline void prework()
{
	scanf("%d",&n);
	for(int i=0;i<n;i++)
		scanf("%lf%lf",&a[i].x,&a[i].y);
	a[n]=a[0];
}

double sector_area(const point &a,const point &b)
{
	double theta=atan2(a.y,a.x)-atan2(b.y,b.x);
	while(theta<=0) theta+=2*pi;
	while(theta>2*pi) theta-=2*pi;
	theta=min(theta,2*pi-theta);
	return R*R*theta/2;
}

inline double calc(const point &a,const point &b)
{
	point p[2];int num=0;
	bool ina=sgn(sqrt(dot(a,a))-R)<0,inb=sgn(sqrt(dot(b,b))-R)<0;
	if(ina)
	{
		if(inb) return fabs(det(a,b))/2;
		else
		{
			circle_cross_line(line(a,b),point(0,0),R,p,num);
            return fabs(sector_area(b,p[0]))+fabs(det(a,p[0]))/2.0;
		}
	}
	else
	{
		if(inb)
		{
			circle_cross_line(line(a,b),point(0,0),R,p,num);
			return sector_area(p[0],a)+fabs(det(p[0],b))/2;
		}
		else
		{
			circle_cross_line(line(a,b),point(0,0),R,p,num);
			if(num==2)
				return sector_area(a,p[0])+sector_area(p[1],b)+fabs(det(p[0],p[1]))/2.0;
			else
				return sector_area(a,b);
		}
	}
}

inline double area()
{
	double res=0;
	for(int i=0;i<n;i++)
	{
		int f=sgn(det(a[i],a[i+1]));
		if(f!=0)
			res+=f*(calc(a[i],a[i+1]));
	}
	return res;
}

inline void mainwork()
{
	ans=fabs(area())+eps;
}

inline void print()
{
	printf("%.2f\n",ans);
}

int main()
{
	while(~scanf("%lf",&R))
	{
		prework();
		mainwork();
		print();
	}	
	return 0;
}

 

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/liufengwei1/article/details/87821200

智能推荐

fine tune chatgpt_finetune embed chatgpt-程序员宅基地

文章浏览阅读2.1k次。fine tune openAI model ( 微调chatgpt)_finetune embed chatgpt

在stable diffusion中遇到以下问题应该如何解决“当前加载的模型是一个LORA,而不是一个稳定扩散检查点。RuntimeError:”_runtimeerror: the model currently loading is a lor-程序员宅基地

文章浏览阅读414次,点赞7次,收藏2次。如果上述步骤都不能解决您的问题,您可能需要更详细地描述您的代码和错误信息,以便于他人提供更具体的帮助。在描述问题时,请确保提供足够的细节,包括错误消息的完整内容、您使用的代码片段、以及您已经尝试过的解决方法。这可能意味着您尝试使用的模型或权重与您的代码或任务不兼容。如果您是从预训练模型开始,请确保您下载的是稳定扩散模型的权重,而不是其他类型的模型权重。检查您的代码中加载模型的部分,确保您使用了正确的类和方法来加载稳定扩散模型。确认您加载的模型文件确实是稳定扩散模型,而不是LORA模型。_runtimeerror: the model currently loading is a lora instead of a stable diff

error: macro "toLine" requires 2 arguments, but only 1 given      QLine toLine() const;_macro "_min" requires 2 arguments, but only 1 give-程序员宅基地

文章浏览阅读2.6k次。/include/qt/QtCore/qvariant.h:307:18: error: macro "toLine" requires 2 arguments, but only 1 given QLine toLine() const; ^include/qt/QtCore/qvariant.h:307:11: error: expected ‘..._macro "_min" requires 2 arguments, but only 1 given

bzoj 3479: [Usaco2014 Mar]Watering the Fields 最小生成树_usaco 2014 mar watering the fields-程序员宅基地

文章浏览阅读286次。→题目链接←裸的最小生成树...只会prim算法n^2暴力建边然后跑最小生成树学了这么长的时间终于第一次写最小生成树真棒啊代码:#include#include#include#include#includeusing namespace std;struct node{ int s,t; int len; node(int x,i_usaco 2014 mar watering the fields

基于SpringBoot的人事管理系统_基于springboot人事管理系统-程序员宅基地

文章浏览阅读2.7k次,点赞2次,收藏40次。系统主要分为管理员和普通用户和员工三部分,主要功能包括个人中心,普通用户管理,员工管理,人事档案管理,部门管理,薪酬管理,人事调动管理,职务管理,培训管理,招聘信息管理,求职简历管理,邀请面试管理,录用信息管理,员工应聘管理,系统管理等功能。管理员登录进入人事管理系统可以查看个人中心,普通用户管理,员工管理,人事档案管理,部门管理,薪酬管理,人事调动管理,职务管理,培训管理,招聘信息管理,求职简历管理,邀请面试管理,录用信息管理,员工应聘管理,系统管理等功能等内容。..._基于springboot人事管理系统

JAVA 解析未知JSON并获得key,value,value的类型_java解析未知json-程序员宅基地

文章浏览阅读8.4k次,点赞2次,收藏6次。try { JSONObject object=new JSONObject("{\"showid\":\"38f5ef6ae35711e0a046\", \n" + " \"showname\":\"\\u996d\\u5c40\\u4e5f\\u75af\\u72c2\", \n" + _java解析未知json

随便推点

ImportError: cannot import name Template解决方案_importerror: cannot import name 'prompttemplate' f-程序员宅基地

文章浏览阅读6.2w次,点赞12次,收藏11次。本文主要介绍了ImportError: cannot import name Template解决方案,希望能对学习python的同学们有所帮助。文章目录1. 问题描述2. 原因分析3. 解决方案_importerror: cannot import name 'prompttemplate' from 'langchain.prompts.cha

Laravel随笔 Windows下Redis安装/php安装redis扩展_windows laravel 8使用 phpredis 扩展来连接 redis-程序员宅基地

文章浏览阅读164次。下载redis windows版 https://github.com/microsoftarchive/redis/releases (msi版本可以一键安装)PHP安装redis扩展 https://www.cnblogs.com/enjie/p/7978879.htmlPHP安装igbinary扩展 https://pecl.php.net/package/igbinary/3.1...._windows laravel 8使用 phpredis 扩展来连接 redis

同声传译免费软件app哪个好?让国际交流变得轻松又有趣-程序员宅基地

文章浏览阅读832次,点赞14次,收藏18次。软件能够实现几乎无延迟的语音翻译,让我们能够即时理解对方的意图并作出快速的回应。这一过程中,翻译结果的呈现流畅且准确,为我们提供了近乎无缝的跨语言交流体验。DeepL Translator的翻译结果准确度高,能够准确地把握原文的含义,避免了歧义和误解的产生。它拥有直观简洁的界面,操作简单方便。它采用了先进的语音识别和机器翻译技术,结合大量的语料库进行训练,确保翻译结果的准确性和流畅性。

一文详解opencv摄像头数字识别_摄像头数字识别在其他场景下识别不了-程序员宅基地

文章浏览阅读1.4w次,点赞32次,收藏286次。本文的目标是实现识别摄像头图像中的数字。实际应用场景包括 车牌号识别 ,部分竞赛的 A4纸打印数字识别 。摄像头数字识别分为两个步骤:1. 提取图像中的ROI区域,如截取车牌的矩形区域,或截取A4纸的图像。2. 对ROI区域进行数字识别。数字识别相对来说较为简单,先介绍数字识别的方法和原理。_摄像头数字识别在其他场景下识别不了

TCP三次握手四次挥手详解_tcp三次挥手四次挥手-程序员宅基地

文章浏览阅读2k次。三次握手和四次挥手的总结_tcp三次挥手四次挥手

域账户信息导出脚本_Facebook OAuth漏洞导致的Facebook账户劫持-程序员宅基地

文章浏览阅读2k次。平时在用“Login with Facebook”功能进行跳转登录时,因为其用到了多个URL重定向跳转,所以总会给我有一种不安全的感觉。但是,要想发现Facebook漏洞,并非易事,需要莫大的功夫和精力,更别说涉及登录的Facebook OAuth了,这更是难上加难。然而,我就发现了Facebook OAuth这么一个漏洞,获得了Facebook官方$55,000的奖励。就是这么一个漏洞...

推荐文章

热门文章

相关标签