Programming Language: C# (CSharp)

Namespace/Package Name: System

Class/Type: Vector3

Method/Function: Distance2D

Examples at hotexamples.com: 23

The System Vector3 class in C# provides several built-in mathematical methods, including Distance2D, which calculates the two-dimensional Euclidean distance between two Vector3 points. This function can be useful for many applications, such as gaming or navigation.

**Example 1:**

In this example, we create two Vector3 points with the z-coordinate equal to zero to represent two points on a 2D plane. We then use the Distance2D method to calculate the distance between the two points, which returns 7.071068, the correct value for the distance between two points on a 2D plane.

**Example 2:**

Vector3 point1 = new Vector3(0, 0, 0); // 3D point, z-coordinate is not used Vector3 point2 = new Vector3(5, 5, 0); float distance = Vector3.Distance2D(point1, point2);

In this example, we create two Vector3 points with the z-coordinate equal to zero to represent two points on a 2D plane. We then use the Distance2D method to calculate the distance between the two points, which returns 7.071068, the correct value for the distance between two points on a 2D plane.

Vector3[] points = new Vector3[] { new Vector3(0, 0, 0), new Vector3(5, 5, 0), new Vector3(10, 10, 0) }; for (int i = 0; i < (points.Length - 1); i++) { float distance = Vector3.Distance2D(points[i], points[i + 1]); Console.WriteLine("Distance between points " + i + " and " + (i+1) + ": " + distance); }In this example, we have an array of three Vector3 points that represent three points on a 2D plane. We then iterate through the array and calculate the distance between each consecutive pair of points using Distance2D. The console output will display the distance between each pair of points. The System Vector3 class is included in the .NET Standard library and is part of the System.Numerics namespace.

Frequently Used Methods

Frequently Used Methods

Frequently Used Methods

Related in langs

Frequently Used Methods

Related