Created
February 15, 2014 15:38
-
-
Save slaskis/9020959 to your computer and use it in GitHub Desktop.
A quick test with abstract types in haxe. This is really cool
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abstract AVector(Array<Float>) { | |
public var x(get, never):Float; | |
public var y(get, never):Float; | |
public var z(get, never):Float; | |
public var length(get, never):Float; | |
public var lengthSq(get, never):Float; | |
public inline function new(x:Float, y:Float, z:Float=0.) this = [x, y, z]; | |
@:op(A+B) public static inline function add(a : AVector, b : AVector) : AVector { | |
return new AVector(a.x+b.x,a.y+b.y,a.z+b.z); | |
} | |
@:op(A-B) public static inline function sub(a : AVector, b : AVector) : AVector { | |
return new AVector(a.x-b.x,a.y-b.y,a.z-b.z); | |
} | |
@:op(A*B) public static inline function mulVector(a : AVector, b : AVector) : AVector { | |
return new AVector(a.x*b.x,a.y*b.y,a.z*b.z); | |
} | |
@:op(A*B) public static inline function mulInteger(a : AVector, b : Int) : AVector { | |
return new AVector(a.x*b,a.y*b,a.z*b); | |
} | |
inline function get_x():Float return this[0]; | |
inline function get_y():Float return this[1]; | |
inline function get_z():Float return this[2]; | |
function get_lengthSq():Float return this[0]*this[0] + this[1]*this[1] + this[2]*this[2]; | |
function get_length():Float return Math.sqrt(this[0]*this[0] + this[1]*this[1] + this[2]*this[2]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment