publicstruct Vector2 { ...... publicfloat Length { get => (float)Math.Sqrt(X * X + Y * Y); } publicstatic Vector2 Up { get => new Vector2(0, 1f); } publicstatic Vector2 Down { get => new Vector2(0, -1f); } publicstatic Vector2 Left { get => new Vector2(-1f, 0); } publicstatic Vector2 Right { get => new Vector2(1f, 0); } publicstatic Vector2 Zero { get => new Vector2(0, 0); } }
publicstruct Vector2 { publicVector2(float x, float y) { X = x; Y = y; }
publicfloat X { get; } publicfloat Y { get; } publicfloat Length { get => (float)Math.Sqrt(X * X + Y * Y); } publicstatic Vector2 Up { get => new Vector2(0, 1f); } publicstatic Vector2 Down { get => new Vector2(0, -1f); } publicstatic Vector2 Left { get => new Vector2(-1f, 0); } publicstatic Vector2 Right { get => new Vector2(1f, 0); } publicstatic Vector2 Zero { get => new Vector2(0, 0); }
publicstaticbooloperator ==(Vector2 a, Vector2 b) => a.Equals(b); publicstaticbooloperator !=(Vector2 a, Vector2 b) => !(a == b); publicstatic Vector2 operator +(Vector2 a, Vector2 b) => new Vector2(a.X + b.X, a.Y + b.Y); publicstatic Vector2 operator -(Vector2 a, Vector2 b) => new Vector2(a.X - b.X, a.Y - b.Y); publicstatic Vector2 operator *(Vector2 a, Vector2 b) => new Vector2(a.X * b.X, a.Y * b.Y); publicstatic Vector2 operator *(Vector2 a, float b) => new Vector2(a.X * b, a.Y * b); publicstatic Vector2 operator /(Vector2 a, Vector2 b) => new Vector2(a.X / b.X, a.Y / b.Y); publicstatic Vector2 operator /(Vector2 a, float b) => new Vector2(a.X / b, a.Y / b);
publicstatic Vector2 Normalize(Vector2 vector) { Vector2 v = vector; v /= Math.Max(Math.Abs(v.X), Math.Abs(v.Y)); v /= v.Length; return v; } }