UVALive 7281 求凸包 并判断点是否在凸包内

题解

二分判断点在凸包内,把凸包分成以p0为顶点的tot-2个三角形,判断是否有一个三角形把所要判断的点包住

#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
int n,m,tot;
struct point
{
    double x,y;
}p[100000],a[100000],b[100000];
bool cmp(point A,point B)
{
    if(A.x!=B.x)
    return A.x<B.x;
    return A.y<B.y;
}
point operator -(point A,point B)
{
    point c;
    c.x=A.x-B.x;
    c.y=A.y-B.y;
    return c;
}
double cross(point A,point B)
{
    return A.x*B.y-B.x*A.y;
}
void dopack()
{
    tot=0;
    for(int i=1;i<=n;i++)
    {
        while(tot>1&&cross(p[tot-1]-p[tot-2],a[i]-p[tot-2])<=0)tot--;
        p[tot++]=a[i];
    }
    int k=tot;
    for(int i=n-1;i>0;i--)
    {
        while(tot>k&&cross(p[tot-1]-p[tot-2],a[i]-p[tot-2])<=0)tot--;
        p[tot++]=a[i];
    }
    if(n>1)tot--;
}
bool check(point A)
{
    int l=1,r=tot-2,mid;
    while(l<=r)
    {
        mid=(l+r)>>1;
        double a1=cross(p[mid]-p[0],A-p[0]);
        double a2=cross(p[mid+1]-p[0],A-p[0]);
        if(a1>=0&&a2<=0)
        {
            if(cross(p[mid+1]-p[mid],A-p[mid])>=0)return true;
            return false;
        }
        else if(a1<0)
        {
            r=mid-1;
        }
        else
        {
            l=mid+1;
        }
    }
    return false;
}
int main()
{
    while(cin>>n)
    {
        for(int i=1;i<=n;i++)
        {
            scanf("%lf%lf",&a[i].x,&a[i].y);
        }
        sort(a+1,a+1+n,cmp);
        dopack();
        scanf("%d",&m);
        int ans=0;
        for(int i=1;i<=m;i++)
        {
            scanf("%lf%lf",&b[i].x,&b[i].y);
            if(check(b[i]))ans++;
        }
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-07-29 14:39:33

UVALive 7281 求凸包 并判断点是否在凸包内的相关文章

poj1584——判断凸包,判断点是否在多边形内

poj1584——判断凸包,判断点是否在多边形内 A Round Peg in a Ground Hole Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5441   Accepted: 1729 Description The DIY Furniture company specializes in assemble-it-yourself furniture kits. Typically, the pieces o

poj1584--A Round Peg in a Ground Hole(判断凸包,并且判断圆是否在凸包内)

A Round Peg in a Ground Hole Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5399   Accepted: 1712 Description The DIY Furniture company specializes in assemble-it-yourself furniture kits. Typically, the pieces of wood are attached to on

判断点是否在多边形内

有一个n边形,顶点为p1,p2,...,pn;给定一个已知点p,判断p在此多边形内还是外. 预备知识: 两线段相交的定义,如果一条线段的两端分别处在另一条线段的两端,则此两线段相交 判断2点在线段的两侧可以用向量的叉乘实现! 基本步骤: 1,过p点垂直向上作一条射线 2,判断此射线与n边形n条边的交点 3,把所有交点相加,如果是奇数则说明在多边形内,否则在多边形外 思路非常的简单,另外说明一下几种特殊的情况: 1,射线与多边形的顶点相交:比如射线过多边形的Pi点,则如果Pi-1和Pi+1在此射线

C# 判断点是否在多边形内

/// <summary>/// 判断点是否在多边形内/// </summary>/// <param name="pnt">点</param>/// <param name="pntlist">区域的点集</param>/// <returns></returns>public static bool PointInFeaces(PointF pnt, List<

AE 判断点是否在面内

1 /// <summary> 2 /// 根据面要素的ID获取面,判断点是否在面内 3 /// </summary> 4 /// <param name="point">要判断的点,射线的起点</param> 5 /// <param name="ID">面的ID</param> 6 /// <param name="pFeatureLayer">面要素所在的图

百度地图 判断marker是否在多边形内

昨天画了圆形,判marker是否存在圆形内.今天来画多边形,判断marker在多边形内. 百度地图API覆盖物多边形类 http://developer.baidu.com/map/reference/index.php?title=Class:%E8%A6%86%E7%9B%96%E7%89%A9%E7%B1%BB/Polygon http://developer.baidu.com/map/reference/index.php?title=Class:%E8%A6%86%E7%9B%96%

判断对象是否在视线内

// Cast a sphere with the desired distance. Check each collider hit to see if it is within the field of view. Set objectFound // to the object that is most directly in front of the agent /// <summary> /// Withins the sight. /// </summary> ///

js 日期比较大小,js判断日期是否在区间内,js判断时间段是否在另外一个时间段内

/** * 日期解析,字符串转日期 * @param dateString 可以为2017-02-16,2017/02/16,2017.02.16 * @returns {Date} 返回对应的日期对象 */ function dateParse(dateString){ var SEPARATOR_BAR = "-"; var SEPARATOR_SLASH = "/"; var SEPARATOR_DOT = "."; var dateArr

判断点是否在凸多边形内

判断点是否在凸多边形内的方法很多,此处仅给出使用向量叉积判断点是否在凸多边形内的方法. 以下图为例说明问题: 原则: 1. 将多边形的第i条边的第一个顶点指向点P得到向量 v1,然后将从第一个顶点指向第二个顶点得到向量v2,叉乘这两个向量. 2.如果叉乘结果与上一条边的叉乘结果的乘积大于0则继续执行,如果乘积小于0,表示点P不在凸多边形内,直接返回即可. 要点:要求凸多边形的点以固定的顺序给出,例如固定为逆时针或顺时针. 实现的代码如下: struct Point { float x; floa