旋转卡壳法求最大三角形

poj2079

Triangle
Time Limit: 3000MS Memory Limit: 30000K
Total Submissions: 8023 Accepted: 2368

Description

Given n distinct points on a plane, your task is to find the triangle that have the maximum area, whose vertices are from the given points.

Input

The input consists of several test cases. The first line of each test case contains an integer n, indicating the number of points on the plane. Each of the following n lines contains two integer xi and yi, indicating the ith points. The last line of the input is an integer −1, indicating the end of input, which should not be processed. You may assume that 1 <= n <= 50000 and −10 4 <= xi, yi <= 10 4 for all i = 1 . . . n.

Output

For each test case, print a line containing the maximum area, which contains two digits after the decimal point. You may assume that there is always an answer which is greater than zero.

Sample Input

3
3 4
2 6
2 7
5
2 6
3 9
2 0
8 0
6 5
-1

Sample Output

0.50
27.00

程序:

#include"string.h"
#include"stdio.h"
#include"math.h"
#include"stdlib.h"
#define M 50001
#define inf 999999999
#define eps 1e-10
typedef struct node
{
    double x,y,cos,dis;
}E;
E p[M],q[M];
double max(double a,double b)
{
    return a>b?a:b;
}
int cmp(const void *a,const void *b)
{
    if(fabs((*(struct node*)a).cos-(*(struct node*)b).cos)<eps)
        return (*(struct node*)a).dis>(*(struct node*)b).dis?1:-1;
    else
        return (*(struct node*)b).cos>(*(struct node*)a).cos?1:-1;
}
double angle(node p1,node p2)
{
    double x1=p2.x-p1.x;
    double y1=p2.y-p1.y;
    double x2=1;
    double y2=0;
    return (x1*x2+y1*y2)/sqrt((x1*x1+y1*y1)*(x2*x2+y2*y2));
}
double pow(double x)
{
    return x*x;
}
double Len(node p1,node p2)
{
    return pow(p2.x-p1.x)+pow(p2.y-p1.y);
}
double cross(node p0,node p1,node p2)
{
    double x1=p1.x-p0.x;
    double y1=p1.y-p0.y;
    double x2=p2.x-p0.x;
    double y2=p2.y-p0.y;
    return x1*y2-x2*y1;
}
int main()
{
    int n,i,j,k;
    while(scanf("%d",&n),n!=-1)
    {
        int tep;
        E start;
        start.x=start.y=inf;
        for(i=0;i<n;i++)
        {
            scanf("%lf%lf",&p[i].x,&p[i].y);
            if(p[i].y<start.y)
            {
                start=p[i];
                tep=i;
            }
            else if(fabs(p[i].y-start.y)<eps)
            {
                if(p[i].x<start.x)
                {
                    start=p[i];
                    tep=i;
                }
            }
        }
        p[tep].dis=0;
        p[tep].cos=10;
        for(i=0;i<n;i++)
        {
            if(i!=tep)
            {
                p[i].cos=angle(start,p[i]);
                p[i].dis=Len(start,p[i]);
            }
        }
        qsort(p,n,sizeof(p[0]),cmp);
        q[0]=p[n-1];
        q[1]=p[0];
        q[2]=p[1];
        int cnt=2;
        for(i=2;i<n;i++)
        {
            while(cross(q[cnt-1],q[cnt],p[i])<0)
            {
                cnt--;
            }
            q[++cnt]=p[i];
        }//求凸包
        j=1;
        double s=0;
        j=k=1;
        for(i=0;i<cnt;i++)
        {
            while(cross(q[i],q[j%cnt],q[(k+1)%cnt])>cross(q[i],q[j%cnt],q[k%cnt]))
                k++;
            s=max(s,cross(q[i],q[j%cnt],q[k%cnt]));
            while(cross(q[i],q[(j+1)%cnt],q[k%cnt])>cross(q[i],q[j%cnt],q[k%cnt]))
                j++;
            s=max(s,cross(q[i],q[j%cnt],q[k%cnt]));
        }//注意cross()中i,j,k的书写顺序
        /*其思路是这样的,定点I,p,q,先I,p固定,让q旋转找到最大的面积三角形,之后,I,q固定,p旋转,
        找到最大的三角形面积,比较记录.然后i++;直到i遍历所有顶点.所求出来的三角形就是面积最大.
        这里的旋转卡壳思想就是固定,旋转.这样的.显然i++后,p,q两点不需要再从i+1,i+2开始,这个好
        形容,对p,q进行取模运算的时候,注意自己的SP栈指针多大.*/
        printf("%.2lf\n",s/2);
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
旋转卡壳法(Rotating Calipers Algorithm),也称为圆规算法,是一种用于计算二维凸多边形内最近点对之间最短距离的有效算法。在MATLAB中,你可以通过编写一个函数来实现这个过程。以下是基本步骤: 1. **初始化**:选择两个顶点作为初始“圆规”(通常取顶点集合的第一和最后一个元素),记为`p1`和`p2`。 2. **迭代**:对于每个顶点`p`,计算它与当前圆规对角线所围成的三角形的两边长度`d1`和`d2`。 - 如果`d1`+`d2`小于当前最短距离,更新最短距离,并调整圆规为新形成的对角线。 - 否则,将圆规顺时针移动到下一个顶点,除非即将形成闭合路径,这时需要逆时针回溯。 3. **结束条件**:当圆规再次回到起始位置时,循环结束,因为这意味着所有可能的对角线都已被考虑过。 4. **返回结果**:最短距离即为找到的最小距离。 在MATLAB中,可以创建一个函数,例如`minDistancePolyhedron`,接受凸多边形的顶点列表作为输入,然后按照上述逻辑遍历并计算。下面是一个简化的示例代码片段: ```matlab function d = rotatingCalipers(p) n = length(p); if n < 3 % 凸多边形最少要有三个顶点 error('Not a valid polygon'); end p1 = p(1); % 初始圆规 p2 = p(end); minDist = Inf; % 初始化最小距离 for i = 2:n-1 d1 = norm(p(i) - p1); d2 = norm(p(i+1) - p1); if d1 + d2 < minDist [d, ~] = min([minDist, d1, d2]); p2 = p(i+1); else if i == n-1 % 如果形成闭合路径,回溯到开始 p2 = p1; else p2 = p(i); end end end d = minDist; % 返回最小距离 end % 示例使用 vertices = [0 0; 1 0; 1 1; 0 1]; % 四边形 minDistance = rotatingCalipers(vertices); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值