如何判断3个或更多线段的列表是否构成封闭区域(3D)? [英] How can I tell if a list of 3 or more line segments form a closed region (in 3D)?
问题描述
好的,这是我到目前为止:
假设:您至少有3个共面线段
-
按开始点的x坐标或类似的方式对列表进行排序
-
第2行的开始点位于第1行的结束点,依此类推。
-
如果最后一个结束点位于第一个结束点,那么我知道这是一个封闭的区域
我会如何去实施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
Sort the list by x-coordinate of start point or something
Step through the list and see if the start point of line2 is at the end point of line 1 and so on
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屋!