Enumerate points to determine whether the point is inside the triangle


There has been considerable archeological work on the ancient Myacm culture. Many artifacts have been found in what have been called power fields: a fairly small area, less than 100 meters square where there are from four to fifteen tall monuments with crystals on top. Such an area is mapped out above. Most of the artifacts discovered have come from inside a triangular area between just three of the monuments, now called the power triangle. After considerable analysis archeologists agree how this triangle is selected from all the triangles with three monuments as vertices: it is the triangle with the largest possible area that does not contain any other monuments inside the triangle or on an edge of the triangle. Each field contains only one such triangle.

Archeological teams are continuing to find more power fields. They would like to automate the task of locating the power triangles in power fields. Write a program that takes the positions of the monuments in any number of power fields as input and determines the power triangle for each power field.

A useful formula: the area of a triangle with vertices (x1, y1), (x2, y2), and (x3, y3) is the absolute value of

0.5 * [(y3 - y1)(x2 - x1) - (y2 - y1)(x3 - x1)].
Input
For each power field there are several lines of data. The first line is the number of monuments: at least 4, and at most 15. For each monument there is a data line that starts with a one character label for the monument and is followed by the coordinates of the monument, which are nonnegative integers less than 100. The first label is A, and the next is B, and so on.

There is at least one such power field described. The end of input is indicated by a 0 for the number of monuments. The first sample data below corresponds to the diagram in the problem.
Output
For each power field there is one line of output. It contains the three labels of the vertices of the power triangle, listed in increasing alphabetical order, with no spaces.
Sample Input
6
A 1 0
B 4 0
C 0 3
D 1 3
E 4 4
F 0 6
4
A 0 0
B 1 0
C 99 0
D 99 99
0
Sample Output
BEF
BCD


The meaning of the question: I will give you some on the plane, and judge which three points form the largest triangle area, and there are no points in its interior and edges.
Thought analysis: Enumeration and judgment can be done, but the judgment point I started to write is not in the triangle line. The internal cross product judges Wa, I don’t know where the problem is, and a method
code example is changed later :
#define ll long long
const int maxn = 1e6+5;
const int mod = 1e9+7;
const double eps = 1e-9;

struct point
{
    double x, y;
    point(double _x=0, double _y=0):x(_x), y(_y){}
     
    // point - point = vector
    point operator-(const point &v){
        return point(x-v.x, y-v.y);
    }
 
};
 
int dcmp(double x){
    if (fabs(x)<eps) return 0;
    else return x<0?-1:1;
}
bool operator == (const point &a, const point &b){
    return (dcmp(a.x-b.x)==0 && dcmp(a.y-b.y)==0);
}
 
typedef point Vector; // Vector represents a vector

int n;
point p[20], mid[10];

double Cross(Vector a, Vector b){return a.x*b.y-a.y*b.x;}
double Area2(point a, point b, point c){return Cross(b-a, c-a);}
double Dot(Vector a, Vector b){return a.x*b.x+a.y*b.y;}
bool one(point p, point a1, point a2){
    return dcmp(Cross(a1-p, a2-p))==0 && dcmp(Dot(a1-p, a2-p))<=0;
}
int pointin(point po){
    int wn = 0;
    mid[3] = mid[0];
    for(int i = 0; i < 3; i++){
        if (one(po, mid[i], mid[i+1]) || po==mid[i]) return 1; //边界
        int k = dcmp(Cross(mid[i+1]-mid[i], po-mid[i]));
        int d1 = dcmp(mid[i].y-po.y);
        int d2 = dcmp(mid[i+1].y-po.y);
        if (k>0 && d1 <= 0 && d2>0) wn++;
        if (k<0 && d2 <= 0 && d1>0) wn--;
    }
    if (wn != 0) return 1; //internal
    return 0;
}

bool check(int x, int y, int z){
    mid[0] = p[x], mid[1] = p[y], mid[2] = p[z];
    mid[3] = p[x];
    for(int i = 1; i <= n; i++){
        if (i == x || i == y || i == z) continue;
        if (pointin(p[i])) return false;    
    }
    return true;
}

