给定四条线段,每条线段的端点坐标为一个坐标。我们需要判断这四条线段是否构成一个矩形。
示例:
输入:segments[] = [(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 检查给定的四个点是否形成正方形(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)-优快云博客
我们可以利用矩形的性质来解决这个问题。首先,我们检查线段的唯一端点总数,如果这些点的数量不等于4,则该线段不能构成矩形。然后,我们检查所有点对之间的距离,最多应有3个不同的距离,一个用于对角线,两个用于边。最后,我们将检查这三个距离之间的关系。对于构成矩形的线段,这些距离应满足勾股定理,因为矩形的边和对角线构成一个直角三角形。如果它们满足上述条件,则我们将由线段构成的多边形标记为矩形,否则不是。
示例代码:
// C# program to check whether it is possible
// to make a rectangle from 4 segments
using System;
using System.Collections.Generic;
class GFG {
public static int N = 4;
// Utility method to return square of distance
// between two points
public static int getDis(KeyValuePair<int,int> a, KeyValuePair<int,int> b)
{
return (a.Key - b.Key)*(a.Key - b.Key) +
(a.Value - b.Value)*(a.Value - b.Value);
}
// method returns true if line Segments make
// a rectangle
public static bool isPossibleRectangle(int[,] segments)
{
HashSet<KeyValuePair<int, int>> st = new HashSet<KeyValuePair<int, int>>();
// putting all end points in a set to
// count total unique points
for (int j = 0; j < N; j++)
{
st.Add(new KeyValuePair<int, int>(segments[j,0], segments[j,1]));
st.Add(new KeyValuePair<int, int>(segments[j,2], segments[j,3]));
}
// If total unique points are not 4, then
// they can't make a rectangle
if (st.Count != 4)
return false;
// dist will store unique 'square of distances'
HashSet<int> dist = new HashSet<int>();
// calculating distance between all pair of
// end points of line segments
foreach(var it1 in st){
foreach(var it2 in st){
if(it1.Key != it2.Key && it1.Value != it2.Value){
dist.Add(getDis(it1, it2));
}
}
}
// if total unique distance are more than 3,
// then line segment can't make a rectangle
if (dist.Count > 3)
return false;
// copying distance into array. Note that set maintains
// sorted order.
int[] distance = new int[3];
int i = 0;
foreach(var it in dist){
distance[i] = it;
i = i + 1;
}
// If line seqments form a square
if (dist.Count == 2)
return (2*distance[0] == distance[1]);
// distance of sides should satisfy pythagorean
// theorem
return (distance[0] + distance[1] == distance[2]);
}
// Driver code
public static void Main()
{
int[,] segments = {
{4, 2, 7, 5},
{2, 4, 4, 2},
{2, 4, 5, 7},
{5, 7, 7, 5}
};
if(isPossibleRectangle(segments) == true){
Console.WriteLine("Yes");
}
else{
Console.WriteLine("No");
}
}
}
// The code is contributed by Nidhi goel.
输出:
Yes
时间复杂度: O(n² logn)
辅助空间: O(n)
如果您喜欢此文章,请收藏、点赞、评论,谢谢,祝您快乐每一天。
1万+

被折叠的 条评论
为什么被折叠?



