判断在几何对象内算法

判断点是否在有限长线段或直线上

// 判断点是否在有限长线段或直线上
const bool Geo::is_inside(const Point &point, const Line &line, const bool infinite)
{
    return Geo::distance(point, line.front(), line.back(), infinite) < Geo::EPSILON;
}

// 判断点是否在有限长线段或直线上
const bool Geo::is_inside(const Point &point, const Point &start, const Point &end, const bool infinite)
{
    return Geo::distance(point, start, end, infinite) < Geo::EPSILON;
}

判断点是否在多段线上

// 判断点是否在多段线上
const bool Geo::is_inside(const Point &point, const Polyline &polyline)
{
    for (size_t i = 1, count = polyline.size(); i < count; ++i)
    {
        if (Geo::is_inside(point, polyline[i-1], polyline[i]))
        {
            return true;
        }
    }
    return false;
}

判断点是否在多边形内

判断点是否在多边形内:coincide决定是否包含点在多边形上的情况。实现采用射线法。

// 判断点是否在多边形内,coincide决定是否包含点在多边形上的情况
const bool Geo::is_inside(const Point &point, const Polygon &polygon, const bool coincide)
{
    if (!polygon.empty() && Geo::is_inside(point, polygon.bounding_rect(), coincide))
    {
        size_t count = 0;
        double x = (-FLT_MAX);
        for (const Geo::Point &p : polygon)
        {
            x = std::max(x, p.x);
        }
        Geo::Point temp, end(x + 80, point.y);
        if (coincide)
        {
            for (size_t i = 1, len = polygon.size(); i < len; ++i)
            {
                if (Geo::is_inside(point, polygon[i-1], polygon[i]))
                {
                    return true;
                }
                if (Geo::is_intersected(point, end, polygon[i-1], polygon[i], temp))
                {
                    if (polygon[i - 1].y == polygon[i].y)
                    {
                        if ((polygon[i == 1 ? len-2 : i-2].y < polygon[i].y && polygon[i == len-1 ? 1 : i+1].y > polygon[i].y)
                            || (polygon[i == 1 ? len-2 : i-2].y > polygon[i].y && polygon[i == len-1 ? 1 : i+1].y < polygon[i].y))
                        {
                            ++count;
                        }
                    }
                    else
                    {
                        if (Geo::distance(temp, polygon[i-1]) < Geo::EPSILON)
                        {
                            if ((polygon[i == 1 ? len-2 : i-2].y < polygon[i-1].y && polygon[i].y < polygon[i-1].y) ||
                                (polygon[i == 1 ? len-2 : i-2].y > polygon[i-1].y && polygon[i].y > polygon[i-1].y))
                            {
                                ++count;
                            }
                        }
                        else
                        {
                            ++count;
                        }
                    }
                }
            }
        }
        else
        {
            for (size_t i = 1, len = polygon.size(); i < len; ++i)
            {
                if (Geo::is_inside(point, polygon[i-1], polygon[i]))
                {
                    return false;
                }
                if (Geo::is_intersected(point, end, polygon[i-1], polygon[i], temp))
                {
                    if (polygon[i - 1].y == polygon[i].y)
                    {
                        if ((polygon[i == 1 ? len-2 : i-2].y < polygon[i].y && polygon[i == len-1 ? 1 : i+1].y > polygon[i].y)
                            || (polygon[i == 1 ? len-2 : i-2].y > polygon[i].y && polygon[i == len-1 ? 1 : i+1].y < polygon[i].y))
                        {
                            ++count;
                        }
                    }
                    else
                    {
                        if (Geo::distance(temp, polygon[i-1]) < Geo::EPSILON)
                        {
                            if ((polygon[i == 1 ? len-2 : i-2].y < polygon[i-1].y && polygon[i].y < polygon[i-1].y) ||
                                (polygon[i == 1 ? len-2 : i-2].y > polygon[i-1].y && polygon[i].y > polygon[i-1].y))
                            {
                                ++count;
                            }
                        }
                        else
                        {
                            ++count;
                        }
                    }
                }
            }
        }
        return count % 2 == 1;
    }
    else
    {
        return false;
    }
}

