Mathematica:不相交线段
我们如何告诉Mathematica给我们一组不相交的线?在这种情况下,如果两条线有一个公共点(不是端点),则它们相交。考虑这个简单的情况:
l1 = {{-1, 0}, {1, 0}};
l2 = {{0, -1}, {0, 1}};
lines = {l1, l2};
这个想法是创建一个函数,给定一组线,返回一组不相交的线。如果存在这样的函数,例如 split
,那么 的输出
split[lines]
将为
{
{{-1, 0}, {0,0}},
{{ 0, 0}, {1,0}},
{{ 0,-1}, {0,0}},
{{ 0, 0}, {0,1}}
}
该函数检测到 {0,0}
是集合中两条线之间的交集,并且为了有非相交线它打破了相交处的线段,从而生成了另外 2 条线。如果原始输入包含更多行,则此过程会变得更加复杂。有谁知道如何在不使用循环的情况下在 Mathematica 中有效地完成此操作?了解一种算法来查找是否 两条线相交。
注意
这个问题是我尝试找出如何制作电线的第二部分Mathematica 中的隐藏线删除框架。请随意添加更多合适的标签。
How can we tell Mathematica to gives us a set of non-intersecting lines? In this case two lines intersect if they have a point (not an endpoint) in common. Consider this simple case:
l1 = {{-1, 0}, {1, 0}};
l2 = {{0, -1}, {0, 1}};
lines = {l1, l2};
The idea is to create a function which, given a set a lines, returns a set of non-intersecting lines. If such function exists say split
then the output of
split[lines]
would be
{
{{-1, 0}, {0,0}},
{{ 0, 0}, {1,0}},
{{ 0,-1}, {0,0}},
{{ 0, 0}, {0,1}}
}
The function detected that {0,0}
is the intersection between the two lines in the set and in order to have non-intersecting lines it broke the line segments at the intersections thus generating 2 more lines. This process gets more complicated if the original input contains more lines. Does anyone know how to do this efficiently in Mathematica without using loops? It might help to know an algorithm to find if two lines are intersecting.
Note
This question is the second part of my attempt to find out how to make wire frames in Mathematica with hidden line removal. Please feel free to add more appropriate tags.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您假设存在拆分,则需要将其应用于所有对; 生成
这些可以通过以下方式 :
permsnodups[{a, b, c, d}]
给出{{a, b}, {a, c}, {a, d} , {b, c}, {b, d}, {c, d}}
,您可以在其上映射您的split
函数(即这些都是对,确保如果<代码>{a,b}那么{b,a}
就没有了,从那时起你就无缘无故地做了两倍的工作——这就像做 $\sum_{i,j>i}$ 而不是 $\sum_ {i,j}$)。编辑:这是
split
的实现(我在半个小时左右的时间内无法访问互联网,因此手动计算出相关方程,这不是基于您提供的链接,也不是基于您提供的链接)它是优化的还是漂亮的):(事实上,它非常慢且丑陋)。不管怎样,你可以看到它的工作原理是这样的:
它会产生类似
和
(您可以移动定位器)。请注意,只要其中一条线是垂直的,我的
split2
就会除以零(这可以修复,但我还没有这样做)。无论如何,这一切都非常缓慢且丑陋。通过编译和使其可列出(并使用您提供的链接)可以使其更快,但我当前的茶歇已经结束(或者是半个多小时前)。我稍后会尝试回到这个话题。
同时,一定要询问是否有任何具体问题(例如,如果您看不到垂直线的中断点)。请注意,虽然这符合您的要求,但如果您确实映射一系列行,您最终会得到一个参差不齐的列表,您必须将其展平。但是,这就是您所要求的:)
if you assume that split exists, you then need to apply it to all pairs; these may be produced by
which does this:
permsnodups[{a, b, c, d}]
gives{{a, b}, {a, c}, {a, d}, {b, c}, {b, d}, {c, d}}
, over which you could map yoursplit
function (ie these are all pairs, making sure that if{a,b}
is there then{b,a}
is not since then you are doing twice the work for no reason--it's like doing $\sum_{i,j>i}$ as opposed to $\sum_{i,j}$).EDIT: Here is an implementation of
split
(I was stuck with no internet access for half an hour or so, so worked out the relevant equations by hand, and this is not based on the link you gave nor is it optimized or pretty):(in fact it's atrociously slow and ugly). Anyway, you can see that it works like this:
which produces things like
and
(you can move the locators around). Mind you, my
split2
divides by zero whenever one of the lines is vertical (this can be fixed but I haven't done it).In any case this is all very slow and ugly. It could be made faster by compiling and making listable (and using the link you gave), but my current coffee break is over (or was over half an hour ago). I'll try to get back to this later.
Meanwhile, do ask if there are any concrete questions (eg, if you can't see what breaks for vertical lines). And note that while this does what you ask, if you do map over a list of lines you'll end up with a ragged list which you will have to flatten. But, this is what you asked for :)
为了确定交集,您还可以采用以下参数方法,该方法不会遇到涉及笛卡尔方程的方法的常见问题(即除以零...):
For determining the intersection, you can also do the following parametric approach, that does not suffer from the usual problems of methods involving the cartesian equations (ie division by zero ...):