94 lines
2.0 KiB
C#
94 lines
2.0 KiB
C#
using System.Runtime.InteropServices;
|
|
|
|
namespace Prism
|
|
{
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
public struct Vec2
|
|
{
|
|
public float X;
|
|
public float Y;
|
|
|
|
|
|
public static Vec2 Zero = new Vec2(0.0f, 0.0f);
|
|
public Vec2(float scalar)
|
|
{
|
|
X = Y = scalar;
|
|
}
|
|
|
|
public Vec2(float x, float y)
|
|
{
|
|
X = x;
|
|
Y = y;
|
|
}
|
|
|
|
public Vec2(Vec3 vec) {
|
|
X = vec.X;
|
|
Y = vec.Y;
|
|
}
|
|
|
|
public void Clamp(Vec2 min, Vec2 max) {
|
|
X = Mathf.Clamp(X, min.X, max.X);
|
|
Y = Mathf.Clamp(Y, min.Y, max.Y);
|
|
}
|
|
|
|
public float LengthSquared()
|
|
{
|
|
return X * X + Y * Y;
|
|
}
|
|
|
|
public float Length()
|
|
{
|
|
return (float)Math.Sqrt(X * X + Y * Y);
|
|
}
|
|
|
|
public Vec2 SafeNormalized()
|
|
{
|
|
float length = Length();
|
|
if (length > float.Epsilon)
|
|
{
|
|
return new Vec2(X / length, Y / length);
|
|
}
|
|
else
|
|
{
|
|
return Zero;
|
|
}
|
|
}
|
|
public Vec2 Normalized()
|
|
{
|
|
float length = Length();
|
|
return new Vec2(X / length, Y / length);
|
|
}
|
|
|
|
public void SafeNormalize()
|
|
{
|
|
float length = Length();
|
|
if (length > float.Epsilon)
|
|
{
|
|
X = X / length;
|
|
Y = Y / length;
|
|
}
|
|
}
|
|
|
|
public void Normalize()
|
|
{
|
|
float length = Length();
|
|
X = X / length;
|
|
Y = Y / length;
|
|
}
|
|
|
|
public static Vec2 operator -(Vec2 l, Vec2 r)
|
|
{
|
|
return new Vec2(l.X - r.X, l.Y - r.Y);
|
|
}
|
|
|
|
public static Vec2 operator -(Vec2 vector)
|
|
{
|
|
return new Vec2(-vector.X, -vector.Y);
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
return $"({X}, {Y})";
|
|
}
|
|
}
|
|
} |