Page 1 of 1

Alguien que sepa AS3 puede traducir esto?

PostPosted: Wed Oct 10, 2012 12:35 am
by erkosone
Source Code (Text) [ Download ] [ Hide ]
  • private function insidePolygon(pointList:Array, p:Point):Boolean
  • {
  •    var counter:int = 0;
  •    var i:int;
  •    var xinters:Number;
  •    var p1:PointTest;
  •    var p2:PointTest;
  •    var n:int = pointList.length;
  •      
  •    p1 = pointList[0];
  •    for (i = 1; i <= n; i++)
  •    {
  •       p2 = pointList[i % n];
  •       if (p.y > Math.min(p1.y, p2.y))
  •       {
  •          if (p.y <= Math.max(p1.y, p2.y))
  •          {
  •             if (p.x <= Math.max(p1.x, p2.x))
  •             {
  •                if (p1.y != p2.y) {
  •                   xinters = (p.y - p1.y) * (p2.x - p1.x) / (p2.y - p1.y) + p1.x;
  •                   if (p1.x == p2.x || p.x <= xinters)
  •                      counter++;
  •                }
  •             }
  •          }
  •       }
  •       p1 = p2;
  •    }
  •    if (counter % 2 == 0)
  •    {
  •       return(false);
  •    }
  •    else
  •    {
  •       return(true);
  •    }
  • }


Está en flash o algo así.. pero no termino de entender algunas partes, el código parece interesante por lo simple que es, permite detectar si un punto está dentro de un polígono.
source: http://www.cristalab.com/tutoriales/det ... 3-c86779l/

Re: Alguien que sepa AS3 puede traducir esto?

PostPosted: Wed Oct 10, 2012 10:31 am
by GINO
Que parte no entiendes exactamente. Ese código lo que hace es recorrer la lista de lados del poligono y ver cuantos de ellos interseccionan con un hipotético rayo horizontal que parte del punto. Si el número de lados que interseccionan es impar, el punto está dentro, si es par, fuera.

Re: Alguien que sepa AS3 puede traducir esto?

PostPosted: Wed Oct 10, 2012 10:50 am
by BigHead
y es actionScript 3, te pego el resto de codigo por si te es últil

polygonTest.as

Code: Select all
package 
{
   import flash.display.Graphics;
   import flash.display.Sprite;
   import flash.events.Event;
   import flash.events.MouseEvent;
   import flash.geom.Point;
   /**
    * ...

    */
   public class PolygonTest extends Sprite
   {
      private var _pointList:Array;
      private var _lineContainer:Sprite;
      
      public function PolygonTest()
      {
         super();
         _pointList = new Array();
         _lineContainer = new Sprite();
         addChild(_lineContainer);
         createPoints();
         
         
         addEventListener(Event.ENTER_FRAME, enterFrameHandler);
      }
      
      private function enterFrameHandler(e:Event):void
      {
         var mp:Point = new Point(parent.mouseX, parent.mouseY);
         drawLines();
         //trace(insidePolygon(_pointList, mp));
         status.text = (insidePolygon(_pointList, mp)) ? "Inside" : "Outside";
      }
      
      private function createPoints():void
      {
         var aPoints:Array = new Array( { x:100, y:100 }, { x:400, y:100 }, { x:400, y:250 }, { x:100, y:250 } );
         var p:PointTest;
         for each(var i:Object in aPoints)
         {
            p = new PointTest();
            p.x = i.x;
            p.y = i.y;
            _pointList.push(p);
            addChild(p);
         }
      }
      
      private function drawLines():void
      {
         var g:Graphics = _lineContainer.graphics;
         g.clear();
         g.lineStyle(1, 0xff0000);
         g.moveTo(_pointList[0].x, _pointList[0].y);
         for each(var p:PointTest in _pointList)
         {
            g.lineTo(p.x, p.y);
         }
         g.lineTo(_pointList[0].x, _pointList[0].y);
      }
      
      
      private function insidePolygon(pointList:Array, p:Point):Boolean
      {
         var counter:int = 0;
         var i:int;
         var xinters:Number;
         var p1:PointTest;
         var p2:PointTest;
         var n:int = pointList.length;
         
         p1 = pointList[0];
         for (i = 1; i <= n; i++)
         {
            p2 = pointList[i % n];
            if (p.y > Math.min(p1.y, p2.y))
            {
               if (p.y <= Math.max(p1.y, p2.y))
               {
                  if (p.x <= Math.max(p1.x, p2.x))
                  {
                     if (p1.y != p2.y) {
                        xinters = (p.y - p1.y) * (p2.x - p1.x) / (p2.y - p1.y) + p1.x;
                        if (p1.x == p2.x || p.x <= xinters)
                           counter++;
                     }
                  }
               }
            }
            p1 = p2;
         }
         if (counter % 2 == 0)
         {
            return(false);
         }
         else
         {
            return(true);
         }
      }
      
      
   }

}




Esto sólo hace la llamada a las funciones e inicializa

PointTest.as
Code: Select all

package 
{
   import flash.display.Sprite;
   import flash.events.Event;
   import flash.events.MouseEvent;
   /**
    * ...

    */
   public class PointTest extends Sprite
   {
      
      public function PointTest()
      {
         super();
         
         graphics.beginFill(0x555500);
         graphics.drawCircle(0, 0, 10);
         graphics.endFill();
         
         addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
      }
      
      private function mouseDownHandler(e:MouseEvent):void
      {
         addEventListener(Event.ENTER_FRAME, enterFrameHandler);
         
         stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
      }
      
      private function mouseUpHandler(e:MouseEvent):void
      {
         removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
         stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
      }
      
      private function enterFrameHandler(e:Event):void
      {
         x = parent.mouseX;
         y = parent.mouseY;
      }
      
   }

}

Espero te sirva de algo, saludos


Re: Alguien que sepa AS3 puede traducir esto?

PostPosted: Wed Oct 10, 2012 11:49 am
by erkosone
creo que estaba saturado ayer, hoy lo veo mas claro, gracias igualmente, si tengo alguna duda volveré a comentar.