如何判断3个或更多线段的列表是否构成封闭区域(3D)? [英] How can I tell if a list of 3 or more line segments form a closed region (in 3D)?

查看:368
本文介绍了如何判断3个或更多线段的列表是否构成封闭区域(3D)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,这是我到目前为止:

假设:您至少有3个共面线段


  1. 按开始点的x坐标或类似的方式对列表进行排序


  2. 第2行的开始点位于第1行的结束点,依此类推。


  3. 如果最后一个结束点位于第一个结束点,那么我知道这是一个封闭的区域


我会如何去实施C-Sharp?你认为它甚至会起作用吗?如果是这样,我应该使用哪种排序算法?

解决方案

假设线段如下所示。
$ b

  class Segment 
{
public Point A {get;组; }
public Point B {get;组; }
}

class Point
{
public double X {get;组; }
public double Y {get;组; }
public double Z {get;组; }
}

假设是包含3个线段的 IEnumerable< Segment> 。然后你可以检查3条线段是否形成一个封闭的三角形,如下所示。

  bool closed = segments 
.SelectMany (segment => new [] {segment.A,segment.B})
.GroupBy(point => new {point.X,point.Y,point.Z})
.All (group => group.Count()== 2);

这并不考虑 A B 是一样的。您可以轻松添加,并自行决定在这种情况下您要做什么。


OK, here's what I have so far:

Assumptions: You have at least 3 coplanar line segments

  1. Sort the list by x-coordinate of start point or something

  2. Step through the list and see if the start point of line2 is at the end point of line 1 and so on

  3. If the end point of the last one is on the start point of the first one, then I know it is a closed region

How would I go about implementing that in C-Sharp? Do you think it will even work? If so, what sorting algorithm should I use?

解决方案

Suppose a line segment is represented as follows.

class Segment
{
    public Point A { get; set; }
    public Point B { get; set; }
}

class Point
{
    public double X { get; set; }
    public double Y { get; set; }
    public double Z { get; set; }
}

Suppose segments is an IEnumerable<Segment> containing 3 line segments. Then you can check if the 3 line segments form a closed triangle as follows.

bool closed = segments
    .SelectMany(segment => new[] { segment.A, segment.B })
    .GroupBy(point => new { point.X, point.Y, point.Z })
    .All(group => group.Count() == 2);

This doesn't consider the degenerate case of segments for which A and B are the same. You can easily add that in and decide for yourself what you want to do in that case.

这篇关于如何判断3个或更多线段的列表是否构成封闭区域(3D)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