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 between point and rotating rectangle forked from: Closest point between point and rectangle

Addon: rotating rectangle
/**
 * Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/duGc
 */

// forked from mutantleg's Closest point between point and rectangle
package {
    import flash.events.Event;
    import flash.display.Sprite;
    public class FlashTest extends Sprite {
        
        public function FlashTest() {
            // write as3 code here..
        
            rectSprite = new Sprite();
            rectSprite.graphics.clear();
            rectSprite.graphics.lineStyle(2,0);
            rectSprite.graphics.drawRect( -myRect.xrad, -myRect.yrad, myRect.xrad*2, myRect.yrad*2);
            rectSprite.graphics.drawRect(-4,-4, 8,8);
                addChild(rectSprite);
        
            addEventListener(Event.ENTER_FRAME, onEnter);    
        }//ctor
        
        public var myRect:cRect = new cRect();
        public var myCirc:cCirc = new cCirc();
        
        public var rectSprite:Sprite;
        
        public function onEnter(e:Event):void
        {
            
             myRect.ang += 0.01;
            
             myCirc.cx = this.mouseX;
             myCirc.cy = this.mouseY;           
            
            
            graphics.clear();
            graphics.lineStyle(2,0);
            
            rectSprite.x = myRect.cx;
            rectSprite.y = myRect.cy;
            rectSprite.rotationZ = myRect.ang * (180 / 3.1415); //convert rad to degrees
           // 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;
            var rx:Number;
            var ry:Number;
            var kx:Number;
            var ky:Number;
            var sina:Number;
            var cosa:Number;
            
            
            //convert the coordinate system of the circle to the coord system of the rectangle
            
            rx = a.cx - b.cx;
            ry = a.cy - b.cy;
            
            sina = Math.sin(-b.ang);
            cosa = Math.cos(-b.ang);
            
            kx = cosa * rx - sina * ry;
            ky = sina * rx + cosa * ry;
            
           // graphics.drawCircle(kx, ky, 5);
            
            //find closest point to the transformed point
            
            if (kx < -b.xrad) { ix = -b.xrad;}
            else if (kx > b.xrad) { ix = b.xrad;}
            else { ix = kx; }
            
            if (ky < -b.yrad) { iy = -b.yrad;}
            else if (ky > b.yrad) { iy = b.yrad;}
            else { iy = ky; }
            
            //convert the closest point back to the circle coordinates
            
           
            
            rx = ix;
            ry = iy;
            
             sina = Math.sin(b.ang);
            cosa = Math.cos(b.ang);
            
             kx = cosa * rx - sina * ry;
             ky = sina * rx + cosa * ry;
            
            ix = kx + b.cx;
            iy = ky + b.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 var ang:Number = 0;
    
    public function cRect() {}//ctor
    
}//classend