nyoj-952 - largest quadrilateral (vector cross product)

topic link

 

1  /* 
2      Name:nyoj-952-maximum quadrilateral 
 3      Copyright:
 4      Author:
 5      Date: 2018/4/27 10:46:24
 6      Description:
 7      Enumerate a diagonal line, and then choose one
 8      to see the big guys Explanation, in a two-dimensional vector, the result of the cross product (still a vector) is equal to the area
 9      Use the cross product to find the area of ​​the triangle, the clockwise of the point, 
 10      The positive and negative of the counterclockwise are different, know where the point is on the diagonal side, 
 11      to find the largest area of ​​each side respectively, add the two, it is the largest quadrilateral area obtained by this diagonal
 12  */ 
13 #include <iostream>
 14 #include <cstdio>
 15 #include < cstring >
 16  using  namespace std;
 17  const  double eps = 1e-8;
18 struct node{
19     double x ,y;
20 } arr[305];
21 double cross(node a,node b1,node b2){//求(b1-a) 和(b2-a) 的叉乘 
22     double x1,y1,x2,y2;
23     x1=b1.x-a.x;
24     y1=b1.y-a.y;
25     x2=b2.x-a.x;
26     y2=b2.y-a.y;   
27     return x1*y2-x2*y1;
28 }
29 int main()
30 {
31     int n;
32     while (cin>>n) {
33         memset(arr, 0, sizeof(arr));
34         for (int i=0; i<n; i++) {
35             cin>>arr[i].x>>arr[i].y;
36         }
37         double lmax = 0, rmax = 0, area = 0;
38         for (int i=0; i<n; i++) {
39             for (int j=i+1; j<n; j++ ) {
 40                  lmax = 0 , rmax = 0 ; // If the vector result is negative, the maximum value is still 0, of course, the left and right are one negative and one positive 
41                  for ( int k= 0 ; k<n; k++ ) {
 42                      if (k != i && k!= j){
 43                          /* 
44                              Find the maximum area on both sides of the diagonal respectively, vector dot product to find the area on one side of the diagonal
 45                              without dividing by 2 When , find the area of ​​the parallelogram
 46                              
47                              find the largest area on the left and right, and add them together, which is the area of ​​the two largest parallelograms.
 Divide 48                              by 2, and take each of the two parallelograms with the same diagonal. Max case 
 49                          */ 
50                         double tmp = cross(arr[i], arr[j], arr[k]);
51                         lmax = max(lmax, tmp);
52                         rmax = max(rmax, -tmp);
53                     }
54                 }
55                 if(lmax <= eps || rmax <= eps) continue;
56                 area = max(area, lmax + rmax);
57             }
58         }
59         printf("%lf\n", area / 2);
60     }
61     return 0;
62 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324952375&siteId=291194637