-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector.cs
More file actions
120 lines (110 loc) · 2.5 KB
/
Vector.cs
File metadata and controls
120 lines (110 loc) · 2.5 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using System;
public class Vector
{
#region Private Vars
private double _x, _y;
#endregion
#region Properties
public double X
{
get
{
return _x;
}
set
{
_x = value;
}
}
public double Y
{
get
{
return _y;
}
set
{
_y = value;
}
}
public double Angle
{
get
{
return Math.Atan2(_y, _x);
}
set
{
double len = this.Length;
_x = Math.Cos(value) * len;
_y = Math.Sin(value) * len;
}
}
public double Length
{
get
{
return Math.Sqrt(Math.Pow(_x, 2) + Math.Pow(_y, 2));
}
set
{
var angle = this.Angle;
_x = Math.Cos(angle) * value;
_y = Math.Sin(angle) * value;
}
}
public int iX
{
get { return (int)_x; }
}
public int iY
{
get { return (int)_y; }
}
#endregion
#region Constructors
public Vector(double XComp, double YComp)
{
this.X = XComp;
this.Y = YComp;
}
#endregion
#region Operators
public static Vector operator +(Vector v1, Vector v2) => new Vector(v1.X + v2.X, v1.Y + v2.Y);
public static Vector operator -(Vector v1, Vector v2) => new Vector(v1.X - v2.X, v1.Y - v2.Y);
public static Vector operator *(Vector v1, double multiplier) => new Vector(v1.X * multiplier, v1.Y * multiplier);
public static Vector operator /(Vector v1, double d) => new Vector(v1.X / d, v1.Y / d);
#endregion
#region Methods
public void AddTo(Vector V)
{
this._x += V.X;
this._y += V.Y;
}
public void SubtractFrom(Vector V)
{
this._x -= V.X;
this._y -= V.Y;
}
public void MultiplyBy(double Value)
{
this._x *= Value;
this._y *= Value;
}
public void DivideBy(double Value)
{
this._x /= Value;
this._y /= Value;
}
public double AngleTo(Vector vector)
{
return Math.Atan2(vector.Y - this.Y, vector.X - this.X);
}
public double DistanceTo(Vector vector)
{
var dx = vector.X - this.X;
var dy = vector.Y - this.Y;
return Math.Sqrt(Math.Pow(dx, 2) + Math.Pow(dy, 2));
}
#endregion
}