C++ 检查四个线段是否形成一个矩形(Check if four segments form a rectangle)

我们有四条线段作为其端点的一对坐标。我们需要判断这四条线段是否构成矩形。 
示例: 

输入:segment[] = [(4, 2), (7, 5), 
                       (2, 4), (4, 2), 
                       (2, 4), (5, 7), 
                       (5, 7), (7, 5)]
输出:
是 给定这些线段,可以组成一个长度为 3X2 的矩形。

输入:segment[] = [(7, 0), (10, 0), 
                     (7, 0), (7, 3), 
                     (7, 3), (10, 2), 
                     (10, 2), (10, 0)]
输出:否
这些线段不能组成矩形。

上述示例如下所示:
这个问题主要是:如何检查给定的四个点是否形成一个正方形
JavaScript:https://blog.youkuaiyun.com/hefeng_aspnet/article/details/145686032
C#:https://blog.youkuaiyun.com/hefeng_aspnet/article/details/145686008
Python:https://blog.youkuaiyun.com/hefeng_aspnet/article/details/145685981
Java:https://blog.youkuaiyun.com/hefeng_aspnet/article/details/145685948
C++:https://blog.youkuaiyun.com/hefeng_aspnet/article/details/145685863

        我们可以通过使用矩形的属性来解决这个问题。首先,我们检查线段的唯一端点总数,如果这些点的数量不等于 4,则线段不能构成矩形。然后我们检查所有点对之间的距离,最多应该有 3 个不同的距离,一个用于对角线,两个用于边,最后我们将检查这三个距离之间的关系,对于构成矩形的线段,这些距离应该满足勾股关系,因为矩形的边和对角线构成直角三角形。如果它们满足上述条件,那么我们将线段构成的多边形标记为矩形,否则不是。

示例代码:

// C++ program to check whether it is possible 
// to make a rectangle from 4 segments 
#include <bits/stdc++.h> 
using namespace std; 
#define N 4 
 
// structure to represent a segment 
struct Segment 

    int ax, ay; 
    int bx, by; 
}; 
 
// Utility method to return square of distance 
// between two points 
int getDis(pair<int, int> a, pair<int, int> b) 

    return (a.first - b.first)*(a.first - b.first) + 
        (a.second - b.second)*(a.second - b.second); 

 
// method returns true if line Segments make 
// a rectangle 
bool isPossibleRectangle(Segment segments[]) 

    set< pair<int, int> > st; 
 
    // putting all end points in a set to 
    // count total unique points 
    for (int i = 0; i < N; i++) 
    { 
        st.insert(make_pair(segments[i].ax, segments[i].ay)); 
        st.insert(make_pair(segments[i].bx, segments[i].by)); 
    } 
 
    // If total unique points are not 4, then 
    // they can't make a rectangle 
    if (st.size() != 4) 
        return false; 
 
    // dist will store unique 'square of distances' 
    set<int> dist; 
 
    // calculating distance between all pair of 
    // end points of line segments 
    for (auto it1=st.begin(); it1!=st.end(); it1++) 
        for (auto it2=st.begin(); it2!=st.end(); it2++) 
            if (*it1 != *it2) 
                dist.insert(getDis(*it1, *it2)); 
 
    // if total unique distance are more than 3, 
    // then line segment can't make a rectangle 
    if (dist.size() > 3) 
        return false; 
 
    // copying distance into array. Note that set maintains 
    // sorted order. 
    int distance[3]; 
    int i = 0; 
    for (auto it = dist.begin(); it != dist.end(); it++) 
        distance[i++] = *it; 
 
    // If line seqments form a square 
    if (dist.size() == 2) 
    return (2*distance[0] == distance[1]); 
 
    // distance of sides should satisfy pythagorean 
    // theorem 
    return (distance[0] + distance[1] == distance[2]); 

 
// Driver code to test above methods 
int main() 

    Segment segments[] = 
    { 
        {4, 2, 7, 5}, 
        {2, 4, 4, 2}, 
        {2, 4, 5, 7}, 
        {5, 7, 7, 5} 
    }; 
 
    (isPossibleRectangle(segments))?cout << "Yes\n":cout << "No\n"; 

输出: 

时间复杂度:

辅助空间: O(n)

如果您喜欢此文章,请收藏、点赞、评论,谢谢,祝您快乐每一天。

C++中,计算线段与多边形(或多段线)的交点通常涉及几何学算法,如射线投射法、点到直线距离、叉积等。这里提供一个简单的示例,我们将假设多边形是由一系列线段构成的简单闭合路径。 首先,你需要有以下几个函数: 1. **判断两点是否线段上**:检查端点和给定点的顺序以及向量的方向。 2. **计算线段长度**:用于比较线段和多边形边缘。 3. **点在线段内或边界**:检查点与线段的关系。 4. **点到线段的距离**:找到最近点的位置。 5. **计算两条线段的交点**:如果它们相交,返回交点;否则返回无。 以下是一个简化版的代码片段,实际应用中需要更复杂的算法,比如Sutherland-Hodgman算法: ```cpp #include <vector> #include <cmath> struct Point { double x, y; }; struct Segment { Point start, end; }; // 简化版本,仅处理两个线段相交的情况 bool linesIntersect(Segment line1, Segment line2) { double crossProduct = (line1.end.x - line1.start.x) * (line2.end.y - line2.start.y) - (line1.end.y - line1.start.y) * (line2.end.x - line2.start.x); if (crossProduct == 0) // 平行 return false; double u1 = ((line2.end.x - line2.start.x) * (line1.start.x - line2.start.x) + (line2.end.y - line2.start.y) * (line1.start.y - line2.start.y)) / crossProduct; double u2 = ((line1.end.x - line1.start.x) * (line1.start.x - line2.start.x) + (line1.end.y - line1.start.y) * (line1.start.y - line2.start.y)) / crossProduct; // 检查u值是否在[0, 1]范围内 return u1 >= 0 && u1 <= 1 && u2 >= 0 && u2 <= 1; } Point findIntersection(Segment seg1, Segment seg2) { if (!linesIntersect(seg1, seg2)) return {0, 0}; // 如果不相交,返回原点作为假象交点 double u1 = ((seg2.end.x - seg2.start.x) * (seg1.start.x - seg2.start.x) + (seg2.end.y - seg2.start.y) * (seg1.start.y - seg2.start.y)) / crossProduct; double u2 = ((seg1.end.x - seg1.start.x) * (seg1.start.x - seg2.start.x) + (seg1.end.y - seg1.start.y) * (seg1.start.y - seg2.start.y)) / crossProduct; return {seg1.start.x + u1 * (seg1.end.x - seg1.start.x), seg1.start.y + u1 * (seg1.end.y - seg1.start.y)}; } // 示例多边形由Segment[] polygon_segments表示 std::vector<Point> findPolygonIntersection(Segment* polygon_segments, int n) { std::vector<Point> intersections; for (int i = 0; i < n; ++i) { for (int j = i + 1; j < n; ++j) { intersections.push_back(findIntersection(polygon_segments[i], polygon_segments[j])); } } // 可能需要进一步过滤掉重复的交点,以及排除那些位于多边形外或内部的点 // 这里省略了细节... return intersections; } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hefeng_aspnet

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值