Skip to content

Instantly share code, notes, and snippets.

@tbrosman
Created July 10, 2014 05:27
Show Gist options
  • Save tbrosman/4b87085c0ae198129370 to your computer and use it in GitHub Desktop.
Save tbrosman/4b87085c0ae198129370 to your computer and use it in GitHub Desktop.
uppercase XY shape hack
package vtgmath;
typedef Vector2Shape =
{
#if use_uppercase_xy
public var X:Float;
public var Y:Float;
#else
public var x:Float;
public var y:Float;
#end
};
#if !use_uppercase_xy
@:forward(x, y)
#end
abstract Vector2(Vector2Shape) from Vector2Shape to Vector2Shape
{
#if use_uppercase_xy
public var x(get, set):Float;
public var y(get, set):Float;
#end
public function new(x:Float, y:Float)
{
#if use_uppercase_xy
this = {X: x, Y: y};
#else
this = {x: x, y: y};
#end
}
@:op(A + B)
public static inline function add(a:Vector2, b:Vector2):Vector2
{
return new Vector2(a.x + b.x, a.y + b.y);
}
@:op(A * B)
public static inline function dot(a:Vector2, b:Vector2):Float
{
return a.x * b.x + a.y * b.y;
}
@:op(A * B)
public static inline function multiply(s:Float, a:Vector2):Vector2
{
return new Vector2(s * a.x, s * a.y);
}
#if use_uppercase_xy
private inline function get_x()
{
return this.X;
}
private inline function set_x(x:Float)
{
return this.X = x;
}
private inline function get_y()
{
return this.Y;
}
private inline function set_y(y:Float)
{
return this.Y = y;
}
#end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment