📜  检查四个点是否构成平行四边形

📅  最后修改于: 2021-10-23 08:54:24             🧑  作者: Mango

给定二维空间中的四个点,我们需要找出它们是否构成平行四边形。

平行四边形。

平行四边形有四个边。相对的两条边平行且长度相同。

例子:

Points = [(0, 0),  (4, 0),  (1, 3),  (5, 3)]
Above points make a parallelogram.

Points = [(0, 0), (2, 0), (4, 0), (2, 2)]
Above points does not make a parallelogram 
as first three points itself are linear.

检查正方形和矩形的问题可以从正方形检查和矩形检查中读出,但在这个问题中,我们需要检查平行四边形。平行四边形的主要性质是平行四边形的对边平行且等长,平行四边形的对角线相互平分。我们使用第二个属性来解决这个问题。由于有四个点,我们可以通过考虑每一对得到总共 6 个中点。现在要让四个点组成平行四边形,其中 2 个中点应该相等,其余的应该不同。
在下面的代码中,我们创建了一个地图,它存储与每个中点对应的对。计算完所有中点后,我们遍历地图并检查每个中点的出现情况,如果恰好一个中点出现两次而其他中点出现一次,则给定四个点构成平行四边形,否则不会。

// C++ code to test whether four points make a 
// parallelogram or not
#include 
using namespace std;
  
// structure to represent a point
struct point {
    double x, y;
    point() { }
    point(double x, double y)
        : x(x), y(y) { }
  
    // defining operator < to compare two points
    bool operator<(const point& other) const
    {
        if (x < other.x) {
            return true;
        } else if (x == other.x) {
            if (y < other.y) {
                return true;
            }
        }
        return false;
    }
};
  
// Utility method to return mid point of two points
point getMidPoint(point points[], int i, int j)
{
    return point((points[i].x + points[j].x) / 2.0, 
                (points[i].y + points[j].y) / 2.0);
}
  
// method returns true if point of points array form 
// a parallelogram
bool isParallelogram(point points[])
{ 
    map > midPointMap;
  
    // looping over all pairs of point to store their
    // mid points
    int P = 4;
    for (int i = 0; i < P; i++) {
        for (int j = i + 1; j < P; j++) {
            point temp = getMidPoint(points, i, j);
  
            // storing point pair, corresponding to
            // the mid point
            midPointMap[temp].push_back(point(i, j));
        }
    }
  
    int two = 0, one = 0;
  
    // looping over (midpoint, (corresponding pairs)) 
    // map to check the occurence of each midpoint
    for (auto x : midPointMap) {
          
        // updating midpoint count which occurs twice
        if (x.second.size() == 2) 
            two++;
          
        // updating midpoing count which occurs once
        else if (x.second.size() == 1) 
            one++;
          
        // if midpoint count is more than 2, then 
        // parallelogram is not possible
        else
            return false;     
    }
  
    // for parallelogram, one mid point should come 
    // twice and other mid points should come once
    if (two == 1 && one == 4) 
        return true;
      
    return false;
}
  
// Driver code to test above methods
int main()
{
    point points[4];
  
    points[0] = point(0, 0);
    points[1] = point(4, 0);
    points[2] = point(1, 3);
    points[3] = point(5, 3);
  
    if (isParallelogram(points)) 
        cout << "Given points form a parallelogram";
    else
        cout << "Given points does not form a "
                "parallelogram";
    return 0;
}

输出:

Given points form a parallelogram

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程。