Skip to content

Instantly share code, notes, and snippets.

@mortenpi mortenpi/range.cc
Created Aug 28, 2015

Embed
What would you like to do?
Function to create a vector of evenly spaced numbers in C++.
// Create a vector of evenly spaced numbers.
vector<double> range(double min, double max, size_t N) {
vector<double> range;
double delta = (max-min)/double(N-1);
for(int i=0; i<N; i++) {
range.push_back(min + i*delta);
}
return range;
}
@adam-erickson

This comment has been minimized.

Copy link

adam-erickson commented Feb 26, 2018

You might want to check this out. I've found it to be at least twice as fast for large vectors (1M points) and 100x faster for small vectors (100 points).

    // Linear interpolation following MATLAB linspace
    std::vector<double> LinearSpacedArray(double a, double b, std::size_t N)
    {
        double h = (b - a) / static_cast<double>(N-1);
        std::vector<double> xs(N);
        std::vector<double>::iterator x;
        double val;
        for (x = xs.begin(), val = a; x != xs.end(); ++x, val += h) {
            *x = val;
        }
        return xs;
    }
@script526

This comment has been minimized.

Copy link

script526 commented Nov 10, 2019

Thanks for the idea! I was looking for something like this. Example code for Unreal Engine 4:

// Distribute vectors evengly in a straight line across X axis
TArray<FVector> AShip::rangeOfVectors(FVector start, FVector end, int32 sizeOf)
{
	TArray<FVector> rangeOfVectors;
	float step = FVector::Dist(start, end) / sizeOf - 1;
	for (int32 i = 0; i < sizeOf; i++)
	{
		rangeOfVectors.Add(FVector(start.X + (i * step), start.Y, start.Z));
	}
	return rangeOfVectors;
}
@canberkgurel

This comment has been minimized.

Copy link

canberkgurel commented Dec 31, 2019

You might want to check this out. I've found it to be at least twice as fast for large vectors (1M points) and 100x faster for small vectors (100 points).

    // Linear interpolation following MATLAB linspace
    std::vector<double> LinearSpacedArray(double a, double b, std::size_t N)
    {
        double h = (b - a) / static_cast<double>(N-1);
        std::vector<double> xs(N);
        std::vector<double>::iterator x;
        double val;
        for (x = xs.begin(), val = a; x != xs.end(); ++x, val += h) {
            *x = val;
        }
        return xs;
    }

Thanks @adam-erickson ! Especially for large vectors, it's a good practice to pass the vector by reference.

void LinearSpacedArray(std::vector<double> &xs, double a, double b, std::size_t N)
{
    double h = (b - a) / static_cast<double>(N-1);
    std::vector<double>::iterator x;
    double val;
    for (x = xs.begin(), val = a; x != xs.end(); ++x, val += h) {
        *x = val;
    }
} 
@adam-erickson

This comment has been minimized.

Copy link

adam-erickson commented Jan 2, 2020

I agree @canberkgurel. The version provided assumes only the original three parameters are available, akin to linspace.

@giraldeau

This comment has been minimized.

Copy link

giraldeau commented Jan 8, 2020

The performance difference is probably mainly due to initializing the vector to the correct size once and avoids the push_back() reallocation and copies when growing. Notice that the successive addition val += h accumulates error. After just a few iterations, the result will drift from min + i * delta from the original code.

@script526

This comment has been minimized.

Copy link

script526 commented May 6, 2020

Another version for 3D space (Unreal Engine API).

TArray<FVector> USplashComponent::distributeVectorsAcrossLine(FVector start, FVector end, int32 dens) const
{
	TArray<FVector> vectors;

	// safety check
	if (dens <= 0)
	{
		return vectors;
	}
	else
	{
		dens = FMath::Clamp(dens, 0, 500);
		FVector nextVector;

		for (int32 i = 0; i < dens + 1; i++)
		{
			nextVector.X = FMath::Lerp(start.X, end.X, (float)i / (float)dens);
			nextVector.Y = FMath::Lerp(start.Y, end.Y, (float)i / (float)dens);
			nextVector.Z = FMath::Lerp(start.Z, end.Z, (float)i / (float)dens);
			vectors.Add(nextVector);
		}
		return vectors;
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.