A more effective way to find the number of points with a given distance between them on a line?
-
Given three doubles minx, maxx and delta I want to work out how many times delta fits in the interval between minx and maxx without touching the maxx. I've found this solution to the problem in some inherited code. This is fine when m is small, but as m grows it obviously takes longer and longer. I guess it was easy to code, but not particularly efficient.
public int FindMaxM(double minx, double maxx, double delta)
{
int m;
while (minx + m * delta < maxx)
{
m++;
}
return m;
}My own implementation looks like this, but I wondered in an academic way if there was an even better way to do it. How would you implement it?
public int FindMaxM(double minx, double maxx, double delta)
{
int m = (int) (((maxx - minx)/delta));
//if the division is resulting in an actual integer reduce the gridsize
if (minx + m * delta >= maxx)
{
m--;
}
return m;
} -
Given three doubles minx, maxx and delta I want to work out how many times delta fits in the interval between minx and maxx without touching the maxx. I've found this solution to the problem in some inherited code. This is fine when m is small, but as m grows it obviously takes longer and longer. I guess it was easy to code, but not particularly efficient.
public int FindMaxM(double minx, double maxx, double delta)
{
int m;
while (minx + m * delta < maxx)
{
m++;
}
return m;
}My own implementation looks like this, but I wondered in an academic way if there was an even better way to do it. How would you implement it?
public int FindMaxM(double minx, double maxx, double delta)
{
int m = (int) (((maxx - minx)/delta));
//if the division is resulting in an actual integer reduce the gridsize
if (minx + m * delta >= maxx)
{
m--;
}
return m;
}Just off the cuff I would say:
m = (int)((maxx-minx) / delta);
The difficult we do right away... ...the impossible takes slightly longer.
-
Given three doubles minx, maxx and delta I want to work out how many times delta fits in the interval between minx and maxx without touching the maxx. I've found this solution to the problem in some inherited code. This is fine when m is small, but as m grows it obviously takes longer and longer. I guess it was easy to code, but not particularly efficient.
public int FindMaxM(double minx, double maxx, double delta)
{
int m;
while (minx + m * delta < maxx)
{
m++;
}
return m;
}My own implementation looks like this, but I wondered in an academic way if there was an even better way to do it. How would you implement it?
public int FindMaxM(double minx, double maxx, double delta)
{
int m = (int) (((maxx - minx)/delta));
//if the division is resulting in an actual integer reduce the gridsize
if (minx + m * delta >= maxx)
{
m--;
}
return m;
} -
Given three doubles minx, maxx and delta I want to work out how many times delta fits in the interval between minx and maxx without touching the maxx. I've found this solution to the problem in some inherited code. This is fine when m is small, but as m grows it obviously takes longer and longer. I guess it was easy to code, but not particularly efficient.
public int FindMaxM(double minx, double maxx, double delta)
{
int m;
while (minx + m * delta < maxx)
{
m++;
}
return m;
}My own implementation looks like this, but I wondered in an academic way if there was an even better way to do it. How would you implement it?
public int FindMaxM(double minx, double maxx, double delta)
{
int m = (int) (((maxx - minx)/delta));
//if the division is resulting in an actual integer reduce the gridsize
if (minx + m * delta >= maxx)
{
m--;
}
return m;
}The answers so far probably don't satisfy the "without touching the maxx" requirement; it may depend on your definition of "touching". You might get away with it by subtracting a tiny fraction of delta from maxx before using the division approach... :)
Luc Pattyn [My Articles] Nil Volentibus Arduum