-
Notifications
You must be signed in to change notification settings - Fork 0
/
vec2.bf
190 lines (147 loc) · 3.78 KB
/
vec2.bf
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
using System;
namespace Quickmaths {
public struct vec2 : IFormattable {
public static readonly vec2 X = vec2(1,0);
public static readonly vec2 Y = vec2(0,1);
public float x;
public float y;
//////////////////////////////////////////////////////////////////////////
// Constructors
//////////////////////////////////////////////////////////////////////////
public this(float x, float y) {
this.x = x;
this.y = y;
}
public this(float x) {
this.x = x;
this.y = x;
}
//////////////////////////////////////////////////////////////////////////
// Methods
//////////////////////////////////////////////////////////////////////////
public float dot(vec2 vec) {
return this.x*vec.x + this.y+vec.y;
}
public float cross(vec2 vec) {
return this.x*vec.y + this.y*vec.x;
}
public float magnitude() {
return Math.Sqrt(magnitude2());
}
public float magnitude2() {
return x*x + y*y;
}
public vec2 normal() {
float mag = magnitude();
if (mag == 0)
return this;
return this / magnitude();
}
public vec2 trim(float length) {
return this.normal() * length;
}
public vec2 clampLength(float min, float max) {
return this.normal() * Math.Clamp(this.magnitude(), min, max);
}
public vec2 abs() {
return vec2(
Math.Abs(this.x),
Math.Abs(this.y) );
}
public vec2 floor() {
return vec2(
Math.Floor(this.x),
Math.Floor(this.y) );
}
public vec2 ceil() {
return vec2(
Math.Ceiling(this.x),
Math.Ceiling(this.y) );
}
public vec2 clamp(vec2 min, vec2 max) {
return vec2(
Math.Clamp(this.x, min.x, max.x),
Math.Clamp(this.y, min.y, max.y) );
}
//////////////////////////////////////////////////////////////////////////
// Overrides
//////////////////////////////////////////////////////////////////////////
public void ToString(String outString, String format, IFormatProvider formatProvider) {
outString.Append("(");
x.ToString(outString, format, formatProvider);
outString.Append(",");
y.ToString(outString, format, formatProvider);
outString.Append(")");
}
//////////////////////////////////////////////////////////////////////////
// Casting
//////////////////////////////////////////////////////////////////////////
public static explicit operator vec2(float[2] v) {
return vec2( v[0], v[1] );
}
public static explicit operator float[2](vec2 v) {
return float[2]( v.x, v.y );
}
//////////////////////////////////////////////////////////////////////////
// Operators
//////////////////////////////////////////////////////////////////////////
public static bool operator==(vec2 a, vec2 b) {
return a.x == b.x
&& a.y == b.y;
}
public static bool operator!=(vec2 a, vec2 b) {
return a.x != b.x
|| a.y != b.y;
}
public static vec2 operator+(vec2 a, vec2 b) {
return vec2(
a.x + b.x,
a.y + b.y );
}
public static vec2 operator-(vec2 a, vec2 b) {
return vec2(
a.x - b.x,
a.y - b.y );
}
public static vec2 operator-(vec2 a) {
return vec2(
-a.x,
-a.y );
}
public static vec2 operator*(vec2 a, vec2 b) {
return vec2(
a.x * b.x,
a.y * b.y );
}
public static vec2 operator*(float a, vec2 b) {
return vec2(
a * b.x,
a * b.y );
}
public static vec2 operator*(vec2 a, float b) {
return vec2(
a.x * b,
a.y * b );
}
public static vec2 operator/(vec2 a, vec2 b) {
return vec2(
a.x / b.x,
a.y / b.y );
}
public static vec2 operator/(float a, vec2 b) {
return vec2(
a / b.x,
a / b.y );
}
public static vec2 operator/(vec2 a, float b) {
return vec2(
a.x / b,
a.y / b );
}
public static vec2 operator%(vec2 a, vec2 b) {
return vec2(
a.x % b.x,
a.y % b.y );
}
}
}