如何找到两条相反的法线或两条线段?
我有两个段 AB 和 CD(红色)。这两个部分彼此面对。它们并不完全平行,但也永远不会相互垂直。
由此,我需要找到这些线段(蓝色)的两个彼此相反的法线(即这两个法线位于 ABCD 之外)。我知道如何计算段的法线,但显然每个段都有两个法线,并且我无法弄清楚如何以编程方式选择我需要的法线。有什么建议吗?
I have a two segments AB and CD (in red). These two segments are facing each others. They are not completely parallel but will never be perpendicular to each others either.
From that, I need to find the two normals of these segments (in blue) that oppose each others (i.e. the two normals are outside ABCD). I know how to calculate the normals of the segments but obviously each segment has two normals, and I cannot figure out how to programmatically select the ones I need. Any suggestion?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
计算两条线段中点之间的向量 v,从 AB 指向 CD。现在,AB 所需法线在 v 上的投影必须为负,CD 所需法线在 v 上的投影必须为正。因此,只需计算法线,检查 v,并在需要时对法线取反以使它们满足条件。
这是 Python 中的:
请注意,我假设端点定义了满足问题条件的线。当线条具有相同的中点时,我也不检查边缘情况; “外部”的概念在这种情况下不适用。
Calculate the vector v between the midpoints of the two segments, pointing from AB to CD. Now the projection of the desired normal to AB onto v must be negative and the projection of the desired normal to CD onto v must be positive. So just calculate the normals, check against v, and negate the normals if needed to make them satisfy the condition.
Here it is in Python:
Note that I assume the endpoints define lines meeting the conditions in the problem. Nor do I check for the edge case when the lines have the same midpoint; the notion of "outside" doesn't apply in that case.
我认为有两种情况需要考虑:
情况 1:线之间的交点发生在任一线段端点之外。
在这种情况下,@Michael J. Barber 建议的中点方法肯定会起作用。因此,在线段的中点之间形成一个向量,计算法线向量与该中点向量的点积并检查符号。
如果您计算
lineA
的法线,则法线与向量midB -> 的点积为midA
应该是+ve
。情况 2:线之间的交点发生在一条线段的端点内部。
在这种情况下,在不包围交点的线段的任一端点和交点本身之间形成一个向量。
确实包围交点的线段的法线的点积和这个新向量应该是
+ve
。您可以通过要求两个法线之间的点积为
-ve
来找到另一个线段的向外法线(仅在垂直线段的情况下才会不明确)。我假设这些线段不共线或实际上不相交。
希望这有帮助。
I think there are two cases to consider:
Case 1: Intersection between lines occurs outside the endpoints of either segment.
In this case the midpoint method suggested by @Michael J. Barber will work for sure. So form a vector between the midpoints of the segments, compute the dot product of your normal vectors with this midpoint vector and check the sign.
If you're computing the normal for
lineA
, the dot product of the normal with the vectormidB -> midA
should be+ve
.Case 2: Intersection between lines occurs inside the endpoints of one segment.
In this case form a vector between either one of the endpoints of the segment that does not enclose the intersection point and the intersection point itself.
The dot product of the normal for the segment that does enclose the intersection point and this new vector should be
+ve
.You can find the outward normal for the other segment by requiring that the dot product between the two normals is
-ve
(which would only be ambiguous in the case of perpendicular segments).I've assumed that the segments are not co-linear or actually intersecting.
Hope this helps.
您可以按如下方式减少符号的四种组合:
计算法线的点积,负号表示同时显示在外部或内部。
由于我假设您的法线具有单位长度,因此如果点积的大小为 1,您可以检测到并行性。正值表示两者显示方向相同,负值表示两者显示方向不同。
如果法线不平行:将法线
n
的线参数化为x(t) = x0 + t * n
并计算t
code> 两者相交。负数t
表示两者都显示在外面。如果您对其中一个法线执行此操作就足够了,因为您在步骤 1 中将组合从 4 个减少到了 2 个。如果两个法线平行:计算法线的时间
t
击中各段之间的中点。如 2 所示,如果您对其中一个法线执行此操作就足够了,因为您在步骤 1 中将组合从 4 个减少到 2 个。You can reduce the four combinations for the signs as follows:
Calculate the dot product of the normals, a negative sign indicates that both show outside or inside.
As I suppose that your normals have unit lenght, you can detect parallelity if the dot product has magnitude one. A positive value indicates that both show in the same direction, a negative value says that both show in different directions.
It the normals are not parallel: parametrize lines as
x(t) = x0 + t * n
for a normaln
and calculate thet
for which both intersect. A negativet
will indicate that both show outside. It is enough if you do this for one of the normals, as you reduced your combinations from 4 to 2 in step 1.If both normals are parralel: Calculate the time
t
for which the normals hit the midpoint between of your segments. As in 2. is enough if you do this for one of the normals, as you reduced your combinations from 4 to 2 in step 1.