优雅的“左手”测试Polyline [英] Elegant "Left-of" test for Polyline

查看:163
本文介绍了优雅的“左手”测试Polyline的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定:


  • (X,Y)坐标,它是车辆的位置。 / li>
  • (X,Y)的数组,它们是多段线中的顶点。请注意,折线仅包含直线段,不包含弧线。



我想要的:




  • 计算车辆是在折线的左边还是右边(或者位于酒店顶层)。


我的方法: b
$ b

一直没有活动,我相信它已经死了。不过,我有一个解决方案。


但是,左边的测试会产生 em>如果第一个细分受众群被选为最接近的细分受众群,那么离开,否则。 >你已经使用了一些含糊不清的语言。我将使用来说明折线和象限中的线段,以说明由它们分隔的区域。因此,对于您的情况,您会看到一个红色象限,它似乎位于一个的右侧并位于另一个的左侧。



如果左边的测试对不同的分段产生不同的答案,则应该对分段本身重做测试。在你的情况下,你会有:


  • 象限在第一段的右边

  • 象限位于第二段的左侧



两个线段都不同意象限所在的位置,因此您需要进一步消除歧义测试:


  • 第二个分段位于第一个分段的右侧

  • 第一个分段是在第二段的右侧



这可以让我们得出结论:第二段 之间第一部分和象限 - 因为这两部分中的每一部分位于第二部分的不同侧。因此,第二部分比第一部分接近象限,它的答案是左右测试应该用作正确的测试。

(I'我几乎可以肯定,你可以只使用两个消歧测试中的一个,为了清楚起见,我已经把它们都放进去了)。为了完整起见,我相信这个解决方案也是可行的考虑到您对效率和优雅的要求,因为它使用了您从一开始就使用的相同方法(左侧测试),因此它符合所有指定的条件:优雅,高效并且照顾问题。


Given:

  • (X,Y) coordinate, which is the position of a vehicle.
  • Array of (X,Y)'s, which are vertices in a polyline. Note that the polyline consists of straight segments only, no arcs.

What I want:

  • To calculate whether the vehicle is to the left or to the right of the polyline (or on top, ofcourse).

My approach:

Possible issues:

  • When three points form an angle smaller than 90 degrees (such as shown in the image blow), a more complicated scenario arises. When the vehicle is in the red segment as shown below, the closest segment can be either one of the two. However, the left-of test will yield right if the first segment is chosen as the closest segment, and left otherwise. We can easily see (at least, I hope), that the correct result should be that the vehicle is left of the polyline.

My question:

  • How can I elegantly, but mostly efficiently take care of this specific situation?

My fix so far:

  • Compute for both segments a point on that segment, starting from the vertex point.
  • Compute the distance from the vehicle to both of the points, using Euclidian distance
  • Keep the segment for which the computed point is the closest.

I am not very happy with this fix, because I feel like I am missing a far more elegant solution, my fix feels rather "hacky". Efficiency is key though, because it is used on a realtime embedded system.

Existing codebase is in C++, so if you want to write in a specific language, C++ has my preference. Thanks!

[edit] I changed my fix, from a perpendicular point to a parallel point, as I think it is easier to follow the line segment than compute the outward normal.

解决方案

This topic has been inactive for so long that I believe it's dead. I have a solution though.

However, the left-of test will yield right if the first segment is chosen as the closest segment, and left otherwise.

You've used slightly ambiguous language. I'm gonna use segments to speak of the line segments in the polyline and quadrants to speak of the areas delimited by them. So in your case, you'd have a red quadrant which seems to be on the right of one segment and on the left of the other.

If the left-of test yields different answers for different segments, you should redo the test on the segments themselves. In your case, you'd have:

  • The quadrant is on the RIGHT of the first segment
  • The quadrant is on the LEFT of the second segment

Both segments disagree on where the quadrant lies, so you do two further disambiguation tests:

  • The second segment is on the RIGHT of the first segment
  • The first segment is on the RIGHT of the second segment

This allows us to conclude that the second segment is in between the first segment and the quadrant—since each of those two lies on a different side of the second segment. Therefore, the second segment is "closer" to the quadrant than the first and it's answer to the left-right test should be used as the correct one.

(I'm almost sure you can do with only one of the two disambiguation tests, I've put both in for clarity)

For the sake of completeness: I believe this solution also accounts for your demands of efficiency and elegance, since it uses the same method you've been using from the start (the left-of test), so it meets all the conditions specified: it's elegant, it's efficient and it takes care of the problem.

这篇关于优雅的“左手”测试Polyline的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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