improved Vector stuff

This commit is contained in:
Niklas Kapelle 2024-10-16 23:39:07 +02:00
parent b305594ea4
commit afbd1dfd68
Signed by: niklas
GPG Key ID: 4EB651B36D841D16
4 changed files with 113 additions and 117 deletions

View File

@ -7,46 +7,30 @@ import lib.Vec.Vec3;
Basicly a wrapper for Vec3<Int> with some extra functions. Basicly a wrapper for Vec3<Int> with some extra functions.
`Y` represents the height of the point. `Y` represents the height of the point.
**/ **/
@:forward(x, y, z) @:forward
abstract BlockPos(Vec3<Int>) from Vec3<Int> to Vec3<Int> { abstract BlockPos(Vec3<Int>) from Vec3<Int> to Vec3<Int> {
public inline function new(x:Int, y:Int, z:Int) { public inline function new(x:Int, y:Int, z:Int) {
this = new Vec3(x, y, z); this = new Vec3(x, y, z);
} }
@:op(A + B) @:op(A + B)
public function add(rhs:BlockPos):BlockPos { public inline function add(rhs:BlockPos):BlockPos {
return { return this.add(rhs);
y: this.y + rhs.y,
x: this.x + rhs.x,
z: this.z + rhs.z
};
} }
@:op(A - B) @:op(A - B)
public function sub(rhs:BlockPos):BlockPos { public inline function sub(rhs:BlockPos):BlockPos {
return { return this.sub(rhs);
y: this.y - rhs.y,
x: this.x - rhs.x,
z: this.z - rhs.z
};
} }
@:op(A * B) @:op(A * B)
public function multiplyScalar(rhs:Int):BlockPos { public inline function multiplyScalar(rhs:Int):BlockPos {
return { return this.multiplyScalar(rhs);
y: this.y * rhs,
x: this.x * rhs,
z: this.z * rhs
};
} }
@:op(-A) @:op(-A)
public function negate():BlockPos { public inline function negate():BlockPos {
return { return this.negate();
y: -this.y,
x: -this.x,
z: -this.z
};
} }
@:op(A == B) @:op(A == B)
@ -59,22 +43,6 @@ abstract BlockPos(Vec3<Int>) from Vec3<Int> to Vec3<Int> {
return !equals(rhs); return !equals(rhs);
} }
public function dot(rhs:BlockPos):Float {
return this.x * rhs.x + this.y * rhs.y + this.z * rhs.z;
}
public function cross(rhs:BlockPos):BlockPos {
return {
x: this.x * rhs.y - this.y * rhs.x,
y: this.y * rhs.z - this.z * rhs.y,
z: this.z * rhs.x - this.x * rhs.z,
};
}
public function length():Float {
return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
}
public function hashCode():Int { public function hashCode():Int {
return (this.x * 73856093) ^ (this.y * 19349663) ^ (this.z * 83492791); return (this.x * 73856093) ^ (this.y * 19349663) ^ (this.z * 83492791);
} }
@ -83,10 +51,6 @@ abstract BlockPos(Vec3<Int>) from Vec3<Int> to Vec3<Int> {
return 'BlockPos(${this.x}, ${this.y}, ${this.z})'; return 'BlockPos(${this.x}, ${this.y}, ${this.z})';
} }
public function distance(rhs:BlockPos):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));
}
/** /**
Returns a list of positions neighboring the block. No Diagonal. Returns a list of positions neighboring the block. No Diagonal.
**/ **/

View File

@ -13,34 +13,22 @@ abstract ScreenPos(Vec2<Int>) from Vec2<Int> to Vec2<Int> {
} }
@:op(A + B) @:op(A + B)
public function add(rhs:Vec2<Int>):ScreenPos { public inline function add(rhs:Vec2<Int>):ScreenPos {
return new ScreenPos({ return this.add(rhs);
y: this.y + rhs.y,
x: this.x + rhs.x,
});
} }
@:op(A - B) @:op(A - B)
public function sub(rhs:Vec2<Int>):ScreenPos { public inline function sub(rhs:Vec2<Int>):ScreenPos {
return new ScreenPos({ return this.sub(rhs);
y: this.y - rhs.y,
x: this.x - rhs.x,
});
} }
@:op(A * B) @:op(A * B)
public function multiply(rhs:Vec2<Int>):ScreenPos { public inline function multiply(rhs:Vec2<Int>):ScreenPos {
return new ScreenPos({ return this.multiply(rhs);
y: this.y * rhs.y,
x: this.x * rhs.x,
});
} }
@:op(-A) @:op(-A)
public function negate():ScreenPos { public inline function negate():ScreenPos {
return new ScreenPos({ return this.negate();
y: -this.y,
x: -this.x,
});
} }
} }

