-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVector2D.cs
104 lines (94 loc) · 3.28 KB
/
Vector2D.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
namespace OrbitSim
{
/// <summary>
/// Vector2D (Simple and fast)
/// Author: Keven Villeneuve
/// </summary>
public class Vector2D
{
public double x { get; set; } //X component
public double y { get; set; } //Y component
public double lengthSquared //Vector's length squared
{
get { return x*x + y*y; }
}
public double length //Vector's length
{
get { return System.Math.Sqrt(lengthSquared); }
}
/// <summary>
/// Create a vector from it's components.
/// </summary>
/// <param name="x">X component.</param>
/// <param name="y">Y component.</param>
public Vector2D(double x, double y)
{
this.x = x;
this.y = y;
}
/// <summary>
/// Return a copy of the normalized vector.
/// </summary>
/// <returns>Normalized vector</returns>
public Vector2D getUnitVector()
{
double length = this.length;
return new Vector2D(x/length, y/length);
}
/// <summary>
/// Overload + : Add 2 vectors.
/// </summary>
/// <param name="vector1">Vector 1</param>
/// <param name="vector2">Vector 2</param>
/// <returns>Vector added of vector1 and vector2</returns>
public static Vector2D operator +(Vector2D vector1, Vector2D vector2)
{
return new Vector2D(vector1.x + vector2.x, vector1.y + vector2.y);
}
/// <summary>
/// Overload + : Subtract 2 vectors.
/// </summary>
/// <param name="vector1"></param>
/// <param name="vector2"></param>
/// <returns>Vector subbed of vector1 and vector2</returns>
public static Vector2D operator -(Vector2D vector1, Vector2D vector2)
{
return new Vector2D(vector1.x - vector2.x, vector1.y - vector2.y);
}
/// <summary>
/// Overload + : Multiply a vector and a value.
/// </summary>
/// <param name="vector">Vector</param>
/// <param name="value">Value</param>
/// <returns>Vector multiplied by a value</returns>
public static Vector2D operator *(Vector2D vector, double value)
{
return new Vector2D(vector.x*value, vector.y*value);
}
/// <summary>
/// Overload + : Multiply a value and a vector.
/// </summary>
/// <param name="value">Value</param>
/// <param name="vector">Vector</param>
/// <returns>Vector multiplied by a value</returns>
public static Vector2D operator *(double value, Vector2D vector)
{
return new Vector2D(vector.x * value, vector.y * value);
}
/// <summary>
/// Overload + : Divide a vector and a value.
/// </summary>
/// <param name="vector">Vector</param>
/// <param name="value">Value</param>
/// <returns>Vector divided by a value</returns>
public static Vector2D operator /(Vector2D vector, double value)
{
return new Vector2D(vector.x * 1/value, vector.y * 1/value);
}
public override string ToString()
{
return "Vector2D: (" + x + "," + y + ") :" + length;
}
}
}