-
Notifications
You must be signed in to change notification settings - Fork 0
/
PVector2.cs
111 lines (89 loc) · 3.68 KB
/
PVector2.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
105
106
107
108
109
110
111
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Lone_Mars {
struct PVector2 {
public static readonly PVector2 Zero = new PVector2(FInt.ZeroF, FInt.ZeroF);
public static readonly PVector2 Up = new PVector2(FInt.ZeroF, FInt.OneF);
public static readonly PVector2 Down = new PVector2(FInt.ZeroF, FInt.NOneF);
public static readonly PVector2 Left = new PVector2(FInt.NOneF, FInt.ZeroF);
public static readonly PVector2 Right = new PVector2(FInt.OneF, FInt.ZeroF);
public FInt X;
public FInt Y;
static FInt scratch1 = FInt.ZeroF;
static FInt scratch2 = FInt.ZeroF;
static PVector2 scratchV = PVector2.Zero;
public PVector2(FInt x, FInt y) {
X = x;
Y = y;
}
public FInt Length() {
var dx = X * X;
var dy = Y * Y;
return FInt.Sqrt(dx + dy);
}
public FInt LengthSquared() {
FInt.Multiply(ref X, ref X, out scratch1);
FInt.Multiply(ref Y, ref Y, out scratch2);
FInt.Add(ref scratch1, ref scratch2, out scratch1);
return scratch1;
}
public void LengthSquared(out FInt i) {
FInt.Multiply(ref X, ref X, out scratch1);
FInt.Multiply(ref Y, ref Y, out scratch2);
FInt.Add(ref scratch1, ref scratch2, out i);
}
public void DistanceSquaredTo(ref PVector2 other, out FInt i) {
PVector2.Sub(ref this, ref other, out scratchV);
scratchV.LengthSquared(out i);
}
public static PVector2 operator +(PVector2 a, PVector2 b) {
return new PVector2(a.X + b.X, a.Y + b.Y);
}
public static void Limit(ref PVector2 val, FInt maxLength) {
var l = val.Length();
if (l > maxLength) {
var ratio = maxLength / l;
val.X *= ratio;
val.Y *= ratio;
}
}
public static void Add(ref PVector2 a, ref PVector2 b, out PVector2 c) {
FInt.Add(ref a.X, ref b.X, out c.X);
FInt.Add(ref a.Y, ref b.Y, out c.Y);
}
public static PVector2 operator -(PVector2 a, PVector2 b) {
return new PVector2(a.X - b.X, a.Y - b.Y);
}
public static void Sub(ref PVector2 a, ref PVector2 b, out PVector2 dest) {
FInt.Sub(ref a.X, ref b.X, out dest.X);
FInt.Sub(ref a.Y, ref b.Y, out dest.Y);
}
public static PVector2 operator *(PVector2 a, FInt i) {
return new PVector2(a.X * i, a.Y * i);
}
public static void Multiply(ref PVector2 v, ref FInt i, out PVector2 o) {
FInt.Multiply(ref v.X, ref i, out o.X);
FInt.Multiply(ref v.Y, ref i, out o.Y);
}
public static PVector2 operator /(PVector2 a, FInt i) {
return new PVector2(a.X / i, a.Y / i);
}
public static bool operator ==(PVector2 a, PVector2 b) {
return a.X == b.X && a.Y == b.Y;
}
public static bool operator !=(PVector2 a, PVector2 b) {
return a.X != b.X || a.Y != b.Y;
}
public override bool Equals(object obj) {
return (obj is PVector2 && this == (PVector2)obj);
}
public override int GetHashCode() {
return X.GetHashCode() + Y.GetHashCode();
}
public Microsoft.Xna.Framework.Vector2 ToVector2() {
return new Microsoft.Xna.Framework.Vector2((float)X.ToDouble(), (float)Y.ToDouble());
}
}
}