View File

@ -8,16 +8,96 @@ package lib;
this.x = x; this.x = x;
this.y = y; this.y = y;
} }
public function add(rhs:Vec2<T>):Vec2<T> {
return {
y: this.y + rhs.y,
x: this.x + rhs.x,
};
}
public function sub(rhs:Vec2<T>):Vec2<T> {
return {
y: this.y - rhs.y,
x: this.x - rhs.x,
};
}
public function multiply(rhs:Vec2<T>):Vec2<T> {
return {
y: this.y * rhs.y,
x: this.x * rhs.x,
};
}
public function negate():Vec2<T> {
return {
y: -this.y,
x: -this.x,
};
}
} }
@:structInit class Vec3<T:Float> { @:structInit class Vec3<T:Float> {
public var x:T; public final x:T;
public var y:T; public final y:T;
public var z:T; public final z:T;
public function new(x:T, y:T, z:T) { public function new(x:T, y:T, z:T) {
this.x = x; this.x = x;
this.y = y; this.y = y;
this.z = z; this.z = z;
} }
public function add(rhs:Vec3<T>):Vec3<T> {
return {
x: this.x + rhs.x,
y: this.y + rhs.y,
z: this.z + rhs.z
};
}
public function sub(rhs:Vec3<T>):Vec3<T> {
return {
x: this.x - rhs.x,
y: this.y - rhs.y,
z: this.z - rhs.z
};
}
public function multiplyScalar(rhs:T):Vec3<T> {
return {
x: this.x * rhs,
y: this.y * rhs,
z: this.z * rhs
};
}
public function negate():Vec3<T> {
return {
x: -this.x,
y: -this.y,
z: -this.z
};
}
public function dot(rhs:Vec3<T>):Float {
return this.x * rhs.x + this.y * rhs.y + this.z * rhs.z;
}
public function cross(rhs:Vec3<T>):Vec3<T> {
return {
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 length():Float {
return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
}
public function distance(rhs:Vec3<T>):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));
}
} }

View File

@ -7,78 +7,46 @@ import lib.Vec.Vec3;
Basicly a wrapper for Vec3<Float> with some extra functions. Basicly a wrapper for Vec3<Float> with some extra functions.
`Y` represents the height of the point. `Y` represents the height of the point.
**/ **/
@:forward(x, y, z) @:forward
abstract WorldPos(Vec3<Float>) from Vec3<Float> to Vec3<Float> { abstract WorldPos(Vec3<Float>) from Vec3<Float> to Vec3<Float> {
inline public function new(i:Vec3<Float>) { public inline function new(v:Vec3<Float>) {
this = i; this = v;
} }
@:op(A + B) @:op(A + B)
public function add(rhs:Vec3<Float>):WorldPos { public inline function add(rhs:WorldPos):WorldPos {
return new WorldPos({ return this.add(rhs);
y: this.y + rhs.y,
x: this.x + rhs.x,
z: this.z + rhs.z
});
} }
@:op(A - B) @:op(A - B)
public function sub(rhs:Vec3<Float>):WorldPos { public inline function sub(rhs:Vec3<Float>):WorldPos {
return new WorldPos({ return this.sub(rhs);
y: this.y - rhs.y,
x: this.x - rhs.x,
z: this.z - rhs.z
});
} }
@:op(A * B) @:op(A * B)
public function multiplyScalar(rhs:Float):WorldPos { public inline function multiplyScalar(rhs:Float):WorldPos {
return new WorldPos({ return this.multiplyScalar(rhs);
y: this.y * rhs,
x: this.x * rhs,
z: this.z * rhs
});
} }
@:op(A / B) @:op(A / B)
public function divideScalar(rhs:Float):WorldPos { public function divideScalar(rhs:Float):WorldPos {
return new WorldPos({ return {
y: this.y / rhs, y: this.y / rhs,
x: this.x / rhs, x: this.x / rhs,
z: this.z / rhs z: this.z / rhs
}); };
} }
@:op(-A) @:op(-A)
public function negate():WorldPos { public function negate():WorldPos {
return new WorldPos({ return this.negate();
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 { public function normalize():WorldPos {
var l = length(); var l = this.length();
return multiplyScalar(1 / l); 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) @:op(A == B)
public function equals(rhs:WorldPos):Bool { public function equals(rhs:WorldPos):Bool {
return close(rhs); return close(rhs);
@ -104,8 +72,4 @@ abstract WorldPos(Vec3<Float>) from Vec3<Float> to Vec3<Float> {
z: Math.fround(this.z) 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));
}
} }