浏览代码

Add Matrix.rotate, Matrix.init*, Polygon.transform (#328)

trethaller 7 年之前
父节点
当前提交
0a7252c94e
共有 2 个文件被更改,包括 35 次插入0 次删除
  1. 29 0
      h2d/col/Matrix.hx
  2. 6 0
      h2d/col/Polygon.hx

+ 29 - 0
h2d/col/Matrix.hx

@@ -6,6 +6,8 @@ import hxd.Math;
 **/
 class Matrix {
 
+	static var tmp = new Matrix();
+
 	public var a : Float;
 	public var b : Float;
 	public var c : Float;
@@ -22,6 +24,28 @@ class Matrix {
 		x = 0; y = 0;
 	}
 
+	public inline function initTranslate(x, y) {
+		a = 1; b = 0; c = 0; d = 1;
+		this.x = x;
+		this.y = y;
+	}
+
+	public inline function initScale(sx, sy) {
+		a = sx; b = 0; c = 0; d = sy;
+		x = 0; y = 0;
+	}
+
+	public inline function initRotate(angle) {
+		var cos = Math.cos(angle);
+		var sin = Math.sin(angle);
+		a = cos;
+		b = sin;
+		c = -sin;
+		d = cos;
+		x = 0;
+		y = 0;
+	}
+
 	public function invert() {
 		inverse(this);
 	}
@@ -77,6 +101,11 @@ class Matrix {
 		y *= sy;
 	}
 
+	public function rotate(angle: Float) {
+		tmp.initRotate(angle);
+		multiply(this, tmp);
+	}
+
 	public function toString() {
 		return "MAT=[\n" +
 			"  [ " + Math.fmt(a) + ", " + Math.fmt(b) + " ]\n" +

+ 6 - 0
h2d/col/Polygon.hx

@@ -149,6 +149,12 @@ abstract Polygon(Array<Point>) from Array<Point> to Array<Point> {
 		this.reverse();
 	}
 
+	public function transform(mat: h2d.col.Matrix) {
+		for( i in 0...points.length ) {
+			points[i] = mat.transform(points[i]);
+		}
+	}
+
 	@:noDebug
 	public function contains( p : Point, isConvex = false ) {
 		if( isConvex ) {