| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
求测试数据!!!谁能给一组强一点的测试数据啊???
我下面那个程序考虑了多种特殊情况了!!!还是有错啊。请大牛帮忙看看。
#include <stdio.h>
#include <math.h>
#include <string>
#include <iostream>
using namespace std;
const double pre = 1e-10;
const double Min = -1, Max = 11;
int Comp(double d1, double d2);
struct node{
double x;
double y;
node(double x = 0, double y = 0){
this->x = x; //连结构体也有this指针!
this->y = y;
}
bool operator==(node n){
return Comp(x, n.x) == 0 && Comp(y, n.y) == 0;
}
void setNode(double x, double y){
this->x = x; //连结构体也有this指针!
this->y = y;
}
};
node polygon[100], _polygon[2][100];
int num[2];
double CrossPro(node n1, node n2, node u1, node u2){ //计算叉乘
n1.x = n2.x - n1.x;
n1.y = n2.y - n1.y;
u1.x = u2.x - u1.x;
u1.y = u2.y - u1.y;
return n1.x * u1.y - n1.y * u1.x;
}
int Comp(double d1, double d2){
double d = d1 - d2;
if(fabs(d) < pre)
return 0;
if(d > 0)
return 1;
return -1;
}
bool Cross(node n1, node n2, node u1, node u2){ //判断是否相交
double d1, d2;
d1 = CrossPro(n1, u2, n1, n2);
d2 = CrossPro(n1, n2, n1, u1);
if(Comp(d1 * d2, 0) < 0)
return false;
d1 = CrossPro(u1, n1, u1, u2);
d2 = CrossPro(u1, u2, u1, n2);
if(Comp(d1 * d2, 0) < 0)
return false;
return true;
}
node CrossDot(node n1, node n2, node u1, node u2){ //求两条线段的交点
node ans;
double s1, s2;
s1 = fabs(CrossPro(n1, u1, n1, n2));
s2 = fabs(CrossPro(n1, u2, n1, n2));
ans.x = (s2 * u1.x + s1 * u2.x) / (s1 + s2);
ans.y = (s2 * u1.y + s1 * u2.y) / (s1 + s2);
return ans;
}
double area(node polygon[], int n){ //n为点数
double ans = 0;
int i;
polygon[n] = polygon[0];
for(i = 0; i < n; i++){
ans += (polygon[i].x * polygon[i + 1].y - polygon[i].y * polygon[i + 1].x);
}
return fabs(ans / 2.0);
}
void Mid(node n1, node n2, node& u1, node& u2){ //该程序尚未验证!
if(n1.x == n2.x){
u1.x = Min;
u2.x = Max;
u1.y = u2.y = (n1.y + n2.y) / 2.0;
}else if(n1.y == n2.y){
u1.y = Min;
u2.y = Max;
u1.x = u2.x = (n1.x + n2.x) / 2.0;
}else{
u1.x = Min;
u2.x = Max;
u1.y = (n1.y + n2.y) / 2.0 - (n2.x - n1.x) * (u1.x - (n1.x + n2.x) / 2.0) / (n2.y - n1.y);
u2.y = (n1.y + n2.y) / 2.0 - (n2.x - n1.x) * (u2.x - (n1.x + n2.x) / 2.0) / (n2.y - n1.y);
}
}
void Parallel(node polygon[], int& n, node n1, node n2, node w){ //在检测到线段和多边行的某条边平行之后做处理
int i;
int k1, k2;
if(!(polygon[n - 1] == polygon[0]))
polygon[n] = polygon[0];
k1 = CrossPro(n1, w, n1, n2);
for(i = 0; i < n; i++){
k2 = CrossPro(n1, polygon[i], n1, n2);
if(k2 != 0){
if(Comp(k1 * k2, 0) < 0){ //不在同一侧
n = 0;
}
return;
}
}
}
void PrintPoly(node polygon[], int n){
for(int i = 0; i < n; i++)
printf("(%lf, %lf) ", polygon[i].x, polygon[i].y);
printf("\n\n");
}
void Next(node polygon[], int& n, node n1, node n2, node w){
if(n == 0)
return;
polygon[n] = polygon[0];
int i, flag, j, a;
node Cro[2];
flag = 0;
int g;
num[0] = num[1] = 0;
g = 0;
/*
printf("3:\n");
PrintPoly(polygon, n);
*/
for(i = 0; i < n; i++){
_polygon[g][num[g]++] = polygon[i];
if(Cross(polygon[i], polygon[i + 1], n1, n2)){
flag = 1; //表示有交点
//下一步 还得判断是否平行!!
//printf("i:%d\n", i);
a = i - 1;
if(a < 0)
a = n - 1;
if(Comp(CrossPro(polygon[i], polygon[i + 1], n1, n2), 0) == 0){ //如果平行的话
//printf("parallel\n");
Parallel(polygon, n, n1, n2, w);
return ;
}
Cro[g] = CrossDot(polygon[i], polygon[i + 1], n1, n2);
if(!(Cro[g] == polygon[i])){
_polygon[g][num[g]++] = Cro[g];
}else{
a = i - 1;
if(a < 0)
a = n - 1;
int b1 = CrossPro(n1, polygon[a], n1, n2);
int b2 = CrossPro(n1, polygon[i + 1], n1, n2);
int b3 = CrossPro(n1, polygon[i], n1, n2);
if(Comp(b1 * b2, 0) > 0 || (Comp(b1, 0) == 0 && Comp(b3, 0) == 0 )){
//printf("w:(%lf, %lf)\n", w.x, w.y);
Parallel(polygon, n, n1, n2, w);
return ;
}
}
int gg = (g + 1) % 2;
_polygon[gg][num[gg]++] = Cro[g];
if(Cro[g] == polygon[i + 1]){
a = (i + 2) % n;
int b1 = CrossPro(n1, polygon[a], n1, n2);
int b2 = CrossPro(n1, polygon[i + 1], n1, n2);
int b3 = CrossPro(n1, polygon[i], n1, n2);
if(Comp(b1 * b3, 0) > 0 || (Comp(b1, 0) == 0 && Comp(b2, 0) == 0 )){
Parallel(polygon, n, n1, n2, w);
return ;
}
i++;
}
g = (g + 1) % 2;
}
//printf("i: %d\n", i);
}
//判断两个交点是否相等
if(flag == 0 || Cro[0] == Cro[1]){
Parallel(polygon, n, n1, n2, w);
return ;
}else{
int k1, k2;
k1 = CrossPro(n1, w, n1, n2);
n = 0;
for(i = 0; i < num[0]; i++){
k2 = CrossPro(n1, _polygon[0][i], n1, n2);
if(k2 != 0){
if(Comp(k1 * k2, 0) > 0){
//如果在同一侧
for(i = 0; i < num[0]; i++){
polygon[i] = _polygon[0][i];
n = num[0];
}
return;
}
break;
}
}
for(i = 0; i < num[1]; i++){
k2 = CrossPro(n1, _polygon[1][i], n1, n2);
if(k2 != 0){
if(Comp(k1 * k2, 0) > 0){
//如果在同一侧
for(i = 0; i < num[1]; i++){
polygon[i] = _polygon[1][i];
n = num[1];
}
return;
}
break;
}
}
}
//输出多边形上的点集
}
int main(){
node n1, n2, u1, u2;
string str;
int n = 4;
double ans = 0;
polygon[0].setNode(0, 0);
polygon[1].setNode(10, 0);
polygon[2].setNode(10, 10);
polygon[3].setNode(0, 10);
n1.setNode(0, 0);
while(scanf("%lf%lf", &n2.x, &n2.y)){
cin >>str;
Mid(n1, n2, u1, u2);
//printf("(%lf, %lf) --> (%lf, %lf)\n", u1.x, u1.y, u2.x, u2.y);
if(str[0] == 'H'){
Next(polygon, n, u1, u2, n2);
}else if(str[0] == 'C'){
Next(polygon, n, u1, u2, n1);
}else{
//判断前后两个点是否相等
if(!(n1 == n2))
n = 0; //如果距离相同,解集就是一条直线,直线是没有面积的
}
/*
printf("1:\n");
PrintPoly(_polygon[0], num[0]);
printf("2:\n");
PrintPoly(_polygon[1], num[1]);
printf("3:\n");
PrintPoly(polygon, n);
*/
ans = area(polygon, n);
printf("%.2lf\n", ans);
n1 = n2;
if(fabs(ans) < pre)
break;
}
return 0;
}
/*
测试线段是否相交
1 1
3 3
2 0
1 3
1 1
3 3
1.499 1503
1 3
1 1
3 3
1 1
3 3
面积检验
3
0 0
2 0
2 2
数据检验
10.0 10.0 Colder
10.0 0.0 Hotter
0.0 0.0 Colder
5.0 5.0 Hotter
*/
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator