Monthly Archives: May 2009

Point in Polygon

Actionscript:
  1. // function found here:
  2. // http://www.alienryderflex.com/polygon/
  3. function pointInPolygon(x:Number, y:Number, polyX:Array, polyY:Array):Boolean{
  4.     var j:int = polyX.length - 1;
  5.     var oddNodes:Boolean = false;
  6.     for (var i:int=0; i <polyX.length; i++) {
  7.     if (polyY[i] <y && polyY[j]>= y ||  polyY[j] <y && polyY[i]>= y) {
  8.  
  9.       if (polyX[i] + (y - polyY[i]) / (polyY[j] - polyY[i]) * (polyX[j] - polyX[i]) <x) {
  10.         oddNodes = !oddNodes;
  11.       }
  12.     }
  13.     j = i;
  14.   }
  15.   return  oddNodes;
  16. }
  17.  
  18. // draw an overly complex poly and store all  x y coords
  19. var pX:Array= new Array();
  20. var pY:Array = new Array();
  21. graphics.beginFill(0xCC0000);
  22. for (var i:int = 0; i<60; i++){
  23.     pX[i] = Math.random()*stage.stageWidth;
  24.     pY[i] =Math.random()*stage.stageHeight;
  25.     if (i == 0){
  26.         graphics.moveTo(pX[i], pY[i]);
  27.     }else{
  28.         graphics.lineTo(pX[i], pY[i]);
  29.     }
  30. }
  31. addEventListener(Event.ENTER_FRAME, onLoop);
  32. function onLoop(evt:Event):void {
  33.        alpha = 1;
  34.        if (pointInPolygon(mouseX, mouseY, pX, pY)){
  35.             alpha = .5;
  36.        }
  37. }

This snippet shows how to test to see if a point is inside a polygon. This may not really be useful as hitTestPoint() is likely faster - however this could be modified for other purposes such as polygon to polygon collision. This algorithm works nicely on all kinds of polygons ... convex, concave etc...

I found this algorithm in an article by Darel Rex Finley ... here.

I've used this function in Processing a few times...

Posted in graphics algorithms, misc | Tagged , | 2 Comments

Multiton

Actionscript:
  1. package {
  2.  
  3.     public class Multiton {
  4.        
  5.         public var test:String = "";
  6.        
  7.         private static  var _okToCreate:Boolean;
  8.         private static  var _instances:Object = new Object();
  9.         private static var _instanceNum:int = 0;
  10.  
  11.         public function Multiton() {
  12.             if (_okToCreate) {
  13.                 _instanceNum++;
  14.                 trace("created instance: " +_instanceNum);
  15.             } else {
  16.                 throw new Error(this + " is a Multiton... you must use the getInstance() method to instantiate it.");
  17.             }
  18.         }
  19.         public static function getInstance(key:String="default"):Multiton {
  20.             if (!_instances[key]) {
  21.                 _okToCreate = true;
  22.                 _instances[key] = new Multiton();
  23.                 _okToCreate = false;
  24.             }
  25.             return _instances[key];
  26.         }
  27.     }
  28. }

Holding off on the follow up to yesterdays post because I haven't finished it.... instead, here's a Multiton - Just like a Singleton but with multiple instances. You use it like this:

Actionscript:
  1. var m0:Multiton = Multiton.getInstance("one");
  2. m0.test = "i am instance one";
  3.  
  4. var m1:Multiton = Multiton.getInstance("two");
  5. m1.test = "i am instance two";
  6.  
  7. // elsewhere in the app
  8.  
  9. var multiton:Multiton = Multiton.getInstance("one");
  10. trace(multiton.test);
  11.  
  12. trace(Multiton.getInstance("two").test);

I've used this a few times and I seem to recall the Pure MVC makes use of this pattern (could be wrong about that though).

Posted in OOP | Tagged , | Leave a comment

Private Square Bracket Syntax

Actionscript:
  1. package {
  2.    
  3.     import flash.display.Sprite
  4.    
  5.     public class Test extends Sprite {
  6.        
  7.         private var _test:String = "private square brace syntax";
  8.         public function Test(){
  9.             trace(this["_test"]);
  10.         }
  11.     }
  12. }

It's important to note that square bracket syntax works with private vars... tomorrow I'll post a class I wrote that makes use of this technique to preserve encapsulation in an interesting way...

(auto-post didn't post this one yesterday for some reason... weird wordpress bug maybe?)

Posted in Uncategorized | Tagged , | 1 Comment

Hill Climbing

Actionscript:
  1. var target:Array = ("actionsnippet").split("");
  2. var leng:int=target.length;
  3. var iterations:int = 0;
  4.  
  5. var alphabet:Array = ("abcdefghijklmnopqrstuvwxyz").split("");
  6. var search:Array = randomString();
  7. var indices:Array = new Array();
  8. for (var i:int = 0; i<leng; i++) indices.push(i);
  9.  
  10. addEventListener(Event.ENTER_FRAME, onLoop);
  11. function onLoop(evt:Event):void {
  12.     for (var i:int = 0; i<10; i++){
  13.         if (indices.length> 0){
  14.            var ii:int = int(Math.random()*indices.length);
  15.            var index:int = indices[ii];
  16.          
  17.            search[index] = randomChar();
  18.            
  19.            if (search[index] == target[index]){
  20.                indices.splice(ii,1);  
  21.            }
  22.            trace(search);
  23.            iterations++;
  24.         }else{
  25.             trace("found after "+iterations+" iterations");
  26.             removeEventListener(Event.ENTER_FRAME, onLoop);
  27.             break;
  28.         }
  29.     }
  30. }
  31. function randomChar():String { return alphabet[int(Math.random()*alphabet.length)]; };
  32. function randomString():Array {
  33.     var str:Array = new Array();
  34.     for (var i:int = 0; i<leng; i++) {
  35.         str.push(randomChar());
  36.     }
  37.     return str;
  38. }

A little random hill climbing...

Here are the last few iterations... lowest number of iterations I noticed was round 250...

....
j,c,t,i,o,n,s,n,i,p,p,e,t
y,c,t,i,o,n,s,n,i,p,p,e,t
s,c,t,i,o,n,s,n,i,p,p,e,t
w,c,t,i,o,n,s,n,i,p,p,e,t
e,c,t,i,o,n,s,n,i,p,p,e,t
j,c,t,i,o,n,s,n,i,p,p,e,t
z,c,t,i,o,n,s,n,i,p,p,e,t
l,c,t,i,o,n,s,n,i,p,p,e,t
f,c,t,i,o,n,s,n,i,p,p,e,t
a,c,t,i,o,n,s,n,i,p,p,e,t
found after 361 iterations

Posted in arrays, misc, strings | Tagged , | 5 Comments