Skip to content

Instantly share code, notes, and snippets.

@jhm-ciberman
Last active December 28, 2021 22:32
Show Gist options
  • Save jhm-ciberman/2edb29dcadbc953af9e0eacaa0d25296 to your computer and use it in GitHub Desktop.
Save jhm-ciberman/2edb29dcadbc953af9e0eacaa0d25296 to your computer and use it in GitHub Desktop.
Vector2Int.cs
using System;
using System.Numerics;
using System.Runtime.CompilerServices;
namespace LifeSim
{
public struct Vector2Int : IEquatable<Vector2Int>
{
public int X;
public int Y;
public static Vector2Int One => new Vector2Int(1, 1);
public static Vector2Int Zero => new Vector2Int(0, 0);
public Vector2Int(int x, int y)
{
this.X = x;
this.Y = y;
}
public Vector2Int(uint x, uint y)
{
this.X = (int)x;
this.Y = (int)y;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2Int operator +(Vector2Int a, Vector2Int b)
{
return new Vector2Int(a.X + b.X, a.Y + b.Y);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2Int operator -(Vector2Int a, Vector2Int b)
{
return new Vector2Int(a.X - b.X, a.Y - b.Y);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2Int operator *(Vector2Int a, Vector2Int b)
{
return new Vector2Int(a.X * b.X, a.Y * b.Y);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2Int operator /(Vector2Int a, Vector2Int b)
{
return new Vector2Int(a.X / b.X, a.Y / b.Y);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2Int operator *(Vector2Int a, int b)
{
return new Vector2Int(a.X * b, a.Y * b);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2Int operator /(Vector2Int a, int b)
{
return new Vector2Int(a.X / b, a.Y / b);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2Int operator *(Vector2Int a, uint b)
{
return new Vector2Int(a.X * (int)b, a.Y * (int)b);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2Int operator /(Vector2Int a, uint b)
{
return new Vector2Int(a.X / (int)b, a.Y / (int)b);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(Vector2Int lhs, Vector2Int rhs)
{
return lhs.X == rhs.X && lhs.Y == rhs.Y;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator !=(Vector2Int lhs, Vector2Int rhs)
{
return !(lhs == rhs);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator Vector2(Vector2Int a)
{
return new Vector2(a.X, a.Y);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override bool Equals(object? other)
{
if (!(other is Vector2Int)) return false;
return this.Equals((Vector2Int)other);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals(Vector2Int other)
{
return this.X == other.X && this.Y == other.Y;
}
public override int GetHashCode()
{
return this.X.GetHashCode() ^ (this.Y.GetHashCode() << 16);
}
public override string? ToString()
{
return "(" + this.X + ", " + this.Y + ")";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment