zoukankan      html  css  js  c++  java
  • 枚举点,判断点是否在三角形内部


    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
    

    题意 : 给你平面上得一些,判断哪三个点组成得三角形面积最大,且其内部和边上没有点
    思路分析 :枚举加判断就可以,但是我开始写的判断点在不在三角行内部叉积判断Wa 了,不知道哪里得问题,后面换了个方法
    代码示例:
    #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 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表示向量
    
    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; //内部
        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 ans = -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
    ", i, j, k, area);
                            if (area > ans) {
                                ans = area; 
                                p1 = i, p2 = j, p3 = k; 
                            }
                        }
                    }
                }
            }
            printf("%c%c%c
    ", 'A'+p1-1, 'A'+p2-1, 'A'+p3-1);
        }    
    
        return 0;
    }
    

     WA掉的,待更新..

    #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 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表示向量
    
    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("--
    "); 
            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 ans = 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
    ", i, j, k, area);
                            if (area > ans) {
                                ans = area; 
                                p1 = i, p2 = j, p3 = k;
                                //printf("+++
    ");
                            }
                        }
                    }
                }
            }
            //printf("%d %d %d
    ", p1, p2, p3);
            printf("%c%c%c
    ", 'A'+p1-1, 'A'+p2-1, 'A'+p3-1);
        }    
    
        return 0;
    }
    
    东北日出西边雨 道是无情却有情
  • 相关阅读:
    用户画像
    华为离职副总裁徐家骏:年薪千万的工作感悟
    JAVA CAS原理深度分析-转载
    彻底理解ThreadLocal二
    彻底理解ThreadLocal一
    观察者模式(浅谈监听器工作原理)
    Java编程提高性能时需注意的地方
    Spring对Quartz的封装实现简单需注意事项
    FileInputStream和BufferedInputStream的区别
    java
  • 原文地址:https://www.cnblogs.com/ccut-ry/p/8995415.html
Copyright © 2011-2022 走看看