-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vector2.cs
54 lines (41 loc) · 2.15 KB
/
Vector2.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
using System;
namespace WinForMono.Graphics {
public sealed class Vector2 : IEquatable<Vector2> {
public Vector2() { x = 0; y = 0; }
public Vector2(float _x, float _y) { x = _x; y = _y; }
public float x {
get;
set;
}
public float y {
get;
set;
}
public bool Equals(Vector2 other) {
return (
x == other.x &&
y == other.y
);
}
public override string ToString() {
return "Vector2 {\v" +
new string('\b', 5) + x + ",\v" +
new string('\b', x.ToString().Length+1) + y + "\v" +
new string('\b', y.ToString().Length+4) + // Go back to same column as the "V" of "Vector2"
"}";
}
public float dot(Vector2 other) { float total = 0.0f; total+=(this.x*other.x); total+=(this.y*other.y); return total; }
public float magnitude() => (float)Math.Sqrt((float)Math.Pow(x, 2.0f) + (float)Math.Pow(y, 2.0f));
public Vector2 interp(Vector2 other, float interp_amt) => this+((other-this)*interp_amt);
public Vector2 midpoint(Vector2 other) => (this+other)/2.0f;
public Vector2 unit() { float mag = magnitude(); return new Vector2(x/mag, y/mag); }
public static bool operator ==(Vector2 self, Vector2 other) => self.Equals(other);
public static bool operator !=(Vector2 self, Vector2 other) => !self.Equals(other);
public static Vector2 operator +(Vector2 self, Vector2 other) => new Vector2(self.x+other.x, self.y+other.y);
public static Vector2 operator -(Vector2 self, Vector2 other) => new Vector2(self.x-other.x, self.y-other.y);
public static Vector2 operator *(Vector2 self, float other) => new Vector2(self.x*other, self.y*other);
public static Vector2 operator /(Vector2 self, float other) => new Vector2(self.x/other, self.y/other);
public static implicit operator System.Drawing.PointF(Vector2 self) => new System.Drawing.PointF(self.x, self.y);
public static readonly Vector2 ZERO = new Vector2(0.0f, 0.0f);
}
}