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

📅  最后修改于: 2021-04-27 06:19:13             🧑  作者: 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个中点。现在,要使四个点成为平行四边形,中点中的两个应该相等,而其余的应该不同。
在下面的代码中,我们创建了一个地图,其中存储了与每个中点相对应的对。在计算了所有中点之后,我们遍历了地图并检查了每个中点的出现。如果恰好一个中点发生了两次,而其他中点发生了一次,则给定四个点,则制作一个平行四边形,否则不做。

// 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