使用 LINQ 确定 2 个图的相对位置
我有两个点数组:double[] minus
和 double[] plus
,例如:
double[] minus = new[]
{
24.043414306636713,
26.521399902043807,
23.049167719142361,
24.473177606966754,
18.238281854192408,
};
double[] plus = new[]
{
8.31219054269323,
9.5909890877229582,
11.066525870449567,
22.769068312057193,
24.733540360065991,
};
我需要根据这个数字绘制 2 个图表,并确定它们相对于每个图表的位置other:是否存在交叉点,并且其中哪个在另一个下?
我怎样才能做到这一点? TIA
(请随意重新标记问题或更改主题,我不确定使用了正确的数学术语)
编辑:这是一个 Excel 图表:
很容易确定哪个在上面,哪个在下面。
如何判断红色(加号)与蓝色(减号)有交集?也许使用 LINQ?
I have two arrays of points: double[] minus
and double[] plus
, e.g.:
double[] minus = new[]
{
24.043414306636713,
26.521399902043807,
23.049167719142361,
24.473177606966754,
18.238281854192408,
};
double[] plus = new[]
{
8.31219054269323,
9.5909890877229582,
11.066525870449567,
22.769068312057193,
24.733540360065991,
};
I need to plot 2 diagrams basing on this number and determine their positions relatively to each other: are there an intersection and which of them is under another?
How can I do that? TIA
(please feel free to retag the question or change the topic, I'm not sure in proper math terminology used)
Edit: Here's an Excel diagram:
It's easy to determine which is above and which is under.
How to determine that red (plus) has an intersection with blue (minus)? Using maybe LINQ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
此代码可以找到所有碰撞:
编辑:
我做了两种不同的方法来区分碰撞的类型(即索引之间或索引上),但如果您的目的只是检测是否存在碰撞,只需执行以下操作:
一旦发现碰撞,此代码就会返回,因此通常比上面的更快方法。
This code can found all collisions:
EDIT:
I did two different methods to distinguish the type of collisions (i.e. between indices or on indices), but if your purpouse is just to detect if there's a collision or not, just do the following:
This code returns as soon as a collision is found, so it's generally faster the the above methods.
要确定哪个更高,只需将 minus[i] 与 plus[i] 进行比较 - 较大的值就是 i 处的“较高”值。
要确定交叉点,只需跟踪哪个交叉点较高即可。当这种情况发生变化时,就会出现一个交叉点。
编辑
如果您无法跟踪历史记录,则:
To determine which is higher, just compare minus[i] to plus[i] - whichever has the greater value is the "higher" one at i.
To determine intersections, just keep track of which one is higher. When that changes, there was an intersection.
Edit
If you can't track history, then:
一种方法是将每个系列分解为线段,然后比较两个系列中相应的(按索引)线段。
由于您特别提到了 LINQ,因此这是实现这一目标的一种方法。它不是很漂亮:
输出:(
请注意,您不会获得最后一点的
PlusAbove
,因为它所属的唯一线段代表一个Intersection
。您可能需要如果需要的话可以改变这种行为。)说实话,如果您需要做任何比这更复杂的事情(例如找到交点),我会回避任何“可爱”的解决方案。这里需要良好的面向对象设计。
One way would be to break each series up into line-segments, and then compare corresponding (by index) segments from the two series.
Since you specifically mention LINQ, here's one way to achieve that. It's not very pretty:
Output:
(Note that you don't get a
PlusAbove
for the last point because the only segment it is part of represents anIntersection
. You may need to change this behaviour if desirable.)To be honest, I would shy away from any 'cute' solution if you need to do anything even slightly more complicated than this (e.g. finding the intersection points). Good OO design is needed here.
为什么你不能这样做:
Why can't you just do:
然后,后来:
Then, later:
如果
minus
和plus
是列表:if
minus
andplus
are list: