瀏覽代碼

added RoundRect and testCol

Nicolas Cannasse 11 年之前
父節點
當前提交
fb455d3a5b
共有 4 個文件被更改,包括 194 次插入0 次删除
  1. 58 0
      h2d/col/RoundRect.hx
  2. 75 0
      samples/col/TestCol.hx
  3. 5 0
      samples/col/testCol.hxml
  4. 56 0
      samples/col/testCol.hxproj

+ 58 - 0
h2d/col/RoundRect.hx

@@ -0,0 +1,58 @@
+package h2d.col;
+
+class RoundRect {
+
+	var x : Float;
+	var y : Float;
+	var ray : Float;
+	var dx : Float;
+	var dy : Float;
+	var lenSq : Float;
+	var invLenSq : Float;
+	
+	public inline function new(x:Float,y:Float,rayW:Float,rayH:Float,rotation:Float) {
+		if( rayW < rayH ) {
+			var tmp = rayW;
+			rayW = rayH;
+			rayH = tmp;
+			rotation += Math.PI / 2;
+		}
+		this.ray = rayH;
+		var dx = (rayW - rayH) * Math.cos(rotation);
+		var dy = (rayW - rayH) * Math.sin(rotation);
+		this.x = x - dx;
+		this.y = y - dy;
+		this.dx = dx * 2;
+		this.dy = dy * 2;
+		lenSq = this.dx * this.dx + this.dy * this.dy;
+		invLenSq = 1 / lenSq;
+	}
+
+	// distance segment
+	public inline function distanceCenterSq( p : Point ) {
+		var px = p.x - x;
+		var py = p.y - y;
+		var t = px * dx + py * dy;
+		return if( t < 0 )
+			px * px + py * py;
+		else if( t > lenSq ) {
+			var kx = p.x - (x + dx);
+			var ky = p.y - (y + dy);
+			kx * kx + ky * ky;
+		} else {
+			var tl2 = t * invLenSq;
+			var pdx = x + tl2 * dx - p.x;
+			var pdy = y + tl2 * dy - p.y;
+			pdx * pdx + pdy * pdy;
+		}
+	}
+	
+	public inline function inside( p : Point ) {
+		return distanceCenterSq(p) - ray * ray < 0;
+	}
+
+	public inline function distance( p : Point ) {
+		return Math.sqrt(distanceCenterSq(p)) - ray;
+	}
+
+}

+ 75 - 0
samples/col/TestCol.hx

@@ -0,0 +1,75 @@
+class TestCol extends hxd.App {
+
+	static var RW = 100;
+	static var RH = 30;
+	
+	var rrect : h2d.Graphics;
+	
+	override function init() {
+		
+		var size = RW - RH;
+		var k = 10;
+		rrect = new h2d.Graphics(s2d);
+
+		rrect.beginFill(0xFFFFFFFF);
+		for( i in 0...k+1 ) {
+			var a = Math.PI * i / k - Math.PI / 2;
+			rrect.addPoint(size + RH * Math.cos(a), RH * Math.sin(a));
+		}
+		for( i in 0...k+1 ) {
+			var a = Math.PI * i / k + Math.PI / 2;
+			rrect.addPoint(-size + RH * Math.cos(a), RH * Math.sin(a));
+		}
+		rrect.endFill();
+		
+		rrect.x = s2d.width >> 1;
+		rrect.y = s2d.height >> 1;
+		rrect.rotation = Math.PI / 3;
+		rrect.color = new h3d.Vector();
+		
+		//var r = new h2d.col.RoundRect(rrect.x, rrect.y, RW, RH, rrect.rotation);
+		//mapCol( function(pt) return r.distance(pt) );
+	}
+	
+	function mapCol( dist : h2d.col.Point -> Float, scale = 1. ) {
+		var pt = new h2d.col.Point();
+		var bmp = new hxd.BitmapData(s2d.width, s2d.height);
+		for( x in 0...bmp.width )
+			for( y in 0...bmp.height ) {
+				pt.x = x + 0.5;
+				pt.y = y + 0.5;
+				var d = dist(pt);
+				if( d < 0 ) {
+					var c = Std.int( -d * scale * 4 + 0x20 );
+					if( c > 0xFF ) c = 0xFF;
+					bmp.setPixel(x, y, 0xFF000000 | (c << 16));
+				} else {
+					var c = Std.int( d * scale );
+					if( c > 0xFF ) c = 0xFF;
+					bmp.setPixel(x, y, 0xFF000000 | c);
+				}
+			}
+		var view = new h2d.Bitmap(h2d.Tile.fromBitmap(bmp));
+		view.alpha = 0.5;
+		s2d.addChildAt(view, 0);
+		bmp.dispose();
+	}
+	
+	override function update(dt:Float) {
+		var px = s2d.mouseX;
+		var py = s2d.mouseY;
+		var r = new h2d.col.RoundRect(rrect.x, rrect.y, RW, RH, rrect.rotation);
+		
+		
+		rrect.color.set(0, 0, 1);
+		
+		if( r.inside(new h2d.col.Point(px, py)) )
+			rrect.color.set(0, 1, 0);
+	}
+	
+	static function main() {
+		new TestCol();
+	}
+	
+}
+

+ 5 - 0
samples/col/testCol.hxml

@@ -0,0 +1,5 @@
+-swf col.swf
+-swf-header 800:600:60:FFFFFF
+-swf-version 9
+-main TestCol
+-lib h3d

+ 56 - 0
samples/col/testCol.hxproj

@@ -0,0 +1,56 @@
+<?xml version="1.0" encoding="utf-8"?>
+<project version="2">
+  <!-- Output SWF options -->
+  <output>
+    <movie outputType="Application" />
+    <movie input="" />
+    <movie path="col.swf" />
+    <movie fps="60" />
+    <movie width="800" />
+    <movie height="600" />
+    <movie version="9" />
+    <movie minorVersion="0" />
+    <movie platform="Flash Player" />
+    <movie background="#FFFFFF" />
+  </output>
+  <!-- Other classes to be compiled into your SWF -->
+  <classpaths>
+    <!-- example: <class path="..." /> -->
+  </classpaths>
+  <!-- Build options -->
+  <build>
+    <option directives="" />
+    <option flashStrict="False" />
+    <option mainClass="TestCol" />
+    <option enabledebug="False" />
+    <option additional="-lib h3d" />
+  </build>
+  <!-- haxelib libraries -->
+  <haxelib>
+    <!-- example: <library name="..." /> -->
+  </haxelib>
+  <!-- Class files to compile (other referenced classes will automatically be included) -->
+  <compileTargets>
+    <!-- example: <compile path="..." /> -->
+  </compileTargets>
+  <!-- Assets to embed into the output SWF -->
+  <library>
+    <!-- example: <asset path="..." id="..." update="..." glyphs="..." mode="..." place="..." sharepoint="..." /> -->
+  </library>
+  <!-- Paths to exclude from the Project Explorer tree -->
+  <hiddenPaths>
+    <hidden path="obj" />
+  </hiddenPaths>
+  <!-- Executed before build -->
+  <preBuildCommand />
+  <!-- Executed after build -->
+  <postBuildCommand alwaysRun="False" />
+  <!-- Other project options -->
+  <options>
+    <option showHiddenPaths="False" />
+    <option testMovie="Default" />
+    <option testMovieCommand="" />
+  </options>
+  <!-- Plugin storage -->
+  <storage />
+</project>