获取离散点的边界点

今天公司要求实现一个算法,在给出一堆离散点中,画出最外层的边界。实际就是从离散点中找出最外面的点(或称边界点),这些点用线连接起来即可。

一、实现的原理:

1.任意点(point1)找出最远距离的一个点(point2),该点point2即为其中一个边界点。
2.point1与point2组成的向量v1(point1 - point2)中找出与其最大夹角的一个向量v2。
3.重复:v2中找与其最大夹角的向量v3.....
4.直到最后找到的向量方向为点point2的向量时结束。

二、这里实现的代码为C#,只要搞懂原理,可以转为各种语言的实现

1.首先是平面向量类 PlaneVector

PlaneVector
  1       ///   <summary>
  2       ///  平面向量类
  3       ///   </summary>
  4       public   struct  PlaneVector
  5      {
  6           public   static   readonly  PlaneVector Empty;
  7 
  8           ///   <summary>
  9           ///  向量的X值
 10           ///   </summary>
 11           private   double  _X;
 12 
 13           ///   <summary>
 14           ///  向量的Y值
 15           ///   </summary>
 16           private   double  _Y;
 17 
 18           public   double  X
 19          {
 20               get
 21              {
 22                   return  _X;
 23              }
 24               set
 25              {
 26                  _X  =  value;
 27              }
 28          }
 29 
 30           public   double  Y
 31          {
 32               get
 33              {
 34                   return  _Y;
 35              }
 36               set
 37              {
 38                  _Y  =  value;
 39              }
 40          }
 41 
 42           public  PlaneVector( double  x,  double  y)
 43          {
 44              _X  =  x;
 45              _Y  =  y;
 46          }
 47 
 48           public  PlaneVector(Point destPoint, Point sourcePoint)
 49          {
 50              _X  =  destPoint.X  -  sourcePoint.X;
 51              _Y  =  destPoint.Y  -  sourcePoint.Y;
 52          }
 53 
 54           public  PlaneVector(PointF destPoint, PointF sourcePoint)
 55          {
 56              _X  =  destPoint.X  -  sourcePoint.X;
 57              _Y  =  destPoint.Y  -  sourcePoint.Y;
 58          }
 59 
 60           public  PlaneVector(PlaneVector destPlaneVector, PlaneVector sourcePlaneVector)
 61          {
 62              _X  =  destPlaneVector._X  -  sourcePlaneVector._X;
  • 1
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
获取离散点的边界可以通过计算凸包来实现。凸包是一个包含所有点的最小凸多边形,它的边界就是离散点的边界。 在 VB.NET 中,可以使用 `System.Windows.Point` 类表示一个点,使用 `System.Windows.Media.PathGeometry` 类表示一个几何路径。可以通过以下步骤获取离散点的边界: 1. 将所有点存储在一个 `List(Of Point)` 中。 2. 使用 `Geometry.Combine` 方法将所有点转换为一个 `PathGeometry`。 3. 使用 `PathGeometry.GetFlattenedPathGeometry` 方法将 `PathGeometry` 扁平化为一个 `PathGeometry`。 4. 使用 `PathGeometry.GetWidenedPathGeometry` 方法将 `PathGeometry` 扩展为一个 `PathGeometry`。 5. 使用 `PathGeometry.GetOutlinedPathGeometry` 方法将 `PathGeometry` 转换为一个 `PathGeometry`,其中只包含边界路径。 6. 使用 `PathGeometry.Figures` 属性获取边界路径。 下面是一个获取离散边界的示例代码: ```vb Imports System.Collections.Generic Imports System.Windows Imports System.Windows.Media Public Module DiscretePointsBoundary Public Function GetBoundary(points As List(Of Point)) As PathFigureCollection ' 将所有点转换为 PathGeometry。 Dim pathGeometry As New PathGeometry() pathGeometry.AddGeometry(New PathGeometry(New PathFigure() {New PathFigure(points(0), points.Select(Function(p) New LineSegment(p, True)).ToList(), True)})) ' 扁平化 PathGeometry。 Dim flattenedGeometry As PathGeometry = pathGeometry.GetFlattenedPathGeometry() ' 扩展 PathGeometry。 Dim widenGeometry As PathGeometry = flattenedGeometry.GetWidenedPathGeometry(New Pen(Brushes.Black, 1)) ' 获取边界 PathGeometry。 Dim outlineGeometry As PathGeometry = widenGeometry.GetOutlinedPathGeometry() ' 获取边界路径。 Return outlineGeometry.Figures End Function End Module ``` 示例代码中,`GetBoundary` 方法接受一个 `List(Of Point)` 参数,返回一个 `PathFigureCollection` 对象,其中包含离散点的边界路径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值