CultLib/packages/cultmath/src/CultMath/float2.cs

84 lines
4.9 KiB
C#
Raw Normal View History

using System;
2026-05-08 01:49:44 +01:00
namespace CultMath;
public partial struct float2 : IEquatable<float2>
2026-05-08 01:49:44 +01:00
{
public static readonly float2 zero = new(0.0f, 0.0f);
public static readonly float2 one = new(1.0f, 1.0f);
2026-09-14 13:29:59 +02:00
public float x;
public float y;
public float2(float x, float y)
{
this.x = x;
this.y = y;
}
public float this[int index]
2026-05-08 01:49:44 +01:00
{
2026-09-14 13:29:59 +02:00
readonly get => index switch
{
0 => x,
1 => y,
_ => throw new ArgumentOutOfRangeException(nameof(index)),
};
set
{
switch (index)
{
case 0: x = value; break;
case 1: y = value; break;
default: throw new ArgumentOutOfRangeException(nameof(index));
}
}
}
2026-05-08 01:49:44 +01:00
public static implicit operator float2(float value) => new(value, value);
public static explicit operator System.Numerics.Vector2(float2 value) => new(value.x, value.y);
public static explicit operator float2(System.Numerics.Vector2 value) => new(value.X, value.Y);
public static float2 operator +(float2 value) => value;
public static float2 operator -(float2 value) => new(-value.x, -value.y);
2026-09-14 13:29:59 +02:00
2026-05-08 01:49:44 +01:00
public static float2 operator +(float2 left, float2 right) => new(left.x + right.x, left.y + right.y);
2026-09-14 13:29:59 +02:00
public static float2 operator +(float2 left, float right) => new(left.x + right, left.y + right);
public static float2 operator +(float left, float2 right) => new(left + right.x, left + right.y);
2026-05-08 01:49:44 +01:00
public static float2 operator -(float2 left, float2 right) => new(left.x - right.x, left.y - right.y);
2026-09-14 13:29:59 +02:00
public static float2 operator -(float2 left, float right) => new(left.x - right, left.y - right);
public static float2 operator -(float left, float2 right) => new(left - right.x, left - right.y);
2026-05-08 01:49:44 +01:00
public static float2 operator *(float2 left, float2 right) => new(left.x * right.x, left.y * right.y);
2026-09-14 13:29:59 +02:00
public static float2 operator *(float2 left, float right) => new(left.x * right, left.y * right);
public static float2 operator *(float left, float2 right) => new(left * right.x, left * right.y);
2026-05-08 01:49:44 +01:00
public static float2 operator /(float2 left, float2 right) => new(left.x / right.x, left.y / right.y);
2026-09-14 13:29:59 +02:00
public static float2 operator /(float2 left, float right) => new(left.x / right, left.y / right);
public static float2 operator /(float left, float2 right) => new(left / right.x, left / right.y);
2026-05-08 01:49:44 +01:00
public static float2 operator %(float2 left, float2 right) => new(left.x % right.x, left.y % right.y);
2026-09-14 13:29:59 +02:00
public static float2 operator %(float2 left, float right) => new(left.x % right, left.y % right);
public static float2 operator %(float left, float2 right) => new(left % right.x, left % right.y);
public static bool2 operator <(float2 left, float2 right) => new(left.x < right.x, left.y < right.y);
public static bool2 operator <(float2 left, float right) => new(left.x < right, left.y < right);
public static bool2 operator <(float left, float2 right) => new(left < right.x, left < right.y);
public static bool2 operator >(float2 left, float2 right) => new(left.x > right.x, left.y > right.y);
public static bool2 operator >(float2 left, float right) => new(left.x > right, left.y > right);
public static bool2 operator >(float left, float2 right) => new(left > right.x, left > right.y);
public static bool2 operator <=(float2 left, float2 right) => new(left.x <= right.x, left.y <= right.y);
public static bool2 operator <=(float2 left, float right) => new(left.x <= right, left.y <= right);
public static bool2 operator <=(float left, float2 right) => new(left <= right.x, left <= right.y);
public static bool2 operator >=(float2 left, float2 right) => new(left.x >= right.x, left.y >= right.y);
public static bool2 operator >=(float2 left, float right) => new(left.x >= right, left.y >= right);
public static bool2 operator >=(float left, float2 right) => new(left >= right.x, left >= right.y);
public static bool2 operator ==(float2 left, float2 right) => new(left.x == right.x, left.y == right.y);
public static bool2 operator ==(float2 left, float right) => new(left.x == right, left.y == right);
public static bool2 operator ==(float left, float2 right) => new(left == right.x, left == right.y);
public static bool2 operator !=(float2 left, float2 right) => new(left.x != right.x, left.y != right.y);
public static bool2 operator !=(float2 left, float right) => new(left.x != right, left.y != right);
public static bool2 operator !=(float left, float2 right) => new(left != right.x, left != right.y);
public readonly bool Equals(float2 other) => x.Equals(other.x) && y.Equals(other.y);
public override readonly bool Equals(object? obj) => obj is float2 other && Equals(other);
public override readonly int GetHashCode() => HashCode.Combine(x, y);
public override readonly string ToString() => FormattableString.Invariant($"float2({x:R}, {y:R})");
2026-05-08 01:49:44 +01:00
}