I currently have an algorithm that generates a point grid. It takes input from the user in the form of a length of x (lx) and a length of y (ly), and what increment, or delta, (dx and dy) to space the points by. I need the points to always start and finish on the edges of the bounding square defined by lx and ly. I’ve tried a few methods:
The start edge of the bounding square is defined as:
double startx = lx / -2.0, starty = ly / -2.0;
My first method determines the number of points and rounds:
int numintervalx = round(lx / dx), numintervaly = round(ly / dy);
My second method determines the number of points and uses the closest integer greater than the number of points:
int numintervalx = ceil(lx / dx), numintervaly = ceil(ly / dy);
My third method determines the number of points and uses the closest integer less than the number of points:
int numintervalx = floor(lx / dx), numintervaly = floor(ly / dy);
The delta is then recalculated to fit the bounding box:
dx = lx / double(numintervalx);
dy = ly / double(numintervaly);
These are then fed into a for loop that generates the points themselves:
for (int i = 0; i <= numintervaly; i++)
for (int j = 0; j <= numintervalx; j++)
{
double point[3] = {startx + dx * j, starty + dy * i, 0};
}
Is there another, more accurate, method that would make the actual grid closer to the user specified grid that still always starts and finishes on the edges?
Think of the integer conversion as adding error. In that case, the way to minimize the error added when converting to an integer is rounding. The worst case is if the user inputs values such that lx/dx is something.5, which means a rounding error of 0.5. Given your problem, this is the best you can do.
Consider renaming numpoints to numintervals or something, as you actually create one more point than numpoints, which is strange.