In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

Closest point between point and rectangle

Move circle with the mouse
/**
 * Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/ymN6
 */

package {
    import flash.events.Event;
    import flash.display.Sprite;
    public class FlashTest extends Sprite {
        
        public function FlashTest() {
            // write as3 code here..
        
            addEventListener(Event.ENTER_FRAME, onEnter);    
        }//ctor
        
        public var myRect:cRect = new cRect();
        public var myCirc:cCirc = new cCirc();
        
        public function onEnter(e:Event):void
        {
             myCirc.cx = this.mouseX;
             myCirc.cy = this.mouseY;           
            
            
            graphics.clear();
            graphics.lineStyle(2,0);
            
            graphics.drawRect(myRect.cx - myRect.xrad, myRect.cy-myRect.yrad, myRect.xrad*2, myRect.yrad*2);
            graphics.drawRect(myRect.cx-4,myRect.cy-4, 8,8);
            
            graphics.drawCircle(myCirc.cx, myCirc.cy, myCirc.rad);
              graphics.drawRect(myCirc.cx-4,myCirc.cy-4, 8,8);
              
             graphics.lineStyle(1,0);
             
            testClosest(myCirc, myRect);
        }//onenter
        
        public function testClosest(a:cCirc, b:cRect):void
        {
            
            var ix:Number;
            var iy:Number;
            
            if (a.cx < b.cx-b.xrad) { ix = b.cx -b.xrad;}
            else if (a.cx > b.cx+b.xrad) { ix = b.cx+b.xrad;}
            else { ix = a.cx; }
            
            if (a.cy < b.cy-b.yrad) { iy = b.cy -b.yrad;}
            else if (a.cy > b.cy+b.yrad) { iy = b.cy+b.yrad;}
            else { iy = a.cy; }
            
            graphics.drawCircle(ix, iy, 5);
            
            
            var dx:Number;
            var dy:Number;
            var mag:Number;
            
            dx = a.cx - ix;
            dy = a.cy - iy;
            mag = Math.sqrt(dx*dx + dy*dy);
            
            if (mag  > a.rad) return;
            if (mag == 0) { mag = 0.000001; }
            
            dx /= mag;
            dy /= mag;
            
            var mx:Number;
            var my:Number;
            
            mx = a.cx + dx * (a.rad - mag);
            my = a.cy + dy * (a.rad - mag);
            
            graphics.lineStyle(1, 0xFFFF0000);
            graphics.drawCircle(mx, my, a.rad);
            
        }//testclosest
        
        
    }//classend
}//package

internal class cCirc
{
    public var cx:Number = 20;
    public var cy:Number = 20;
    public var rad:Number = 16;
    
    public function cCirc() {}//ctor
    
}//classend

internal class cRect
{
    public var cx:Number = 260;
    public var cy:Number = 160;
    
    public var xrad:Number = 96;
    public var yrad:Number = 32;
    
    public function cRect() {}//ctor
    
}//classend