-
-
Save mortenpi/f20a93c8ed3ee7785e65 to your computer and use it in GitHub Desktop.
| // 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; | |
| } |
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;
}
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;
}
}
I agree @canberkgurel. The version provided assumes only the original three parameters are available, akin to linspace.
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.
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;
}
}
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).