我们有四条线段作为其端点的一对坐标。我们需要判断这四条线段是否构成矩形。
示例:
输入: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:JavaScript 如何检查给定的四个点是否形成一个正方形(How to check if given four points form a square)-优快云博客
C#:C# 如何检查给定的四个点是否形成一个正方形(How to check if given four points form a square)_四个坐标判断正方形-优快云博客
Python:Python 如何检查给定的四个点是否形成一个正方形(How to check if given four points form a square)-优快云博客
Java:Java 如何检查给定的四个点是否形成一个正方形(How to check if given four points form a square)_算法题目 四个点检验正方形-优快云博客
C++:C++ 如何检查给定的四个点是否形成一个正方形(How to check if given four points form a square)_顺时针读入一个四边形的各点a,b,c,d的x-y值,并判断它是否能构成正方形(zhengfangx-优快云博客
我们可以通过使用矩形的属性来解决这个问题。首先,我们检查线段的唯一端点总数,如果这些点的数量不等于 4,则线段不能构成矩形。然后我们检查所有点对之间的距离,最多应该有 3 个不同的距离,一个用于对角线,两个用于边,最后我们将检查这三个距离之间的关系,对于构成矩形的线段,这些距离应该满足勾股关系,因为矩形的边和对角线构成直角三角形。如果它们满足上述条件,那么我们将线段构成的多边形标记为矩形,否则不是。
示例代码:
/*package whatever //do not write package name here */
import java.io.*;
import java.util.*;
// java program to check whether it is possible
// to make a rectangle from 4 segments
public class Main {
static int N = 4;
// Utility method to return square of distance
// between two points
public static int getDis(String a, String b)
{
String[] a1 = a.split(",");
String[] b1 = b.split(",");
return (Integer.parseInt(a1[0]) - Integer.parseInt(b1[0]))*(Integer.parseInt(a1[0]) - Integer.parseInt(b1[0])) + (Integer.parseInt(a1[1]) - Integer.parseInt(b1[1]))*(Integer.parseInt(a1[1]) - Integer.parseInt(b1[1]));
}
// method returns true if line Segments make
// a rectangle
public static boolean isPossibleRectangle(int[][] segments)
{
HashSet<String> st = new HashSet<>();
// putting all end points in a set to
// count total unique points
for (int i = 0; i < N; i++)
{
st.add(segments[i][0] + "," + segments[i][1]);
st.add(segments[i][2] + "," + segments[i][3]);
}
// 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'
HashSet<Integer> dist = new HashSet<>();
// calculating distance between all pair of
// end points of line segments
for(String it1 : st){
for(String it2 : st){
if(it1 != it2){
dist.add(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 = new int[3];
int i = 0;
for(int it: dist){
distance[i] = it;
i = i + 1;
}
// 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]);
}
public static void main(String[] args) {
int[][] segments =
{
{4, 2, 7, 5},
{2, 4, 4, 2},
{2, 4, 5, 7},
{5, 7, 7, 5}
};
if(isPossibleRectangle(segments) == true){
System.out.println("Yes");
}
else{
System.out.println("No");
}
}
}
// The code is contributed by Nidhi goel.
输出:
是
时间复杂度:
辅助空间: O(n)
如果您喜欢此文章,请收藏、点赞、评论,谢谢,祝您快乐每一天。