Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Vector2 code

I needed a Vector2 struct for a project and I am not very strong with math, this class seems to be working but I would greatly appreciate any pointers on how to I could optimize the code or if I have made any errors. Also is there a cheaper way to do distance calculation than Math.Sqrt?

    private struct Vector2
    {
        public float X;
        public float Y;

        public Vector2(float x, float y)
        {
            this.X = x;
            this.Y = y;
        }

        public static Vector2 operator +(Vector2 v1, Vector2 v2)
        {
            return new Vector2(v1.X + v2.X, v1.Y + v2.Y);
        }

        public static Vector2 operator -(Vector2 v1, Vector2 v2)
        {
            return new Vector2(v1.X - v2.X, v1.Y - v2.Y);
        }

        public static Vector2 operator *(Vector2 v1, float m)
        {
            return new Vector2(v1.X * m, v1.Y * m);
        }

        public static float operator *(Vector2 v1, Vector2 v2)
        {
            return v1.X * v2.X + v1.Y * v2.Y;
        }

        public static Vector2 operator /(Vector2 v1, float m)
        {
            return new Vector2(v1.X / m, v1.Y / m);
        }

        public static float Distance(Vector2 v1, Vector2 v2)
        {
            return (float)Math.Sqrt(Math.Pow(v1.X - v2.X, 2) + Math.Pow(v1.Y - v2.Y, 2));
        }

        public float Length()
        {
            return (float)Math.Sqrt(X * X + Y * Y);
        }
    }
like image 875
Antonio Nicolaas Teyken Avatar asked Jul 05 '26 22:07

Antonio Nicolaas Teyken


1 Answers

Just 3 comments:

1)In C#, we usually expose properties instead of fields. That is,

    public float X {get; private set;}
    public float Y {get; private set;}

2) The type double gives a higher precision than float, so i would advise you to use double instead of float.

3) You overloaded one of your operators as :

    public static float operator *(Vector2 v1, Vector2 v2)
    {
        return v1.X * v2.X + v1.Y * v2.Y;
    }

Which I take to be the dot (inner product) of two 2d real vectors. In programming, when we overload an operator, the overload should do what the operator does. That is, if we were to overload the operator *(Vector2 v1, Vector2 v2), the overload should multiply these two vectors together. However, there is no definite definition of "multiplication" for two vectors. Using the term "multiplication" in linear algebra is extremely vague. You could be referring to tensor products, outer products, inner products, cross products, scalar product, etc. So to make your code less confusing, I would recommend you implement the inner product operator as

public static float InnerProduct(Vector2 v1, Vector v2)

instead.

As for a better implementation of the square root function, Math.Sqrt is probably one of the fastest one (in the .NET framework). If you were thinking of improving the speed of your class, then writing it in native C/C++ and then writting a managed wrapper for it will probably give you the most improvements.

like image 97
Yiyuan Lee Avatar answered Jul 07 '26 12:07

Yiyuan Lee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!