传送门:HDU2826
题意很清楚了,就是问多边形是否相似。
相似判断的充要条件是对应边成比例,N<=300,暴力枚举两个多边形的对应边,然后比较判断是否对应边比例都相等。
注意一个坑点:求两点距离公式 不要用加号,改用减号。可能加号计算的数值太大了,WA了10多次。。。
代码如下:
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
const double eps = 1e-8;
int dcmp(double x) {
if (fabs(x) < eps) return 0;
return x<0?-1:1;
}
const int maxn = 300+5;
struct Point{
double x,y;
Point (){}
Point (double x, double y):x(x),y(y) {}
};
Point p1[maxn],p2[maxn];
typedef Point Vector;
Vector operator +(Vector a, Vector b) { return Vector(a.x+b.x,a.y+b.y); }
Vector operator -(Vector a, Vector b) { return Vector(a.x-b.x,a.y-b.y); }
double Dot(Vector A, Vector B) { return A.x*B.x - A.y*B.y; }
double Length(Vector A) { return Dot(A,A); }
int n;
double x,y;
bool judge(double dis, int a, int b) {
double tmp;
for (int i=0; i<n; ++i) {
tmp = Length(p1[a]-p1[(a+1)%n])-dis*Length(p2[b]-p2[(b+1)%n]);
if (dcmp(tmp)) return 0;
a = (a+1)%n;
b = (b+1)%n;
}
return 1;
}
bool isSimilar(){
double dis;
for (int i=0; i<n; ++i) {
for (int j=0; j<n; ++j) {
dis = Length(p1[i]-p1[(i+1)%n])/Length(p2[j]-p2[(j+1)%n]);
if (judge(dis,(i+1)%n,(j+1)%n)) return 1;
}
}
return 0;
}
int main(){
while (scanf("%d",&n) == 1) {
for (int i=0; i<n; ++i) {
scanf("%lf%lf",&x,&y);
p1[i] = Point(x,y);
}
for (int i=0; i<n; ++i) {
scanf("%lf%lf",&x,&y);
p2[i] = Point(x,y);
}
if (isSimilar()) printf("Yes\n"); else printf("No\n");
}
return 0;
}
本文介绍了一道HDU2826的计算几何题目,内容涉及判断两个多边形是否相似的问题。关键在于理解相似多边形的充要条件是对应边成比例,并通过暴力枚举方法进行判断。在实现过程中,要注意计算两点距离时使用减号避免数值过大导致错误。

953

被折叠的 条评论
为什么被折叠?



