平行、矩形、左侧判断算法

判断两直线是否平行

// 判断两直线是否平行
const bool Geo::is_parallel(const Point &point0, const Point &point1, const Point &point2, const Point &point3)
{
    if (point0.x == point1.x && point2.x == point3.x)
    {
        return true;
    }
    else
    {
        return ((point0.y - point1.y) / (point0.x - point1.x)) == ((point2.y - point3.y) / (point2.x - point3.x));
    }
}

// 判断两直线是否平行
const bool Geo::is_parallel(const Line &line0, const Line &line1)
{
    return Geo::is_parallel(line0.front(), line0.back(), line1.front(), line1.back());
}

判断多边形是否为矩形

// 判断多边形是否为矩形
const bool Geo::is_Rectangle(const Polygon &polygon)
{
    Geo::Polygon points(polygon);
    if (polygon.size() > 5)
    {
        for (size_t i = 2, count = polygon.size(); i < count; ++i)
        {
            if (Geo::is_inside(points[i - 1], points[i - 2], points[i]))
            {
                points.remove(--i);
                --count;
            }
        }
    }
    if (polygon.size() != 5)
    {
        return false;
    }

    if (Geo::distance(points[0], points[1]) == Geo::distance(points[2], points[3]) &&
        Geo::distance(points[1], points[2]) == Geo::distance(points[0], points[3]))
    {
        const Geo::Point vec0 = points[0] - points[1], vec1 = points[2] - points[1];
        return std::abs(vec0.x * vec1.x + vec0.y * vec1.y) == 0;
    }
    else
    {
        return false;
    }
}

判断点是否在直线左侧

// 判断点是否在直线的左侧
const bool Geo::is_on_left(const Point &point, const Point &start, const Point &end)
{
    return (end.x - start.x) * (point.y - start.y) - (end.y - start.y) * (point.x - end.x) > 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值