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

flash on 2015-2-12

line (axisaligned) rectangle intersection point
Get Adobe Flash player
by mutantleg 11 Feb 2015
/**
 * Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/5uSS
 */

package {
    import flash.events.MouseEvent;
    import flash.text.TextField;
    import flash.events.Event;
    import flash.display.Sprite;
    public class FlashTest extends Sprite {
        public function FlashTest() {
            
          deb = new TextField();
          deb.width = 320;   deb.height = 240;  
          deb.mouseEnabled = false;
          addChild(deb);  
            

          stage.addEventListener(MouseEvent.CLICK, onClick);            
          stage.addEventListener(Event.ENTER_FRAME, onEnter);  
        }//ctor
        
        public function onClick(e:MouseEvent):void
        {
          cx = Math.random()*465; cy= Math.random()*465;
          rx = Math.random()*365; ry= Math.random()*365;
          rw = Math.random()*128+32;   
          rh = Math.random()*128+32;                
        }//onclick
        
        public var cx:Number = 235;
        public var cy:Number = 235;
        
        public var rx:Number = 40;
        public var ry:Number = 30;
        public var rw:Number = 100;
        public var rh:Number = 100;
        
        public var deb:TextField;
        
        public function onEnter(e:Event):void
        {
           graphics.clear();
           graphics.lineStyle(2, 0);   
            
           var mx:Number; var my:Number;
           mx = stage.mouseX;
           my = stage.mouseY; 
            
           graphics.drawRect(rx, ry, rw, rh);
           
           graphics.lineStyle(1,0);
           graphics.moveTo(cx, cy);
           graphics.lineTo(mx, my); 
            
           var t:Number;
           t = rectInter(cx,cy, mx,my, rx,ry, rw,rh);
            
           deb.text = ""+t; 
          
           if (t > 1) { return; }  
           
           var ax:Number; var ay:Number;
           ax = cx +(mx-cx)*t;
           ay = cy +(my-cy)*t;
           
           graphics.lineStyle(2,0);
           graphics.drawCircle(ax,ay, 8); 
            
           graphics.lineStyle(4,0);
           graphics.moveTo(cx,cy);
           graphics.lineTo(ax,ay); 
            
            
        }//onenter
        
        public function rectInter(x0:Number, y0:Number, x1:Number, y1:Number,
         ax:Number, ay:Number, aw:Number, ah:Number):Number
         {
           var t:Number; var ret:Number;   ret = 999.0   
           t = segInter(x0,y0,x1,y1,  ax,ay, ax+aw, ay); if (t < ret) { ret = t;}
           t = segInter(x0,y0,x1,y1,  ax,ay+ah, ax+aw, ay+ah); if (t < ret) { ret = t;}
           t = segInter(x0,y0,x1,y1,  ax,ay, ax, ay+ah); if (t < ret) { ret = t;}
           t = segInter(x0,y0,x1,y1,  ax+aw,ay, ax+aw, ay+ah); if (t < ret) { ret = t;}
           return ret;
         }//rectinter
        
                
         public function segInter(x0:Number, y0:Number, x1:Number, y1:Number,
          x2:Number, y2:Number, x3:Number, y3:Number ):Number
          {   var ua:Number;    var ub:Number;  var bot:Number;
            bot = ((y3-y2)*(x1-x0))-((x3-x2)*(y1-y0));
            if (bot == 0) { return 999;}
            ua = (((x3-x2)*(y0-y2))-((y3-y2)*(x0-x2))) / bot;
            ub = (((x1-x0)*(y0-y2))-((y1-y0)*(x0-x2))) / bot;
            if (ua > 1 || ua < 0 || ub > 1 || ub < 0) { return 999; }          
            return ua;
          }//lineline 
          
    }//classend
}