The Vector2 class in C# is a 2D vector structure that is used for mathematical operations such as adding, subtracting, multiplying, and dividing vectors. It is commonly used in game development for movement, collision detection, and physics calculations.
Here are some examples of how to use Vector2 in C#:
Example 1: Adding two vectors together `` Vector2 vector1 = new Vector2(3, 5); Vector2 vector2 = new Vector2(1, 2); Vector2 result = vector1 + vector2; Console.WriteLine(result); // Result: (4, 7) ``
Example 2: Multiplying a vector by a scalar value `` Vector2 vector = new Vector2(2, 4); float scalar = 2; Vector2 result = vector * scalar; Console.WriteLine(result); // Result: (4, 8) ``
Example 3: Normalizing a vector to a unit vector `` Vector2 vector = new Vector2(3, 4); vector.Normalize(); Console.WriteLine(vector); // Result: (0.6, 0.8) ``
These examples are part of the System.Numerics.Vectors package library, which provides a set of vector and matrix types for high-performance computing.
C# (CSharp) Vector2 - 60 examples found. These are the top rated real world C# (CSharp) examples of Vector2 extracted from open source projects. You can rate examples to help us improve the quality of examples.