题意:给出一个正方形,每条边上有n个点,把这些点作为端点拉线组成的网格(整个正方形由一些四边行拼成),求最大格的面积。
分析:计算几何,求出所有线的交点,分别计算每个格的面积。
在求线段交点时可以用叉积面积的方式,求ac,bd交点:
void intersection(Point &a, Point &b, Point &c, Point &d, Point &ret)
{
double s1 = xmulti(a, c, b);
double s2 = xmulti(a, c, d);
ret.x = (s1 * d.x - s2 * b.x) / (s1 - s2);
ret.y = (s1 * d.y - s2 * b.y) / (s1 - s2);
}
View Code
#include
<
iostream
>
#include < cstdio >
#include < cstdlib >
#include < cstring >
#include < cmath >
using namespace std;
#define maxn 50
struct Point
{
double x, y;
} point[maxn][maxn];
double ans;
int n;
void input()
{
n ++ ;
point[ 0 ][ 0 ].x = 0 ;
point[ 0 ][ 0 ].y = 0 ;
point[ 0 ][n].x = 1 ;
point[ 0 ][n].y = 0 ;
point[n][ 0 ].x = 0 ;
point[n][ 0 ].y = 1 ;
point[n][n].x = 1 ;
point[n][n].y = 1 ;
for ( int i = 1 ; i < n; i ++ )
{
scanf( " %lf " , & point[ 0 ][i].x);
point[ 0 ][i].y = 0 ;
}
for ( int i = 1 ; i < n; i ++ )
{
scanf( " %lf " , & point[n][i].x);
point[n][i].y = 1 ;
}
for ( int i = 1 ; i < n; i ++ )
{
scanf( " %lf " , & point[i][ 0 ].y);
point[i][ 0 ].x = 0 ;
}
for ( int i = 1 ; i < n; i ++ )
{
scanf( " %lf " , & point[i][n].y);
point[i][n].x = 1 ;
}
}
double xmulti(Point & p0, Point & p1, Point & p2)
{
return ((p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y));
}
void intersection(Point & a, Point & b, Point & c, Point & d, Point & ret)
{
double s1 = xmulti(a, c, b);
double s2 = xmulti(a, c, d);
ret.x = (s1 * d.x - s2 * b.x) / (s1 - s2);
ret.y = (s1 * d.y - s2 * b.y) / (s1 - s2);
}
double area(Point & a, Point & b, Point & c, Point & d)
{
double s1 = xmulti(a, c, b);
double s2 = xmulti(a, c, d);
return (abs(s1) + abs(s2)) / 2 ;
}
void work()
{
for ( int i = 1 ; i < n; i ++ )
for ( int j = 1 ; j < n; j ++ )
intersection(point[i][ 0 ], point[ 0 ][j], point[i][n], point[n][j], point[i][j]);
ans = - 1 ;
for ( int i = 0 ; i < n; i ++ )
for ( int j = 0 ; j < n; j ++ )
{
double temp = area(point[i][j], point[i + 1 ][j], point[i + 1 ][j + 1 ], point[i][j + 1 ]);
ans = max(ans, temp);
}
}
int main()
{
// freopen("t.txt", "r", stdin);
while (scanf( " %d " , & n), n)
{
input();
work();
printf( " %.6f\n " , ans);
}
return 0 ;
}
#include < cstdio >
#include < cstdlib >
#include < cstring >
#include < cmath >
using namespace std;
#define maxn 50
struct Point
{
double x, y;
} point[maxn][maxn];
double ans;
int n;
void input()
{
n ++ ;
point[ 0 ][ 0 ].x = 0 ;
point[ 0 ][ 0 ].y = 0 ;
point[ 0 ][n].x = 1 ;
point[ 0 ][n].y = 0 ;
point[n][ 0 ].x = 0 ;
point[n][ 0 ].y = 1 ;
point[n][n].x = 1 ;
point[n][n].y = 1 ;
for ( int i = 1 ; i < n; i ++ )
{
scanf( " %lf " , & point[ 0 ][i].x);
point[ 0 ][i].y = 0 ;
}
for ( int i = 1 ; i < n; i ++ )
{
scanf( " %lf " , & point[n][i].x);
point[n][i].y = 1 ;
}
for ( int i = 1 ; i < n; i ++ )
{
scanf( " %lf " , & point[i][ 0 ].y);
point[i][ 0 ].x = 0 ;
}
for ( int i = 1 ; i < n; i ++ )
{
scanf( " %lf " , & point[i][n].y);
point[i][n].x = 1 ;
}
}
double xmulti(Point & p0, Point & p1, Point & p2)
{
return ((p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y));
}
void intersection(Point & a, Point & b, Point & c, Point & d, Point & ret)
{
double s1 = xmulti(a, c, b);
double s2 = xmulti(a, c, d);
ret.x = (s1 * d.x - s2 * b.x) / (s1 - s2);
ret.y = (s1 * d.y - s2 * b.y) / (s1 - s2);
}
double area(Point & a, Point & b, Point & c, Point & d)
{
double s1 = xmulti(a, c, b);
double s2 = xmulti(a, c, d);
return (abs(s1) + abs(s2)) / 2 ;
}
void work()
{
for ( int i = 1 ; i < n; i ++ )
for ( int j = 1 ; j < n; j ++ )
intersection(point[i][ 0 ], point[ 0 ][j], point[i][n], point[n][j], point[i][j]);
ans = - 1 ;
for ( int i = 0 ; i < n; i ++ )
for ( int j = 0 ; j < n; j ++ )
{
double temp = area(point[i][j], point[i + 1 ][j], point[i + 1 ][j + 1 ], point[i][j + 1 ]);
ans = max(ans, temp);
}
}
int main()
{
// freopen("t.txt", "r", stdin);
while (scanf( " %d " , & n), n)
{
input();
work();
printf( " %.6f\n " , ans);
}
return 0 ;
}