[poj1569]Myacm Triangles--计算几何,判断一个点是否在三角形的内部

题目描述

这里写图片描述

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

题解

这道题目的大概意思就是给你n个点的坐标,要你找出面积最大的由3个点构成的三角形,且该三角形的内部或边上不能有点。

可以看到数据范围发现 n 15 ,所以我们可以枚举所有的三角形,再枚举所有的点,看有没有在三角形里就行了。

现在问题就是怎么判断一个点是否在三角形里了。

这个东西我们可以利用矢量的叉积做。因为三角形是一个凸多边形,所以如果我们沿着三角形的边逆时针走,那么一个点如果在三角形的内部的话,那么就一定会在每一条边的左边,这个大家自己画图就可以想的明白。

所以这又简化成了判断一个点在一条线段的哪一边了,这个可以用矢量的叉积快速的求出来,如果不知道的可以看我的另一篇题解,上面有详细的讲解:戳这里

这道题目就讲完了,下面放上代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define maxn 20
using namespace std;
struct P{
    double x,y;
}p[maxn];
const double eps=1e-10;
P operator - (P a,P b)
{
    P tmp;
    tmp.x=a.x-b.x;tmp.y=a.y-b.y;
    return tmp; 
}
double operator * (P a,P b){
    return a.x*b.y-b.x*a.y;
}
int n;
bool check(P a,P b,P c,P d)
{
    if((b-a)*(c-a)<0)  swap(b,c);
    if((b-a)*(d-a)<-eps)  return false;
    if((c-b)*(d-b)<-eps)  return false;
    if((a-c)*(d-c)<-eps)  return false;
    return true;
}
double dist(P a,P b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double S(P a,P b,P c)
{
    double l1=dist(a,b),l2=dist(b,c),l3=dist(c,a);
    double ar=(l1+l2+l3)/2;
    return sqrt(ar*(ar-l1)*(ar-l2)*(ar-l3));
}
int main()
{
    while(~scanf("%d",&n)&&n)
    {
        double ans=0.0;
        int ans1,ans2,ans3;
        char s[5];
        for(int i=1;i<=n;i++)  scanf("%s%lf%lf",s,&p[i].x,&p[i].y);
        for(int i=1;i<=n;i++){
            for(int j=i+1;j<=n;j++){
                for(int k=j+1;k<=n;k++){
                    int flag=0;
                    for(int l=1;l<=n;l++){
                        if(l==i||l==j||l==k)  continue;
                        if(check(p[i],p[j],p[k],p[l])==true){
                            flag=1;
                            break;
                        }
                    }
                    if(flag==0){
                        if(ans<S(p[i],p[j],p[k])){
                            ans=S(p[i],p[j],p[k]);
                            ans1=i;ans2=j;ans3=k;
                        }
                    }
                }
            }
        }
        printf("%c%c%c\n",ans1-1+'A',ans2-1+'A',ans3-1+'A');
    }
    return 0;
}

谢谢大家!!

猜你喜欢

转载自blog.csdn.net/dark_dawn/article/details/81416694