Category Archives: Math

Closest Point on a Line

Actionscript:
  1. // from
  2. // http://www.gamedev.net/topic/444154-closest-point-on-a-line/
  3.  
  4. function getClosestPoint(A:*, B:*, P:*, segmentClamp:Boolean=true):Point {
  5.     var AP:Point = new Point(P.x - A.x, P.y - A.y),
  6.         AB:Point = new Point(B.x - A.x, B.y - A.y);
  7.     var ab2:Number=AB.x*AB.x+AB.y*AB.y;
  8.     var ap_ab:Number=AP.x*AB.x+AP.y*AB.y;
  9.     var t:Number=ap_ab/ab2;
  10.     if (segmentClamp) {
  11.         if (t<0.0) {
  12.             t=0.0;
  13.         } else if (t> 1.0) {
  14.             t=1.0;
  15.         }
  16.  
  17.     }
  18.     return new Point(A.x + AB.x * t, A.y + AB.y * t);
  19. }
  20.  
  21.  
  22. var point:Sprite = Sprite(addChild(new Sprite()));
  23. point.x = 50;
  24. point.y = 50;
  25. with(point.graphics) beginFill(0), drawCircle(0,0, 3);
  26.  
  27. var line:MovieClip = MovieClip(addChild(new MovieClip()));
  28. line.a = new Point(20, 100);
  29. line.b = new Point(300, 60);
  30. with(line.graphics) lineStyle(0), moveTo(line.a.x, line.a.y), lineTo(line.b.x, line.b.y);
  31.  
  32. var closestPoint:Point = getClosestPoint(line.a, line.b, point);
  33.  
  34. var closest:Sprite = Sprite(addChild(new Sprite()));
  35. closest.x = closestPoint.x;
  36. closest.y = closestPoint.y;
  37. with(closest.graphics) beginFill(0xFF0000), drawCircle(0,0, 3);

Posted in Math | Leave a comment

Fish Curve

Actionscript:
  1. var xp:Number = 0;
  2. var yp:Number = 0;
  3. var t:Number = 0;
  4. var a:Number = 200;
  5. x = stage.stageWidth / 2;
  6. y = stage.stageHeight / 2;
  7.  
  8. graphics.lineStyle(0,0x000000);
  9. addEventListener(Event.ENTER_FRAME, onRun);
  10. function onRun(evt:Event):void {
  11.     xp = a * Math.cos(t) - (a * Math.pow(Math.sin(t),2))/Math.sqrt(2);
  12.     yp = a * Math.cos(t) * Math.sin(t);
  13.     if (t == 0){
  14.       graphics.moveTo(xp, yp);
  15.     }else{
  16.       graphics.lineTo(xp, yp);
  17.     }
  18.     t += 0.05;
  19. }

While surfing mathworld I stumbled upon the equation for something called the Fish Curve. This snippet will draw something like this:

Also posted in misc | Tagged , , | Leave a comment

Astroid Pedal Curve Variation

Actionscript:
  1. var xp:Number = 0;
  2. var yp:Number = 0;
  3. var t:Number = 0;
  4. var r:Number = 200;
  5. x = stage.stageWidth / 2;
  6. y = stage.stageHeight / 2;
  7.  
  8. graphics.lineStyle(0,0x000000);
  9. addEventListener(Event.ENTER_FRAME, onRun);
  10. function onRun(evt:Event):void {
  11.     r = 200 * Math.cos(t / 10);
  12.     xp = r * Math.pow(Math.cos(t), 3);
  13.     yp = r * Math.pow(Math.sin(t), 3);
  14.     if (t == 0){
  15.       graphics.moveTo(xp, yp);
  16.     }else{
  17.       graphics.lineTo(xp, yp);
  18.     }
  19.     t += 0.1;
  20. }

While browsing mathworld I decided to do a variation on this curve . The above snippet will draw something like this:

Also posted in misc | Tagged , , | Leave a comment

Logistic Map Play

Actionscript:
  1. var offX:Number = 100;
  2. var offY:Number = 300;
  3. var scalarX:Number = 6;
  4. var scalarY:Number = 200;
  5. addEventListener(Event.ENTER_FRAME, onLoop);
  6. function onLoop(evt:Event):void{
  7.     var r:Number = mouseY / 100;
  8.     var xn:Number = (mouseX - 100) / 650;
  9.     var xn1:Number = 0;
  10.     graphics.clear();
  11.     graphics.lineStyle(0,0);
  12.     for (var i:int = 0; i<100; i++){
  13.       xn1 = r * xn * (1 - xn);
  14.       xn = xn1;
  15.       if (i == 0){
  16.         graphics.moveTo(offX+i*scalarX,offY+xn1*scalarY);  
  17.       }else{
  18.         graphics.lineTo(offX+i*scalarX, offY+xn1*scalarY);
  19.       }
  20.     }
  21. }

Whenever I can't decide what kind of snippet to make, I simply go to wikipedia or mathworld for a bit and I always end up with something. I've messed with Logistic Maps before (when learning about strange attractors). This is a simple rendering where the x and y axis change the biotic potential (r) and the starting value for x.

Here is the link I used for reference.


Have a look at the swf (just move your mouse around).

Also posted in Graphics, misc | Tagged , , | 2 Comments