C++ Newbie - Please judge my C++ code.

That does not work properly with negatives (for what I need anyways), and it would be simpler to do the data type conversion than use Ceil or Floor based on it’s sign.

Thanks for your suggestions everyone, I ended up simplifying it quite a bit, and even learned a few things in the process (Ternary Conditional Operators), here is the new code:


void UCustomBlueprintNodes::SnapVectorToGrid(FVector Vector, float GridSize, FVector& SnappedVector)
{
	float SnappedX;
	float SnappedY;
	float SnappedZ;
	FVector AbsVector = FVector(FMath::Abs(Vector.X), FMath::Abs(Vector.Y), FMath::Abs(Vector.Z));
	SnappedX = (float)((int)(Vector.X / GridSize))*GridSize;
	SnappedY = (float)((int)(Vector.Y / GridSize))*GridSize;
	SnappedZ = (float)((int)(Vector.Z / GridSize))*GridSize;

	if (FMath::Fmod(AbsVector.X, GridSize) >= GridSize / 2)
	{
		SnappedX += GridSize*((Vector.X < 0) ? (-1) : (1));
	}

	if (FMath::Fmod(AbsVector.Y, GridSize) >= GridSize / 2)
	{
		SnappedY += GridSize*((Vector.Y < 0) ? (-1) : (1));
	}

	if (FMath::Fmod(AbsVector.Z, GridSize) >= GridSize / 2)
	{
		SnappedZ += GridSize*((Vector.Z < 0) ? (-1) : (1));
	}

	SnappedVector = FVector(SnappedX, SnappedY, SnappedZ);
}