int main(){
    char ch[5];
    int p1, p2, p3;
    
    while(~scanf("%d", &n) && n){
        for(int i = 1; i <= n; i++){
            scanf("%s%lf%lf", ch, &p[i].x, &p[i].y);
        }
        double years = -1;
        for(int i = 1; i <= n; i++){
            for(int j = i+1; j <= n; j++){
                for(int k = j+1; k <= n; k++){
                    if (check(i, j, k)) {
                        double area = fabs(Area2(p[i], p[j], p[k]))/2.0;
                        //printf("i = %d j = %d k = %d, area = %f\n", i, j, k, area);
                        if (area > ans) {
                            ans = area;
                            p1 = i, p2 = j, p3 = k;
                        }
                    }
                }
            }
        }
        printf("%c%c%c\n", 'A'+p1-1, 'A'+p2-1, 'A'+p3-1);
    }    

    return 0;
}

 WA lost, to be updated..

#define ll long long
const int maxn = 1e6+5;
const int mod = 1e9+7;
const double eps = 1e-9;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;

struct point
{
    double x, y;
    point(double _x=0, double _y=0):x(_x), y(_y){}
     
    // point - point = vector
    point operator-(const point &v){
        return point(x-v.x, y-v.y);
    }
 
};
 
int dcmp(double x){
    if (fabs(x)<eps) return 0;
    else return x<0?-1:1;
}
bool operator == (const point &a, const point &b){
    return (dcmp(a.x-b.x)==0 && dcmp(a.y-b.y)==0);
}
 
typedef point Vector; // Vector represents a vector

int n;
point p[20];

double Cross(Vector a, Vector b){return a.x*b.y-a.y*b.x;}
double Area2(point a, point b, point c){return Cross(b-a, c-a);}
double Dot(Vector a, Vector b){return a.x*b.x+a.y*b.y;}
bool one(point p, point a1, point a2){
    return dcmp(Cross(a1-p, a2-p))==0 && dcmp(Dot(a1-p, a2-p))<=0;
}


bool check(int x, int y, int z){
    double area = fabs(Area2(p[x], p[y], p[z]))/2.0;
    if (area == 0.0+eps) return false;
    for(int i = 1; i <= n; i++){
        if (i == x || i == y || i == z) continue;
        if (one(p[i], p[x], p[y]) || one(p[i], p[x],p[z]) || one(p[i], p[y], p[z])) return false;
        
        //printf("--\n");
        Vector v1 = p[i]-p[x], v2 = p[i]-p[y], v3 = p[i]-p[z];
        int x1 = dcmp(Cross(v1, v2)), x2 = dcmp(Cross(v1, v3)), x3 = dcmp(Cross(v2, v3));
        
        if (x1 >= 0 && x2 >= 0 && x3 >= 0) return false;
        if (x1 <= 0 && x2 <= 0 && x3 <= 0) return false;
    }
    return true;
}

int main(){
    char ch[5];
    int p1, p2, p3;
    
    while(~scanf("%d", &n) && n){
        for(int i = 1; i <= n; i++){
            scanf("%s%lf%lf", ch, &p[i].x, &p[i].y);
        }
        double years = 0;
        for(int i = 1; i <= n; i++){
            for(int j = i+1; j <= n; j++){
                for(int k = j+1; k <= n; k++){
                    if (check(i, j, k)) {
                        double area = fabs(Area2(p[i], p[j], p[k]))/2.0;
                        //printf("i = %d j = %d k = %d, area = %f\n", i, j, k, area);
                        if (area > ans) {
                            ans = area;
                            p1 = i, p2 = j, p3 = k;
                            //printf("+++\n");
                        }
                    }
                }
            }
        }
        //printf("%d %d %d\n", p1, p2, p3);
        printf("%c%c%c\n", 'A'+p1-1, 'A'+p2-1, 'A'+p3-1);
    }    

    return 0;
}

 

Guess you like

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