poj1408--Fishnet(求多边形面积)
Description
In order to catch fish, he began to make a kind of fishnet by cutting the long thread into short threads and fixing them at pegs on the square wood-frame. He wanted to know the sizes of the meshes of the fishnet to see whether he could catch small fish as well as large ones.
The wood frame is perfectly square with four thin edges on meter long: a bottom edge, a top edge, a left edge, and a right edge. There are n pegs on each edge, and thus there are 4n pegs in total. The positions of pegs are represented by their (x,y)-coordinates. Those of an example case with n=2 are depicted in figures below. The position of the ith peg on the bottom edge is represented by (ai,0). That on the top edge, on the left edge and on the right edge are represented by (bi,1), (0,ci) and (1,di), respectively. The long thread is cut into 2n threads with appropriate lengths. The threads are strained between (ai,0) and (bi,1),and between (0,ci) and (1,di) (i=1,...,n).
You should write a program that reports the size of the largest mesh among the (n+1)2 meshes of the fishnet made by fixing the threads at the pegs. You may assume that the thread he found is long enough to make the fishnet and the wood-frame is thin enough for neglecting its thickness.
Input
n
a1 a2 ... an
b1 b2 ... bn
c1 c2 ... cn
d1 d2 ... dn
you may assume 0 < n <= 30, 0 < ai,bi,ci,di < 1
Output
Sample Input
2 0.2000000 0.6000000 0.3000000 0.8000000 0.1000000 0.5000000 0.5000000 0.6000000 2 0.3333330 0.6666670 0.3333330 0.6666670 0.3333330 0.6666670 0.3333330 0.6666670 4 0.2000000 0.4000000 0.6000000 0.8000000 0.1000000 0.5000000 0.6000000 0.9000000 0.2000000 0.4000000 0.6000000 0.8000000 0.1000000 0.5000000 0.6000000 0.9000000 2 0.5138701 0.9476283 0.1717362 0.1757412 0.3086521 0.7022313 0.2264312 0.5345343 1 0.4000000 0.6000000 0.3000000 0.5000000 0
Sample Output
0.215657 0.111112 0.078923 0.279223 0.348958
题意很简单,在1*1的正方形块中每条边有n个点,对应边的对应点连线,将正方形分割成(n+1)*(n+1)个小正方形,求出小正方形中的最大的面积。
先储存下每个点的坐标,然后用叉积计算出各个小正方形的面积,找出最大值。
#include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std ; #define eps 1e-8 struct node { double x , y ; } p[40][40]; double a[40] , b[40] , c[40] , d[40] ; node f(node a1,node a2,node b1,node b2) { node q = a1 ; double t = ( (a1.x-b1.x)*(b1.y-b2.y) - (a1.y-b1.y)*(b1.x-b2.x) ) / ( (a1.x-a2.x)*(b1.y-b2.y) - ( a1.y-a2.y )*(b1.x-b2.x) ) ; q.x += (a2.x-a1.x)*t ; q.y += ( a2.y-a1.y )*t ; return q ; } int main() { int n , i , j ; double k , max1 , ans ; while( scanf("%d", &n) && n ) { p[0][0].x = p[0][0].y = 0.0 ; for(i = 1 ; i <= n ; i++) { scanf("%lf", &a[i]) ; p[0][i].x = a[i] ; p[0][i].y = 0.0 ; } p[0][n+1].x = 1.0 ; p[0][n+1].y = 0.0 ; p[n+1][0].x = 0 ; p[n+1][0].y = 1.0 ; for(i = 1 ; i <= n ; i++) { scanf("%lf", &b[i]) ; p[n+1][i].x = b[i] ; p[n+1][i].y = 1.0 ; } p[n+1][n+1].x = p[n+1][n+1].y = 1.0 ; for(i = 1 ; i <= n ; i++) { scanf("%lf", &c[i]) ; p[i][0].x = 0.0 ; p[i][0].y = c[i] ; } for(i = 1 ; i <= n ; i++) { scanf("%lf", &d[i]) ; p[i][n+1].x = 1.0 ; p[i][n+1].y = d[i] ; } for(i = 1 ; i <= n ; i++) for(j = 1 ; j <= n ; j++) { if( fabs( c[i]-d[i] ) < eps && fabs( b[j]-a[j] ) < eps ) { p[i][j].x = a[j] ; p[i][j].y = c[i] ; } else if( fabs( c[i]-d[i] ) < eps && fabs( b[j]-a[j] ) > eps ) { p[i][j].x = c[i]*(b[j]-a[j]) + a[j] ; p[i][j].y = c[i] ; } else if( fabs( c[i]-d[i] ) > eps && fabs( b[j]-a[j] ) < eps ) { p[i][j].x = a[j] ; p[i][j].y = a[j]*(d[i]-c[i]) + c[i] ; } else { p[i][j] = f(p[0][j],p[n+1][j],p[i][0],p[i][n+1]) ; } } node s1 , e1 , s2 , e2 ; max1 = 0 ; for(i = 0 ; i <= n ; i++) for(j = 0 ; j <= n ; j++) { s1.x = p[i][j+1].x - p[i][j].x ; s1.y = p[i][j+1].y - p[i][j].y ; e1.x = p[i+1][j+1].x - p[i][j+1].x ; e1.y = p[i+1][j+1].y - p[i][j+1].y ; s2.x = p[i+1][j].x - p[i][j].x ; s2.y = p[i+1][j].y - p[i][j].y ; e2.x = p[i+1][j+1].x - p[i+1][j].x ; e2.y = p[i+1][j+1].y - p[i+1][j].y ; ans = ( fabs(s1.x*e1.y-s1.y*e1.x)/2.0 + fabs(s2.x*e2.y-s2.y*e2.x)/2.0 ) ; max1 = max(max1,ans) ; } printf("%.6lf\n", max1) ; } return 0; }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。