Java 检查四个点是否构成平行四边形(Check whether four points make a parallelogram)

Java判断四点是否构成平行四边形

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

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

例子:

点 = [(0, 0), (4, 0), (1, 3), (5, 3)]

以上点构成平行四边形。

点 = [(0, 0), (2, 0), (4, 0), (2, 2)]

以上点不构成平行四边形,因为前三个点本身是直线。

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

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

正方形检查: 

JavaScript 检查给定的四个点是否形成正方形:JavaScript 检查给定的四个点是否形成正方形(Check if given four points form a square)-优快云博客
C# 检查给定的四个点是否形成正方形:C# 检查给定的四个点是否形成正方形(Check if given four points form a square)-优快云博客
Python 检查给定的四个点是否形成正方形:Python 检查给定的四个点是否形成正方形(Check if given four points form a square)-优快云博客
Java 检查给定的四个点是否形成正方形:Java 检查给定的四个点是否形成正方形(Check if given four points form a square)-优快云博客
C++ 检查给定的四个点是否形成正方形:C++ 检查给定的四个点是否形成正方形(Check if given four points form a square)-优快云博客

长方形检查:

JavaScript 检查四条线段是否形成一个矩形:JavaScript 检查四条线段是否形成一个矩形(Check if four segments form a rectangle)-优快云博客
C# 检查四条线段是否形成一个矩形:C# 检查四条线段是否形成一个矩形(Check if four segments form a rectangle)-优快云博客
Python 检查四条线段是否形成一个矩形:Python 检查四条线段是否形成一个矩形(Check if four segments form a rectangle)-优快云博客
Java 检查四条线段是否形成一个矩形:Java 检查四条线段是否形成一个矩形(Check if four segments form a rectangle)-优快云博客
C++ 检查四条线段是否形成一个矩形:C++ 检查四条线段是否形成一个矩形(Check if four segments form a rectangle)-优快云博客

示例代码:

import java.util.*;

public class Main {
    // structure to represent a point
    static class Point {
        double x, y;
        Point() { }
        Point(double x, double y) {
            this.x = x;
            this.y = y;
        }

        // defining equals and hashCode method to compare two points
        @Override
        public boolean equals(Object obj) {
            if (obj == this) return true;
            if (!(obj instanceof Point)) return false;
            Point other = (Point) obj;
            return Double.compare(x, other.x) == 0
                    && Double.compare(y, other.y) == 0;
        }
        
        @Override
        public int hashCode() {
            return Objects.hash(x, y);
        }
    }

    // Utility method to return mid point of two points
    static Point getMidPoint(Point[] points, int i, int j) {
        return new 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
    static boolean isParallelogram(Point[] points) { 
        Map<Point, List<Point>> midPointMap = new HashMap<>();

        // 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
                if (!midPointMap.containsKey(temp)) {
                    midPointMap.put(temp, new ArrayList<>());
                }
                midPointMap.get(temp).add(new Point(i, j));
            }
        }

        int two = 0, one = 0;

        // looping over (midpoint, (corresponding pairs)) 
        // map to check the occurrence of each midpoint
        for (List<Point> pointsList : midPointMap.values()) {
            int size = pointsList.size();
            // updating midpoint count which occurs twice
            if (size == 2) {
                two++;
            }
            // updating midpoing count which occurs once
            else if (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
    public static void main(String[] args) {
        Point[] points = new Point[4];

        points[0] = new Point(0, 0);
        points[1] = new Point(4, 0);
        points[2] = new Point(1, 3);
        points[3] = new Point(5, 3);

        if (isParallelogram(points)) {
            System.out.println("Given points form a parallelogram");
        } else {
            System.out.println("Given points do not form a parallelogram");
        }
    }
}

// This code is contributed by Prince Kumar

输出:

Given points form a parallelogram

时间复杂度: O(p²logp),其中 p 是点的数量

辅助空间:O(p²),其中 p 是点的数量

方法 2:使用向量:

• 另一种检查四个点是否构成平行四边形的方法是使用向量运算。我们可以计算由点对构成的向量,并检查它们是否满足平行四边形的性质。

• 以下是此方法的算法:

• 以A、B、C、D四个点作为输入。

• 使用公式 (B - A) 和 (D - C) 计算向量 AB 和 CD。

• 使用公式 (C - A) 和 (D - B) 计算向量 AC 和 BD。

• 通过计算 AB 和 CD 的叉积来判断它们是否平行。如果叉积为零,则它们平行。

• 通过计算AC和BD的叉积来判断它们是否平行。如果叉积为零,则它们平行。

• 如果 AB 和 CD 平行,且 AC 和 BD 平行,则这些点形成平行四边形。

以下是此方法的 Java 代码实现:

// structure to represent a point
class Point {
    double x, y;

    Point() {}

    Point(double x, double y)
    {
        this.x = x;
        this.y = y;
    }
}

public class ParallelogramCheck {

    // Calculates the cross product of two vectors.
    static double crossProduct(Point a, Point b)
    {
        return a.x * b.y - a.y * b.x;
    }

    // Checks if the given points form a parallelogram.
    static boolean isParallelogram(Point A, Point B,
                                   Point C, Point D)
    {
        // Calculate vectors AB, CD, AC, and BD
        Point AB = new Point(B.x - A.x, B.y - A.y);
        Point CD = new Point(D.x - C.x, D.y - C.y);
        Point AC = new Point(C.x - A.x, C.y - A.y);
        Point BD = new Point(D.x - B.x, D.y - B.y);

        // Check if AB and CD are parallel
        if (crossProduct(AB, CD) == 0) {
            // Check if AC and BD are parallel
            if (crossProduct(AC, BD) == 0) {
                // Both pairs of opposite sides are
                // parallel,
                // it is a parallelogram
                return true;
            }
        }

        return false; // Not a parallelogram
    }

    // Driver code to test above method

    public static void main(String[] args)
    {
        // Define the points of the quadrilateral
        Point A = new Point(0, 0);
        Point B = new Point(4, 0);
        Point C = new Point(1, 3);
        Point D = new Point(5, 3);

        // Check if the given points form a parallelogram
        if (isParallelogram(A, B, C, D))
            System.out.println(
                "Given points form a parallelogram");
        else
            System.out.println(
                "Given points do not form a parallelogram");
    }
}

输出:

Given points form a parallelogram

时间复杂度:O(n^2logn),其中 n 是点的数量

辅助空间:O(n^2),因为地图数据结构用于存储中点,并且可以存储的最坏情况下的中点数量是 n^2。

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

评论
成就一亿技术人!
拼手气红包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、付费专栏及课程。

余额充值