C# 检查四个线段是否形成一个矩形(Check if four segments form a rectangle)

我们有四条线段作为其端点的一对坐标。我们需要判断这四条线段是否构成矩形。 
示例: 

输入: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)-优快云博客

        我们可以通过使用矩形的属性来解决这个问题。首先,我们检查线段的唯一端点总数,如果这些点的数量不等于 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. 

 输出: 

时间复杂度:

辅助空间: O(n)

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

csdn_aspnet

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值