判断点是否在AABB矩形内

判断点是否在AABB矩形内:coincide决定是否包含点在AABB矩形上的情况。

// 判断点是否在AABB矩形内,coincide决定是否包含点在AABB矩形上的情况
const bool Geo::is_inside(const Point &point, const AABBRect &rect, const bool coincide)
{
    if (rect.empty())
    {
        return false;
    }
    const double x = point.x, y = point.y;
    if (coincide)
    {
        return rect.left() <= x && x <= rect.right() && rect.bottom() <= y && y <= rect.top();
    }
    else
    {
        return rect.left() < x && x < rect.right() && rect.bottom() < y && y < rect.top();
    }
}

判断点是否在圆内

判断点是否在圆内:coincide决定是否包含点在圆上的情况。

// 判断点是否在圆内,coincide决定是否包含点在圆上的情况
const bool Geo::is_inside(const Point &point, const Circle &circle, const bool coincide)
{
    if (circle.empty())
    {
        return false;
    }
    if (coincide)
    {
        return Geo::distance(point, circle.center()) <= circle.radius();
    }
    else
    {
        return Geo::distance(point, circle.center()) < circle.radius();
    }
}

判断点是否在三角形内

判断点是否在三角形内:coincide决定是否包含点在三角形上的情况。实现采用同向法。

// 判断点是否在三角形内,coincide决定是否包含点在三角形上的情况
const bool Geo::is_inside(const Point &point, const Point &point0, const Point &point1, const Point &point2, const bool coincide)
{
    if (coincide)
    {
        const bool a = (point2.x - point.x) * (point0.y - point.y) >= (point0.x - point.x) * (point2.y - point.y);
        const bool b = (point0.x - point.x) * (point1.y - point.y) >= (point1.x - point.x) * (point0.y - point.y);
        const bool c = (point1.x - point.x) * (point2.y - point.y) >= (point2.x - point.x) * (point1.y - point.y);
        return a == b && b == c;
    }
    else
    {
        const bool a = (point2.x - point.x) * (point0.y - point.y) > (point0.x - point.x) * (point2.y - point.y);
        const bool b = (point0.x - point.x) * (point1.y - point.y) > (point1.x - point.x) * (point0.y - point.y);
        const bool c = (point1.x - point.x) * (point2.y - point.y) > (point2.x - point.x) * (point1.y - point.y);
        return a == b && b == c;
    }
}

// 判断点是否在三角形内,coincide决定是否包含点在三角形上的情况
const bool Geo::is_inside(const Point &point, const Triangle &triangle, const bool coincide)
{
    return Geo::is_inside(point, triangle[0], triangle[1], triangle[2], coincide);
}

判断有限长线段是否完全在三角形内

判断有限长线段是否完全在三角形内:线段与三角形相交或有端点在三角形上均不算在三角形内部。

// 判断有限长线段是否完全在三角形内,线段与三角形相交或有端点在三角形上均不算在三角形内部
const bool Geo::is_inside(const Point &start, const Point &end, const Triangle &triangle)
{
    return Geo::is_inside(start, triangle) && Geo::is_inside(end, triangle);
}

判断一个三角形是否完全在另一个三角形内部

判断一个三角形是否完全在另一个三角形内部:与三角形相交或有顶点在三角形上均不算在三角形内部。

// 判断一个三角形是否完全在另一个三角形内部,与三角形相交或有顶点在三角形上均不算在三角形内部
const bool Geo::is_inside(const Triangle &triangle0, const Triangle &triangle1)
{
    return Geo::is_inside(triangle0[0], triangle1) && Geo::is_inside(triangle0[1], triangle1)
        && Geo::is_inside(triangle0[2], triangle1);
}

  • 6
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值