简体   繁体   English

获得最靠近网格点的点

[英]Getting closest point on grid to point

I have a one dimensional gird. 我有一个一维的网格。 its spacing is a floating point. 它的间距是一个浮点。 I have a point with floating point coordinate as well. 我有一个浮点坐标点。 I need to find its distance to the closest grid point. 我需要找到距离最近的网格点的距离。
For example: 例如:

            0.12
             |
             *
 |---------|---------|---------|---------|---------|
 0        0.1       0.2       0.3       0.4       0.5

The result would be -0.02 since the closest point is behind it. 结果将是-0.02因为最接近的点在它后面。
However if it was 但如果是的话

                -0.66
                  |
                  *
 |---------|---------|---------|---------|---------|
-1       -0.8      -0.6      -0.4      -0.2        0

The result will be 0.06 . 结果将是0.06 As you can see its in floating point and can be negative. 正如你可以看到它的浮点,可能是负面的。
I tried the following: 我尝试了以下方法:

float spacing = ...;
float point = ...;

while(point >= spacing) point -= spacing;
while(point < 0) point += spacing;

if(std::abs(point - spacing) < point) point -= spacing;

It works, but I'm sure there is a way without loops 它有效,但我确信有一种没有循环的方法

Let us first compute the nearest points on the left and right as follows: 我们首先计算左右最近的点,如下所示:

leftBorder = spacing * floor(point/spacing);
rightBorder = leftBorder + spacing;

Then the distance is straightforward: 然后距离很简单:

if ((point - leftBorder) < (rightBorder - point))
    distance = leftBorder - point;
else
    distance = rightBorder - point;

Note that, we could find the nearest points alternatively by ceiling: 请注意,我们可以通过天花板找到最近的点:

rightBorder = spacing * ceil(point/spacing);
leftBorder = rightBorder - spacing;
std::vector<float> spacing = ...;
float point = ...;
float result;

Since you say the spacing isn't (linear), I would cache the sums: 既然你说间距不是(线性),我会缓存总和:

std::vector<float> sums(1, 0.0);
float sum=0;
for(int i=0; i<spacing.size(); ++i)
    sums.push_back(sum+=spacing[i]);
//This only needs doing once.
//sums needs to be in increasing order.  

Then do a binary search to find the point to the left: 然后进行二分查找以找到左边的点:

std::vector<float>::iterator iter;
iter = std::lower_bound(sums.begin(), sums.end(), point);

Then find the result from there: 然后从那里找到结果:

if (iter+1 == sums.end())
    return point-*iter;
else {
    float midpoint = (*iter + *(iter+1))/2;
    if (point < midpoint)
        result = point - *iter;
    else
        result = *(iter+1) - point;
}

[EDIT] Don't I feel silly. [编辑]我不觉得傻。 You said the spacing wasn't constant. 你说间距不是恒定的。 I interpreted that as not-linear. 我认为这不是线性的。 But then your sample code is linear, just not a compile-time constant. 但是,您的示例代码线性的,而不是编译时常量。 My bad. 我的错。 I'll leave this answer as a more general solution, though your (linear) question is solvable much faster. 我将这个答案留作更通用的解决方案,尽管你的(线性)问题可以更快地解决。

Here is my first blush attempt, note that this is not tested at all. 这是我的第一次脸红尝试,请注意,这根本没有测试过。

float remainder = fmod(point, spacing); // This is the fractional difference of the spaces
int num_spaces = point/spacing;  // This is the number of "spaces" down you are, rounded down


// If our fractional part is greater than half of the space length, increase the number of spaces.
// Not sure what you want to do when the point is equidistant to both grid points
if(remainder > .5 * spacing) 
{
  ++num_spaces;
}

float closest_value = num_spaces*spacing;
float distance = closest_value - point;

You should just round the number using this: 您应该使用以下方法对数字进行舍入:

float spacing = ...;
float point = ...;
(point > 0.0) ? floor(point + spacing/2) : ceil(point - spacing/2);

更常见的是,对于任意间距,尺寸和距离度量(度量),您正在寻找的结构将是Voronoi图。

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

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