简体   繁体   English

从最近一条线的一组点中找到点的最快算法是什么?

[英]What is the fastest algorithm to find the point from a set of points, which is closest to a line?

I have: 我有:
- a set of points of known size (in my case, only 6 points) - 一组已知大小的点(在我的情况下,只有6分)
- a line characterized by x = s + t * r, where x, s and r are 3D vectors - 由x = s + t * r表征的线,其中x,s和r是3D矢量

I need to find the point closest to the given line. 我需要找到最接近给定线的点。 The actual distance does not matter to me. 实际距离对我来说无关紧要。

I had a look at several different questions that seem related (including this one) and know how to solve this on paper from my highschool math classes. 我看了几个看似相关的不同问题(包括这个 ),并且知道如何从我的高中数学课上解决这个问题。 But I cannot find a solution without calculating every distance, and I am sure there has to be a better/faster way. 但是我没有计算出每一个距离都无法找到解决方案,我相信必须有更好/更快的方法。 Performance is absolutely crucial in my application. 在我的应用中,性能绝对至关重要。

One more thing: All numbers are integers (coordinates of points and elements of s and r vectors). 还有一件事:所有数字都是整数(点的坐标和s和r向量的元素)。 Again, for performance reasons I would like to keep the floating-point math to a minimum. 同样,出于性能原因,我希望将浮点数学保持在最低限度。

You have to process every point at least once to know their distance. 你必须至少处理一次每个点才能知道它们的距离。 Unless you want to repeat the process many times with different lines, simply computing the distance of every point is unavoidable. 除非你想用不同的线重复多次这个过程,否则简单地计算每个点的距离是不可避免的。 So the algorithm has to be O(n). 所以算法必须是O(n)。

Since you don't care about the actual distance, we can make some simplification to the point-distance computation. 由于您不关心实际距离,我们可以对点距离计算进行一些简化。 The exact distance is computed by ( source ): 确切的距离由( 来源 )计算:

d^2 = |r⨯(p-s)|^2 / |r|^2

where is the cross product and |r|^2 is the squared length of vector r . 其中是叉积, |r|^2是向量r的平方长度。 Since |r|^2 is constant for all points, we can omit it from the distance computation without changing result: 由于|r|^2对于所有点都是常数,我们可以在不改变结果的情况下从距离计算中省略它:

d^2 = |r⨯(p-s)|^2

Compare the approximated square distances and keep the minimum. 比较近似的平方距离并保持最小值。 The advantage of this formula is that you can do everything with integers since you mentioned that all coordinates are integers. 这个公式的优点是你可以用整数做所有事情,因为你提到所有坐标都是整数。

I'm afraid you can't get away with computing less than 6 distances (if you could, at least one point would be left out -- including the nearest one). 我恐怕你无法逃脱计算少于6个距离(如果可以的话,至少有一个点会被遗漏 - 包括最近的一个)。

See if it makes sense to preprocess: Is the line fixed and the points vary? 看看预处理是否有意义:线是否固定且点数是否变化? Consider rotating coordinates to make the line horizontal. 考虑旋转坐标以使线条水平。

As there are few points, it is doubtful that this is your bottleneck. 由于积分很少,因此这是您的瓶颈是值得怀疑的。 Measure where the hot spots are, redesign algorithms/data representation, spice up compiler optimization, compile to assembly and bum that. 测量热点的位置,重新设计算法/数据表示,增加编译器优化,编译到汇编和烧伤。 Strictly in that order. 严格按此顺序。

Jon Bentley's "Writing Efficient Programs" (sadly long out of print) and "Programming Pearls" (2nd edition) are full of advise on practical programming. Jon Bentley的“写作高效节目”(遗憾的是已经绝版)和“编程珍珠”(第2版)充满了对实用节目的建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM