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

给定四条线段,每条线段的端点坐标为一个坐标。我们需要判断这四条线段是否构成一个矩形。 
示例: 
 

输入: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)

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

<think>好的,我现在需要回答用户的问题:如何在WPF中使用C#通过Path绘制矩形。首先,我需要回忆一下WPF中Path元素的使用方法,以及如何用C#代码来动态创建和配置Path对象。 首先,Path类在WPF中属于System.Windows.Shapes命名空间,通常用于绘制复杂的形状。用户想用Path来画矩形,而通常矩形可以用Rectangle元素,但使用Path可能更灵活,比如绘制圆角矩形或其他变体。 接下来,我需要确定如何用Path的Data属性来定义矩形。Path.Data通常使用Geometry对象,比如RectangleGeometry或PathGeometry。使用RectangleGeometry比较简单,直接指定Rect和可能的圆角半径。另一种方法是使用PathGeometry和PathFigure组合,手动添加线段来构造矩形。 用户可能希望用C#代码动态创建这个Path,而不是在XAML中静态定义。因此,我需要编写C#代码实例化Path对象,设置其Stroke、Fill等属性,并正确配置Data属性。 考虑到用户提供的引用中有关于样式的部分,比如引用[1]中的Polyline示例,这可能是用XAML代码绘制的。但用户需要的是C#代码,所以需要转换XAML的思维方式到C#。例如,引用[1]中的Polyline用了Points属性,而Path可能需要更复杂的Geometry设置。 步骤可能包括: 1. 创建Path对象。 2. 设置Stroke和Fill等属性。 3. 创建RectangleGeometry或PathGeometry来定义矩形形状。 4. 将Geometry赋值给Path.Data。 5. 将Path添加到WPF的视觉树中,比如某个Canvas或Grid。 另外,需要考虑坐标系统。例如,矩形的起始点(比如左上角坐标)和宽度、高度。如果是使用PathGeometry,需要构建四个LineSegment来形成闭合的矩形路径,或者使用AddLines方法。 可能的问题:用户是否需要圆角矩形?如果是标准矩形RectangleGeometry足够;如果需要更复杂的路径,可能需要PathGeometry。 现在,根据引用[1]中的Polyline示例,用户可能希望类似地用C#代码生成Path元素。因此,我需要提供一个代码示例,展示如何用C#创建Path,设置其Data为一个矩形,并添加到界面中。 可能的代码结构: - 实例化Path对象。 - 创建RectangleGeometry,指定Rect(比如位置和尺寸)。 - 将RectangleGeometry赋值给Path.Data。 - 设置Stroke和Fill的Brush。 - 将Path添加到父容器(如Canvas)的Children集合中。 或者,使用PathGeometry: - 创建PathGeometry和PathFigure。 - 添加LineSegment或PolyLineSegment来绘制四条边。 - 确保PathFigure.IsClosed为true,以闭合矩形。 需要检查这两种方法的正确性,并选择更合适的示例。RectangleGeometry更简单,适合标准矩形;PathGeometry更灵活,适合自定义路径。 另外,用户可能需要动态调整矩形的位置和大小,因此在代码中需要指定具体的坐标值,比如左上角坐标和宽高。 最后,需要确保引用的正确性。根据用户提供的引用,引用[1]中有关于Polyline的C#代码示例,这可能与Path的使用相关,但需要确认是否有直接引用的必要。但用户的问题是关于Path的,而引用中的例子是Polyline,所以可能不需要直接引用,但可以参考类似的代码结构。 总结步骤: 1. 创建Path对象。 2. 配置其Data属性为RectangleGeometry或PathGeometry。 3. 设置其他属性如Stroke、Fill。 4. 将Path添加到界面容器中。 现在,编写具体的代码示例,并解释每一步的作用。同时,确保使用正确的命名空间,比如System.Windows.Shapes.Path和System.Windows.Media.Geometry相关类。 可能的代码示例: 使用RectangleGeometry: Path path = new Path(); path.Stroke = Brushes.Black; path.Fill = Brushes.LightBlue; path.StrokeThickness = 2; RectangleGeometry rectGeometry = new RectangleGeometry(); rectGeometry.Rect = new Rect(50, 50, 100, 60); // X, Y, Width, Height path.Data = rectGeometry; // 添加到Canvas或其他容器 myCanvas.Children.Add(path); 或者使用PathGeometry: PathGeometry pathGeometry = new PathGeometry(); PathFigure figure = new PathFigure(); figure.StartPoint = new Point(50, 50); figure.Segments.Add(new LineSegment(new Point(150, 50), true)); // 上边 figure.Segments.Add(new LineSegment(new Point(150, 110), true)); // 右边 figure.Segments.Add(new LineSegment(new Point(50, 110), true)); // 下边 figure.Segments.Add(new LineSegment(new Point(50, 50), true)); // 左边 figure.IsClosed = true; pathGeometry.Figures.Add(figure); path.Data = pathGeometry; 两种方法都可以,但第一种更简洁。用户可能希望看到这两种方法中的一种或两种,以了解不同实现方式。 在回答中,应该说明这两种方法,并提供代码示例。同时,指出使用Path与Rectangle元素的不同,比如Path更灵活,可以绘制更复杂的形状,而Rectangle元素更简单。 可能还需要提到如何通过C#代码动态调整属性,比如改变位置或大小,或者在运行时修改Path的数据。 最后,生成相关问题,例如如何绘制圆角矩形,如何动态改变形状,或者如何优化绘制性能等。</think>在WPF中使用C#通过`Path`绘制矩形,可以通过两种主要方式实现:使用`RectangleGeometry`或手动构建`PathGeometry`。以下是具体实现步骤和示例代码: --- ### **方法1:使用RectangleGeometry** `RectangleGeometry`直接定义矩形的位置和尺寸,适合标准矩形绘制: ```csharp // 创建Path对象并设置样式 Path path = new Path { Stroke = Brushes.Black, Fill = Brushes.LightBlue, StrokeThickness = 2 }; // 定义矩形的位置和尺寸(X坐标, Y坐标, 宽度, 高度) RectangleGeometry rectGeometry = new RectangleGeometry { Rect = new Rect(50, 50, 100, 60) }; // 将Geometry赋值给Path.Data path.Data = rectGeometry; // 将Path添加到Canvas或其他容器中 myCanvas.Children.Add(path); ``` --- ### **方法2:使用PathGeometry** 通过`PathGeometry`手动定义矩形的四个顶点,适合需要更灵活路径控制的场景: ```csharp Path path = new Path { Stroke = Brushes.Red, StrokeThickness = 3 }; // 创建PathGeometry并定义路径 PathGeometry pathGeometry = new PathGeometry(); PathFigure figure = new PathFigure { StartPoint = new Point(50, 50), // 起点坐标(左上角) IsClosed = true // 闭合路径以形成矩形 }; // 添加线段定义矩形四条边 figure.Segments.Add(new LineSegment(new Point(150, 50), true)); // 上边 figure.Segments.Add(new LineSegment(new Point(150, 110), true)); // 右边 figure.Segments.Add(new LineSegment(new Point(50, 110), true)); // 下边 pathGeometry.Figures.Add(figure); path.Data = pathGeometry; myCanvas.Children.Add(path); ``` --- ### **关键说明** 1. **坐标系**:WPF的坐标系原点默认为容器的左上角,向右为X轴正方向,向下为Y轴正方向。 2. **动态调整**:通过修改`Rect`属性或`PathFigure`的坐标点,可实现动态调整矩形大小和位置。 3. **性能优化**:对于静态图形,优先使用XAML声明;动态生成内容时,C#代码更灵活[^1]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

hefeng_aspnet

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

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

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

打赏作者

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

抵扣说明:

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

余额充值