improved Pos3 with math functions
This commit is contained in:
		
							parent
							
								
									da78641aec
								
							
						
					
					
						commit
						7fe52b1a8a
					
				@ -3,18 +3,18 @@ package lib;
 | 
			
		||||
import lib.Vec.Vec3;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
	Reporesents a Point in a 3D `Int` System.
 | 
			
		||||
	Basicly a wrapper for Vec3<Int> with some extra functions.
 | 
			
		||||
	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 Pos3(Vec3<Int>) from Vec3<Int> to Vec3<Int>{
 | 
			
		||||
	inline public function new(i:Vec3<Int>) {
 | 
			
		||||
abstract Pos3(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<Int>):Pos3 {
 | 
			
		||||
	public function add(rhs: Vec3<Float>):Pos3 {
 | 
			
		||||
		return new Pos3({
 | 
			
		||||
			y: this.y + rhs.y,
 | 
			
		||||
			x: this.x + rhs.x,
 | 
			
		||||
@ -23,7 +23,7 @@ abstract Pos3(Vec3<Int>) from Vec3<Int> to Vec3<Int>{
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@:op(A - B)
 | 
			
		||||
	public function sub(rhs: Vec3<Int>):Pos3 {
 | 
			
		||||
	public function sub(rhs: Vec3<Float>):Pos3 {
 | 
			
		||||
		return new Pos3({
 | 
			
		||||
			y: this.y - rhs.y,
 | 
			
		||||
			x: this.x - rhs.x,
 | 
			
		||||
@ -32,11 +32,20 @@ abstract Pos3(Vec3<Int>) from Vec3<Int> to Vec3<Int>{
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@:op(A * B)
 | 
			
		||||
	public function multiply(rhs: Vec3<Int>): Pos3 {
 | 
			
		||||
	public function multiplyScalar(rhs: Float): Pos3 {
 | 
			
		||||
		return new Pos3({
 | 
			
		||||
			y: this.y * rhs.y,
 | 
			
		||||
			x: this.x * rhs.x,
 | 
			
		||||
			z: this.z * rhs.z
 | 
			
		||||
			y: this.y * rhs,
 | 
			
		||||
			x: this.x * rhs,
 | 
			
		||||
			z: this.z * rhs
 | 
			
		||||
		});
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@:op(A / B)
 | 
			
		||||
	public function divideScalar(rhs: Float): Pos3 {
 | 
			
		||||
		return new Pos3({
 | 
			
		||||
			y: this.y / rhs,
 | 
			
		||||
			x: this.x / rhs,
 | 
			
		||||
			z: this.z / rhs
 | 
			
		||||
		});
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@ -48,4 +57,43 @@ abstract Pos3(Vec3<Int>) from Vec3<Int> to Vec3<Int>{
 | 
			
		||||
			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>):Pos3 {
 | 
			
		||||
		return new Pos3({
 | 
			
		||||
			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():Pos3 {
 | 
			
		||||
		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:Pos3):Bool {
 | 
			
		||||
		return close(rhs);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@:op(A != B)
 | 
			
		||||
	public function notEquals(rhs:Pos3):Bool {
 | 
			
		||||
		return !close(rhs);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public function close(rhs:Pos3, 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 'Pos3(${this.x}, ${this.y}, ${this.z})';
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user