Grid/Vector2Extensions.cs

Extension methods converting Sandbox.Vector2 and Sandbox.Vector3 to an IntVector2 by dividing components by a divisor and rounding to nearest integer.

using System;
using Sandbox;

namespace GridAStar;

public static partial class Vector2Extensions
{
	/// <summary>
	/// Round every value of the Vector2 into an integer after dividing it by divideBy. Ex. Vector2(30.5f, 20.1f).ToIntVector2(24) = IntVector2(1,1)
	/// </summary>
	public static IntVector2 ToIntVector2( this Vector2 self, float divideBy = 1 )
	{
		return new IntVector2( (int)Math.Round( self.x / divideBy ), (int)Math.Round( self.y / divideBy ) );
	}

	/// <summary>
	/// Round every value of the Vector3 into an integer after dividing it by divideBy. Ex. Vector3(30.5f, 20.1f, 50.9f).ToIntVector2(24) = IntVector2(1,1)
	/// </summary>
	public static IntVector2 ToIntVector2( this Vector3 self, float divideBy = 1f )
	{
		return new IntVector2( (int)Math.Round( self.x / divideBy ), (int)Math.Round( self.y / divideBy ) );
	}
}