English 中文(简体)
生成平均间距点网格
原标题:Generate an evenly spaced point grid

目前我有一个生成点网格的算法。 它需要用户以x长( lx )和y长( ly )和什么递增( code> 和 dy ) 或三角洲( < code> 和 < code> /code > )的形式输入点。 我需要在 < strong > aways start and finish > by lx 定义的捆绑方形边缘 输入。 我尝试了几种方法 :

边框方形的起始边缘定义为:

double startx = lx / -2.0, starty = ly / -2.0;

我的第一种方法决定点数和轮数:

int numintervalx = round(lx / dx), numintervaly = round(ly / dy);

我的第二种方法确定点数,使用最接近的整数大于点数:

int numintervalx = ceil(lx / dx), numintervaly = ceil(ly / dy);

我的第三个方法确定点数,使用最接近的整数,小于点数:

int numintervalx = floor(lx / dx), numintervaly = floor(ly / dy);

然后对三角洲进行重新计算,以适应捆绑盒:

dx = lx / double(numintervalx);
dy = ly / double(numintervaly);

然后将其输入 < / code> 环的 < code>, 以生成点本身 :

for (int i = 0; i <= numintervaly; i++)
  for (int j = 0; j <= numintervalx; j++)
  {
    double point[3] = {startx + dx * j, starty + dy * i, 0};
  }

是否有另一种更准确的方法能让实际网格更接近用户指定的网格,

最佳回答

将整数转换想象为添加错误。 在这种情况下, 最小化转换整数时添加的错误的方法是四舍五入。 最坏的情况是, 如果用户输入值为 lx/ dx 是某种值, 也就是说四舍五入误差为0. 5。 鉴于您的问题, 这是您所能做的最好的事情 。

考虑将numpoints 重命名为 numintervals 或其它东西, 因为您实际上创建了一个比 numpoints 更多的点, 这很奇怪 。

问题回答

用户需要将 < code> lx < /code > 和 < code> 和 < code > 的数值分别作为 < code > dx < /code > 和 < code > 的倍数。 当然,这需要一些基本的输入验证, 但它会保证实际网格与用户指定的网格完全相同, 边端总是有点开始和结束 。

您的输入显然不能保证与 lx 兼容, 因为它是 dx 的整数倍, 因此您有问题。 因此, 您的 < em> 必须 < / em > 需要兼容的输入, 最好是将 < code> nx 作为输入, 以及 < code> dx 或 < code> > > lx , 不论在您的应用程序中是否更有意义 。

或者,您可以将用户输入仅作为指南对待,即:

nx = int(ceil(lx/dx));    // get suitable number of points
dx = lx/nx;               // set suitable spacing to fit range exactly




相关问题
How to add/merge several Big O s into one

If I have an algorithm which is comprised of (let s say) three sub-algorithms, all with different O() characteristics, e.g.: algorithm A: O(n) algorithm B: O(log(n)) algorithm C: O(n log(n)) How do ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Enumerating All Minimal Directed Cycles Of A Directed Graph

I have a directed graph and my problem is to enumerate all the minimal (cycles that cannot be constructed as the union of other cycles) directed cycles of this graph. This is different from what the ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签