-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathVec2.cs
225 lines (182 loc) · 5.47 KB
/
Vec2.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
using System;
namespace Nitemare3D
{
public sealed class Vec2
{
public float X, Y;
public const float PI = 3.14159265359f;
public const float Deg2Rad = (PI / 180);
public const float Rad2Deg = (180 / PI);
public static Vec2 Up
{
get
{
return new Vec2(0, -1);
}
}
public static bool linePoint(Vec2 start, Vec2 end, Vec2 point)
{
var px = point.X;
var py = point.Y;
var x1 = start.X;
var x2 = end.X;
var y1 = start.Y;
var y2 = end.Y;
// get distance from the point to the two ends of the line
float d1 = Distance(new Vec2(px, py), new Vec2(x1, y1));
float d2 = Distance(new Vec2(px, py), new Vec2(x2, y2));
// get the length of the line
float lineLen = Distance(start, end);
// since floats are so minutely accurate, add
// a little buffer zone that will give collision
float buffer = 0.1f; // higher # = less accurate
// if the two distances are equal to the line's
// length, the point is on the line!
// note we use the buffer here to give a range,
// rather than one #
if (d1 + d2 >= lineLen - buffer && d1 + d2 <= lineLen + buffer)
{
return true;
}
return false;
}
public Vec2 Rounded()
{
return new Vec2(MathF.Round(X), MathF.Round(Y));
}
public static Vec2 Down
{
get
{
return new Vec2(0, 1);
}
}
public static Vec2 Left
{
get
{
return new Vec2(-1, 0);
}
}
public static Vec2 Right
{
get
{
return new Vec2(1, 0);
}
}
public Vec2()
{
this.X = 0;
this.Y = 0;
}
public override bool Equals(object obj)
{
var vec = obj as Vec2;
return (int)vec.X == (int)X && (int)vec.Y == (int)Y;
}
public static Vec2 Zero()
{
return new Vec2();
}
public Vec2(float X, float Y)
{
this.X = X;
this.Y = Y;
}
public static Vec2 Transform(Vec2 Point, float Angle)
{
return Transform(new Vec2(0, 0), Point, Angle);
}
public static Vec2 operator +(Vec2 A, Vec2 B)
{
return new Vec2(A.X + B.X, A.Y + B.Y);
}
public static Vec2 operator -(Vec2 A, Vec2 B)
{
return new Vec2(A.X - B.X, A.Y - B.Y);
}
public static Vec2 operator /(Vec2 A, Vec2 B)
{
return new Vec2(A.X / B.X, A.Y / B.Y);
}
public static Vec2 operator *(Vec2 A, Vec2 B)
{
return new Vec2(A.X * B.X, A.Y * B.Y);
}
public static Vec2 operator +(Vec2 A, Vec2i B)
{
return new Vec2(A.X + B.X, A.Y + B.Y);
}
public static Vec2 operator -(Vec2 A, Vec2i B)
{
return new Vec2(A.X - B.X, A.Y - B.Y);
}
public static Vec2 operator /(Vec2 A, Vec2i B)
{
return new Vec2(A.X / B.X, A.Y / B.Y);
}
public static Vec2 operator *(Vec2 A, Vec2i B)
{
return new Vec2(A.X * B.X, A.Y * B.Y);
}
public static Vec2 operator /(Vec2 A, float B)
{
return new Vec2(A.X / B, A.Y / B);
}
public static Vec2 operator +(Vec2 A, float B)
{
return new Vec2(A.X + B, A.Y + B);
}
public static Vec2 operator -(Vec2 A, float B)
{
return new Vec2(A.X - B, A.Y - B);
}
public static Vec2 operator *(Vec2 A, float B)
{
return new Vec2(A.X * B, A.Y * B);
}
public static Vec2 operator %(Vec2 A, float B)
{
return new Vec2(A.X % B, A.Y % B);
}
public static float LookAt(Vec2 me, Vec2 target)
{
var dir = target - me;
var r = (float)Math.Atan2(dir.Y, dir.X);
return (r * Rad2Deg) + 180;
}
public static Vec2 Transform(Vec2 Origin, Vec2 Point, float Angle)
{
float c = (float)Math.Cos(Angle);
float s = (float)Math.Sin(Angle);
return new Vec2(
Origin.X + c * Point.X - s * Point.Y,
Origin.Y + s * Point.X + c * Point.Y
) - Origin * 2;
}
internal static float Dot(Vec2 a, Vec2 b)
{
return (a.X * b.X) + (a.Y * b.Y);
}
public static float Magnitude(Vec2 A)
{
return (float)Math.Sqrt(A.X * A.X + A.Y * A.Y);
}
public Vec2 Normalize()
{
float length = Magnitude(this);
return new Vec2(X / length, Y / length);
}
public static float Distance(Vec2 A, Vec2 B)
{
return (float)Math.Sqrt(Math.Pow((B.X - A.X), 2) + Math.Pow((B.Y - A.Y), 2));
}
public static Vec2 Cross(Vec2 lhs, Vec2 rhs)
{
return new Vec2(
lhs.Y * 0 - 0 * rhs.Y,
0 * rhs.X - lhs.X * 0);
}
}
}