Adds some helper utilities.
This commit is contained in:
parent
5d9c201e54
commit
5357da3746
4 changed files with 412 additions and 0 deletions
151
lib/utils/Vector2.dart
Normal file
151
lib/utils/Vector2.dart
Normal file
|
@ -0,0 +1,151 @@
|
|||
class Vector2d {
|
||||
static final Vector2d ZERO = Vector2d(X: 0, Z: 0);
|
||||
double X = 0.0;
|
||||
double Z = 0.0;
|
||||
|
||||
Vector2d({this.X = 0.0, this.Z = 0.0});
|
||||
|
||||
@override
|
||||
bool operator ==(Object otherz) {
|
||||
if (otherz is Vector2d) {
|
||||
Vector2d other = otherz as Vector2d;
|
||||
return X == other.X && Z == other.Z;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
Vector2d operator +(Vector2d other) {
|
||||
Vector2d n = Clone();
|
||||
n.X += other.X;
|
||||
n.Z += other.Z;
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
Vector2d operator -(Vector2d other) {
|
||||
Vector2d n = Clone();
|
||||
n.X -= other.X;
|
||||
n.Z -= other.Z;
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
Vector2d operator *(Vector2d other) {
|
||||
Vector2d n = Clone();
|
||||
n.X *= other.X;
|
||||
n.Z *= other.Z;
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
Vector2d operator /(Vector2d other) {
|
||||
Vector2d n = Clone();
|
||||
n.X /= other.X;
|
||||
n.Z /= other.Z;
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
bool operator >(Vector2d other) {
|
||||
return (X > other.X) || (X > other.Z);
|
||||
}
|
||||
|
||||
bool operator <(Vector2d other) {
|
||||
return (X < other.X) || (X < other.Z);
|
||||
}
|
||||
|
||||
Vector2d Clone() {
|
||||
return Vector2d(X: X, Z: Z);
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return "<${X}, ${Z}>";
|
||||
}
|
||||
|
||||
bool inside(Vector2d min, Vector2d max) {
|
||||
if (min.X <= X && max.X >= X) {
|
||||
if (min.Z <= Z && max.Z >= Z) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
class Vector2i {
|
||||
static final Vector2i ZERO = Vector2i(X: 0, Z: 0);
|
||||
int X = 0;
|
||||
int Z = 0;
|
||||
|
||||
Vector2i({this.X = 0, this.Z = 0});
|
||||
|
||||
@override
|
||||
bool operator ==(Object otherz) {
|
||||
if (otherz is Vector2i) {
|
||||
Vector2i other = otherz as Vector2i;
|
||||
return X == other.X && Z == other.Z;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
Vector2i operator +(Vector2i other) {
|
||||
Vector2i n = Clone();
|
||||
n.X += other.X;
|
||||
n.Z += other.Z;
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
Vector2i operator -(Vector2i other) {
|
||||
Vector2i n = Clone();
|
||||
n.X -= other.X;
|
||||
n.Z -= other.Z;
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
Vector2i operator *(Vector2i other) {
|
||||
Vector2i n = Clone();
|
||||
n.X *= other.X;
|
||||
n.Z *= other.Z;
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
Vector2i operator /(Vector2i other) {
|
||||
Vector2i n = Clone();
|
||||
n.X = (n.X / other.X).round();
|
||||
n.Z = (n.Z / other.Z).round();
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
bool operator >(Vector2i other) {
|
||||
return (X > other.X) || (X > other.Z);
|
||||
}
|
||||
|
||||
bool operator <(Vector2i other) {
|
||||
return (X < other.X) || (X < other.Z);
|
||||
}
|
||||
|
||||
Vector2i Clone() {
|
||||
return Vector2i(X: X, Z: Z);
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return "<${X}, ${Z}>";
|
||||
}
|
||||
|
||||
bool inside(Vector2i min, Vector2i max) {
|
||||
if (min.X <= X && max.X >= X) {
|
||||
if (min.Z <= Z && max.Z >= Z) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue