2016年湖南省第十二届大学生计算机程序设计竞赛 J 三角形和矩形(计算几何)
2016-09-03 21:53
501 查看
思路:直接套多边形面积交模板就可以了,半平面交也是可以做的,不过要注意向量的方向
#include<bits/stdc++.h>
using namespace std;
const int maxn=555;
const int maxisn=10;
const double eps=1e-8;
const double pi=acos(-1.0);
int dcmp(double x){
if(x>eps) return 1;
return x<-eps ? -1 : 0;
}
inline double Sqr(double x){
return x*x;
}
struct Point{
double x,y;
Point(){x=y=0;}
Point(double x,double y):x(x),y(y){};
friend Point operator + (const Point &a,const Point &b) {
return Point(a.x+b.x,a.y+b.y);
}
friend Point operator - (const Point &a,const Point &b) {
return Point(a.x-b.x,a.y-b.y);
}
friend bool operator == (const Point &a,const Point &b) {
return dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)==0;
}
friend Point operator * (const Point &a,const double &b) {
return Point(a.x*b,a.y*b);
}
friend Point operator * (const double &a,const Point &b) {
return Point(a*b.x,a*b.y);
}
friend Point operator / (const Point &a,const double &b) {
return Point(a.x/b,a.y/b);
}
friend bool operator < (const Point &a, const Point &b) {
return a.x < b.x || (a.x == b.x && a.y < b.y);
}
inline double dot(const Point &b)const{
return x*b.x+y*b.y;
}
inline double cross(const Point &b,const Point &c)const{
return (b.x-x)*(c.y-y)-(c.x-x)*(b.y-y);
}
};
Point LineCross(const Point &a,const Point &b,const Point &c,const Point &d){
double u=a.cross(b,c),v=b.cross(a,d);
return Point((c.x*v+d.x*u)/(u+v),(c.y*v+d.y*u)/(u+v));
}
double PolygonArea(Point p[],int n){
if(n<3) return 0.0;
double s=p[0].y*(p[n-1].x-p[1].x);
p
=p[0];
for(int i=1;i<n;i++){
s+=p[i].y*(p[i-1].x-p[i+1].x);
}
return fabs(s*0.5);
}
double CPIA(Point a[],Point b[],int na,int nb){
Point p[maxisn],temp[maxisn];
int i,j,tn,sflag,eflag;
a[na]=a[0],b[nb]=b[0];
memcpy(p,b,sizeof(Point)*(nb+1));
for(i=0;i<na&&nb>2;++i){
sflag=dcmp(a[i].cross(a[i+1],p[0]));
for(j=tn=0;j<nb;++j,sflag=eflag){
if(sflag>=0) temp[tn++]=p[j];
eflag=dcmp(a[i].cross(a[i+1],p[j+1]));
if((sflag^eflag)==-2)
temp[tn++]=LineCross(a[i],a[i+1],p[j],p[j+1]);
}
memcpy(p,temp,sizeof(Point)*tn);
nb=tn,p[nb]=p[0];
}
if(nb<3) return 0.0;
return PolygonArea(p,nb);
}
double SPIA(Point a[],Point b[],int na,int nb){
int i,j;
Point t1[4],t2[4];
double res=0.0,if_clock_t1,if_clock_t2;
a[na]=t1[0]=a[0];
b[nb]=t2[0]=b[0];
for(i=2;i<na;i++){
t1[1]=a[i-1],t1[2]=a[i];
if_clock_t1=dcmp(t1[0].cross(t1[1],t1[2]));
if(if_clock_t1<0) swap(t1[1],t1[2]);
for(j=2;j<nb;j++){
t2[1]=b[j-1],t2[2]=b[j];
if_clock_t2=dcmp(t2[0].cross(t2[1],t2[2]));
if(if_clock_t2<0) swap(t2[1],t2[2]);
res+=CPIA(t1,t2,3,3)*if_clock_t1*if_clock_t2;
}
}
return res;//面积交
//return PolygonArea(a,na)+PolygonArea(b,nb)-res;//面积并
}
Point a[4],b[5];
int main()
{
double x1,x2,x3,x4,y1,y2,y3,y4;
while(scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2)!=EOF)
{
scanf("%lf%lf%lf%lf",&x3,&y3,&x4,&y4);
a[0]=Point(x1,y1);
a[1]=Point(x2,y1);
a[2]=Point(x1,y2);
b[0]=Point(x3,y3);
b[1]=Point(x3,y4);
b[2]=Point(x4,y4);
b[3]=Point(x4,y3);
printf("%.8lf\n",fabs(SPIA(a,b,3,4)));
}
}
Bobo 有一个三角形和一个矩形,他想求他们交的面积。
具体地,三角形和矩形由 8 个整数 x1,y1,x2,y2,x3,y3,x4,y4 描述。
表示三角形的顶点坐标是 (x1,y1),(x1,y2),(x2,y1),
矩形的顶点坐标是 (x3,y3),(x3,y4),(x4,y4),(x4,y3).
输入包含不超过 30000 组数据。
每组数据的第一行包含 4 个整数 x1,y1,x2,y2 (x1≠x2,y1≠y2).
第二行包含 4 个整数 x3,y3,x4,y4 (x3<x4,y3<y4).
(0≤xi,yi≤104)
对于每组数据,输出一个实数表示交的面积。绝对误差或相对误差小于 10-6 即认为正确。
#include<bits/stdc++.h>
using namespace std;
const int maxn=555;
const int maxisn=10;
const double eps=1e-8;
const double pi=acos(-1.0);
int dcmp(double x){
if(x>eps) return 1;
return x<-eps ? -1 : 0;
}
inline double Sqr(double x){
return x*x;
}
struct Point{
double x,y;
Point(){x=y=0;}
Point(double x,double y):x(x),y(y){};
friend Point operator + (const Point &a,const Point &b) {
return Point(a.x+b.x,a.y+b.y);
}
friend Point operator - (const Point &a,const Point &b) {
return Point(a.x-b.x,a.y-b.y);
}
friend bool operator == (const Point &a,const Point &b) {
return dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)==0;
}
friend Point operator * (const Point &a,const double &b) {
return Point(a.x*b,a.y*b);
}
friend Point operator * (const double &a,const Point &b) {
return Point(a*b.x,a*b.y);
}
friend Point operator / (const Point &a,const double &b) {
return Point(a.x/b,a.y/b);
}
friend bool operator < (const Point &a, const Point &b) {
return a.x < b.x || (a.x == b.x && a.y < b.y);
}
inline double dot(const Point &b)const{
return x*b.x+y*b.y;
}
inline double cross(const Point &b,const Point &c)const{
return (b.x-x)*(c.y-y)-(c.x-x)*(b.y-y);
}
};
Point LineCross(const Point &a,const Point &b,const Point &c,const Point &d){
double u=a.cross(b,c),v=b.cross(a,d);
return Point((c.x*v+d.x*u)/(u+v),(c.y*v+d.y*u)/(u+v));
}
double PolygonArea(Point p[],int n){
if(n<3) return 0.0;
double s=p[0].y*(p[n-1].x-p[1].x);
p
=p[0];
for(int i=1;i<n;i++){
s+=p[i].y*(p[i-1].x-p[i+1].x);
}
return fabs(s*0.5);
}
double CPIA(Point a[],Point b[],int na,int nb){
Point p[maxisn],temp[maxisn];
int i,j,tn,sflag,eflag;
a[na]=a[0],b[nb]=b[0];
memcpy(p,b,sizeof(Point)*(nb+1));
for(i=0;i<na&&nb>2;++i){
sflag=dcmp(a[i].cross(a[i+1],p[0]));
for(j=tn=0;j<nb;++j,sflag=eflag){
if(sflag>=0) temp[tn++]=p[j];
eflag=dcmp(a[i].cross(a[i+1],p[j+1]));
if((sflag^eflag)==-2)
temp[tn++]=LineCross(a[i],a[i+1],p[j],p[j+1]);
}
memcpy(p,temp,sizeof(Point)*tn);
nb=tn,p[nb]=p[0];
}
if(nb<3) return 0.0;
return PolygonArea(p,nb);
}
double SPIA(Point a[],Point b[],int na,int nb){
int i,j;
Point t1[4],t2[4];
double res=0.0,if_clock_t1,if_clock_t2;
a[na]=t1[0]=a[0];
b[nb]=t2[0]=b[0];
for(i=2;i<na;i++){
t1[1]=a[i-1],t1[2]=a[i];
if_clock_t1=dcmp(t1[0].cross(t1[1],t1[2]));
if(if_clock_t1<0) swap(t1[1],t1[2]);
for(j=2;j<nb;j++){
t2[1]=b[j-1],t2[2]=b[j];
if_clock_t2=dcmp(t2[0].cross(t2[1],t2[2]));
if(if_clock_t2<0) swap(t2[1],t2[2]);
res+=CPIA(t1,t2,3,3)*if_clock_t1*if_clock_t2;
}
}
return res;//面积交
//return PolygonArea(a,na)+PolygonArea(b,nb)-res;//面积并
}
Point a[4],b[5];
int main()
{
double x1,x2,x3,x4,y1,y2,y3,y4;
while(scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2)!=EOF)
{
scanf("%lf%lf%lf%lf",&x3,&y3,&x4,&y4);
a[0]=Point(x1,y1);
a[1]=Point(x2,y1);
a[2]=Point(x1,y2);
b[0]=Point(x3,y3);
b[1]=Point(x3,y4);
b[2]=Point(x4,y4);
b[3]=Point(x4,y3);
printf("%.8lf\n",fabs(SPIA(a,b,3,4)));
}
}
Description
Bobo 有一个三角形和一个矩形,他想求他们交的面积。具体地,三角形和矩形由 8 个整数 x1,y1,x2,y2,x3,y3,x4,y4 描述。
表示三角形的顶点坐标是 (x1,y1),(x1,y2),(x2,y1),
矩形的顶点坐标是 (x3,y3),(x3,y4),(x4,y4),(x4,y3).
Input
输入包含不超过 30000 组数据。每组数据的第一行包含 4 个整数 x1,y1,x2,y2 (x1≠x2,y1≠y2).
第二行包含 4 个整数 x3,y3,x4,y4 (x3<x4,y3<y4).
(0≤xi,yi≤104)
Output
对于每组数据,输出一个实数表示交的面积。绝对误差或相对误差小于 10-6 即认为正确。
Sample Input
1 1 3 3 0 0 2 2 0 3 3 1 0 0 2 2 4462 1420 2060 2969 4159 257 8787 2970
Sample Output
1.00000000 0.75000000 439744.13967527
相关文章推荐
- CSU 1812 三角形和矩形【湖南省第十二届大学生计算机程序设计竞赛 J题】
- 湖南省第十二届大学生计算机程序设计竞赛 J 三角形和矩形 半平面交
- 2016年湖南省第十二届大学生计算机程序设计竞赛---Parenthesis(线段树求区间最值)
- 2016年湖南省第十二届大学生计算机程序设计竞赛
- CSU 1803 2016 (数学)【2016年湖南省第十二届大学生计算机程序设计竞赛 - A】
- 2016年湖南省第十二届大学生计算机程序设计竞赛
- 2016年湖南省第十二届大学生计算机程序设计竞赛 解题报告
- csu 2016年湖南省第十二届大学生计算机程序设计竞赛Problem A 2016
- 2016年湖南省第十二届大学生计算机程序设计竞赛:B—有向无环图
- 2016年湖南省第十二届大学生计算机程序设计竞赛(重现) A 2016
- 2016年湖南省第十二届大学生计算机程序设计竞赛 A 2016(数学)
- CSU 1809 Parenthesis (线段树)【2016年湖南省第十二届大学生计算机程序设计竞赛 - G】
- 2016年湖南省第十二届大学生计算机程序设计竞赛:G—parathesis
- 2016年湖南省第十二届大学生计算机程序设计竞赛 - G CSU 1809 Parenthesis (线段树)
- 2016年湖南省第十二届大学生计算机程序设计竞赛 A. 2016(数学)
- 2016年湖南省第十二届大学生计算机程序设计竞赛:F—地铁
- CSU1810(2016年湖南省第十二届大学生计算机程序设计竞赛 - H)
- 2016年湖南省第十二届大学生计算机程序设计竞赛(重现)
- 2016年湖南省第十二届大学生计算机程序设计竞赛:A—2016
- 【模拟】【数学】CSU 1803 2016 (2016湖南省第十二届大学生计算机程序设计竞赛)