poj 1408 Fishnet(计算几何)
题意:给出一个1*1的正方形,每条边上都有n个点,由对边上的点连成的线段将正方形分成(n+1)*2个不规则四边形,求其中面积最大的面积。
思路:利用叉积求出所有焦点,然后用叉积求四边形面积求的所有的面积找最大的一个~
代码:
#include <stdio.h> #include <string.h> #include <iostream> #define N 34 using namespace std; struct node { double x , y ; }p[N][N] ; int n ; //叉积求交点 struct node point ( struct node p1 , struct node p2 , struct node p3 , struct node p4 ) { struct node td ; double A1=p2.y-p1.y; double B1=p1.x-p2.x; double C1=p1.y*(p2.x-p1.x)-p1.x*(p2.y-p1.y); double A2=p4.y-p3.y; double B2=p3.x-p4.x; double C2=p3.y*(p4.x-p3.x)-p3.x*(p4.y-p3.y); td.x=(C2*B1-C1*B2)/(A1*B2-A2*B1); td.y=(C2*A1-C1*A2)/(B1*A2-B2*A1); return td ; } //计算面积 double area ( struct node p1 , struct node p2 , struct node p3 , struct node p4 ) { int i , j ; struct node are[4] ; are[0] = p1 ; are[1] = p2 ; are[2] = p3 ; are[3] = p4 ; double sum = 0.0 ; for ( i = 0 ; i < 4 ; i++ ) { j = ( i + 1 ) % 4 ; sum += are[i].x * are[j].y - are[i].y * are[j].x ; } sum /= 2 ; if ( sum < 0 ) sum = -sum ; return sum ; } int main() { int i , j , k ; while ( scanf ( "%d" , &n) , n ) { //给四个角赋值 p[0][0].x = 0.0 ;p[0][0].y = 0.0 ; p[0][n+1].x = 0.0 ; p[0][n+1].y = 1.0 ; p[n+1][n+1].x = 1.0 ; p[n+1][n+1].y = 1.0 ; p[n+1][0].x = 1.0 ; p[n+1][0].y = 0.0 ; //输入四条边上的点 for ( i = 1 ; i <= n ; i++ ) { scanf ( "%lf" , &p[i][0].x) ; p[i][0].y = 0.0 ; } for ( i = 1 ; i <= n ; i++ ) { scanf ( "%lf" , &p[i][n+1].x) ; p[i][n+1].y = 1.0 ; } for ( i = 1 ; i <= n ; i++ ) { scanf ( "%lf" , &p[0][i].y) ; p[0][i].x = 0.0 ; } for ( i = 1 ; i <= n ; i++ ) { scanf ( "%lf" , &p[n+1][i].y) ; p[n+1][i].x = 1.0 ; } //求出其他点 for ( i = 1 ; i <= n ; i++ ) { for ( j = 1 ; j <= n ; j++ ) p[i][j] = point ( p[i][0] , p[i][n+1] , p[0][j] , p[n+1][j] ) ; } /*for ( i = n+1 ; i >= 0 ; i-- ) { for ( j =0 ; j <= n+1 ; j++ ) printf ( "(%lf %lf)" , p[j][i].x , p[j][i].y ); printf ( "\n" ); }*/ //求出最大面积 double maxx = -1.0 ; for ( i = 0 ; i < n + 1 ; i++ ) { for ( j = 0 ; j < n + 1 ; j++ ) { double sum = area( p[i][j] , p[i+1][j] , p[i+1][j+1] , p[i][j+1] ) ; if ( sum > maxx ) maxx = sum ; } } printf ( "%.6lf\n" , maxx ); } return 0 ; }