poj1408

题意:给出一个正方形,每条边上有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 ;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值