Instantly share code, notes, and snippets.

# mortenpi/range.cc Created Aug 28, 2015

Function to create a vector of evenly spaced numbers in C++.
 // Create a vector of evenly spaced numbers. vector range(double min, double max, size_t N) { vector range; double delta = (max-min)/double(N-1); for(int i=0; i

### adam-erickson commented Feb 26, 2018 • edited

 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 LinearSpacedArray(double a, double b, std::size_t N) { double h = (b - a) / static_cast(N-1); std::vector xs(N); std::vector::iterator x; double val; for (x = xs.begin(), val = a; x != xs.end(); ++x, val += h) { *x = val; } return xs; } ``````

### 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 AShip::rangeOfVectors(FVector start, FVector end, int32 sizeOf) { TArray 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 commented Dec 31, 2019 • edited

 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 LinearSpacedArray(double a, double b, std::size_t N) { double h = (b - a) / static_cast(N-1); std::vector xs(N); std::vector::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 &xs, double a, double b, std::size_t N) { double h = (b - a) / static_cast(N-1); std::vector::iterator x; double val; for (x = xs.begin(), val = a; x != xs.end(); ++x, val += h) { *x = val; } } ``````

### adam-erickson commented Jan 2, 2020

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

### giraldeau commented Jan 8, 2020 • edited

 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 commented May 6, 2020 • edited

 Another version for 3D space (Unreal Engine API). ``````TArray USplashComponent::distributeVectorsAcrossLine(FVector start, FVector end, int32 dens) const { TArray 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; } } ``````