renamed Pos and Pos3
This commit is contained in:
111
src/lib/WorldPos.hx
Normal file
111
src/lib/WorldPos.hx
Normal file
@@ -0,0 +1,111 @@
|
||||
package lib;
|
||||
|
||||
import lib.Vec.Vec3;
|
||||
|
||||
/**
|
||||
Reporesents a Point in a 3D `Float` System.
|
||||
Basicly a wrapper for Vec3<Float> with some extra functions.
|
||||
`Y` represents the height of the point.
|
||||
**/
|
||||
@:forward(x, y, z)
|
||||
abstract WorldPos(Vec3<Float>) from Vec3<Float> to Vec3<Float> {
|
||||
inline public function new(i:Vec3<Float>) {
|
||||
this = i;
|
||||
}
|
||||
|
||||
@:op(A + B)
|
||||
public function add(rhs:Vec3<Float>):WorldPos {
|
||||
return new WorldPos({
|
||||
y: this.y + rhs.y,
|
||||
x: this.x + rhs.x,
|
||||
z: this.z + rhs.z
|
||||
});
|
||||
}
|
||||
|
||||
@:op(A - B)
|
||||
public function sub(rhs:Vec3<Float>):WorldPos {
|
||||
return new WorldPos({
|
||||
y: this.y - rhs.y,
|
||||
x: this.x - rhs.x,
|
||||
z: this.z - rhs.z
|
||||
});
|
||||
}
|
||||
|
||||
@:op(A * B)
|
||||
public function multiplyScalar(rhs:Float):WorldPos {
|
||||
return new WorldPos({
|
||||
y: this.y * rhs,
|
||||
x: this.x * rhs,
|
||||
z: this.z * rhs
|
||||
});
|
||||
}
|
||||
|
||||
@:op(A / B)
|
||||
public function divideScalar(rhs:Float):WorldPos {
|
||||
return new WorldPos({
|
||||
y: this.y / rhs,
|
||||
x: this.x / rhs,
|
||||
z: this.z / rhs
|
||||
});
|
||||
}
|
||||
|
||||
@:op(-A)
|
||||
public function negate():WorldPos {
|
||||
return new WorldPos({
|
||||
y: -this.y,
|
||||
x: -this.x,
|
||||
z: -this.z
|
||||
});
|
||||
}
|
||||
|
||||
public function dot(rhs:Vec3<Float>):Float {
|
||||
return this.x * rhs.x + this.y * rhs.y + this.z * rhs.z;
|
||||
}
|
||||
|
||||
public function cross(rhs:Vec3<Float>):WorldPos {
|
||||
return new WorldPos({
|
||||
x: this.y * rhs.z - this.z * rhs.y,
|
||||
y: this.z * rhs.x - this.x * rhs.z,
|
||||
z: this.x * rhs.y - this.y * rhs.x
|
||||
});
|
||||
}
|
||||
|
||||
public function normalize():WorldPos {
|
||||
var l = length();
|
||||
return multiplyScalar(1 / l);
|
||||
}
|
||||
|
||||
public function length():Float {
|
||||
return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
|
||||
}
|
||||
|
||||
@:op(A == B)
|
||||
public function equals(rhs:WorldPos):Bool {
|
||||
return close(rhs);
|
||||
}
|
||||
|
||||
@:op(A != B)
|
||||
public function notEquals(rhs:WorldPos):Bool {
|
||||
return !close(rhs);
|
||||
}
|
||||
|
||||
public function close(rhs:WorldPos, epsilon:Float = 0.001):Bool {
|
||||
return Math.abs(this.x - rhs.x) < epsilon && Math.abs(this.y - rhs.y) < epsilon && Math.abs(this.z - rhs.z) < epsilon;
|
||||
}
|
||||
|
||||
public function toString():String {
|
||||
return 'WorldPos(${this.x}, ${this.y}, ${this.z})';
|
||||
}
|
||||
|
||||
public function round():WorldPos {
|
||||
return new WorldPos({
|
||||
x: Math.fround(this.x),
|
||||
y: Math.fround(this.y),
|
||||
z: Math.fround(this.z)
|
||||
});
|
||||
}
|
||||
|
||||
public function distance(rhs:WorldPos):Float {
|
||||
return Math.sqrt(Math.pow(this.x - rhs.x, 2) + Math.pow(this.y - rhs.y, 2) + Math.pow(this.z - rhs.z, 2));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user