※現在、「wonderfl build flash online」求人コンテンツ制作に関してのアンケートを実施中です!みなさまのお力添えを頂いて、続々とアンケート結果が集まっていますが、まだまだ募集しております。ご協力のほど、どうぞよろしくお願いいたします!

wonderfl運営事務局
→アンケートページ(※ログインしてからお答えいただけるようになっています。)

 notice: Flash editor updated! Join the development! Thanks to MiniBuilder


FORKED
  1. // forked from sebleedelisle's flash on 2009-5-30
  2. package
  3. {
  4.    import flash.display.Graphics;
  5.    import flash.display.Sprite;
  6.    import flash.events.MouseEvent;
  7.    import flash.geom.Point;
  8.    import flash.geom.Rectangle;
  9.    import flash.text.TextField;
  10.    import flash.text.TextFormat;
  11.    import flash.utils.getTimer;
  12.    [SWF(frameRate="100", backgroundColor="0x000000", width="500", height="500")]
  13.    public class TriangleAABBSpeedTest extends Sprite
  14.    {
  15.       
  16.       
  17.       public var outputText : TextField;
  18.       
  19.       public function TriangleAABBSpeedTest()
  20.       {
  21.          super();
  22.          outputText = new TextField(); 
  23.          x = y = 250
  24.          outputText.defaultTextFormat = new TextFormat("Arial"); 
  25.          outputText.textColor = 0xffffff; 
  26.          outputText.text = "Click to start test"
  27.          outputText.x = outputText.y = -250
  28.          outputText.width = 500
  29.          
  30.          addChild(outputText); 
  31.          
  32.          stage.addEventListener(MouseEvent.MOUSE_DOWN, startTest); 
  33.          
  34.       }
  35.    
  36.       // I'm thinking that storing these values as properties rather than declaring them as local vars 
  37.       // should be faster...
  38.          
  39.       private  var x0 : Number
  40.       private  var y0 : Number
  41.       private  var x1 : Number
  42.       private  var y1 : Number
  43.       private  var x2 : Number
  44.       private  var y2 : Number
  45.       private  var l : Number
  46.       private  var r : Number
  47.       private  var t : Number
  48.       private  var b : Number
  49.       
  50.       private  var top_intersection:Number ;
  51.       private  var bottom_intersection : Number
  52.       private  var toptrianglepoint : Number
  53.       private  var bottomtrianglepoint : Number
  54.       
  55.       private  var m: Number;
  56.       private var c : Number   
  57.    
  58.    
  59.       // THESE ARE THE TWO FUNCTIONS YOU CAN OPTIMISE. 
  60.       // I realise that you could inline the lineRectangleIntersect method, but I'd rather not do that just yet... 
  61.       // I'm looking for maths/logic improvements. 
  62.    
  63.       public function triangleAABBIntersectionTest(rect:Rectangle,  vertex0:Point, vertex1:Point, vertex2:Point ) : Boolean
  64.       {
  65.          // YOU MUST LEAVE THESE DECLARATIONS; they simulate necessary data exchange within PV3D
  66.          l = rect.left; 
  67.          r = rect.right; 
  68.          t = rect.top; 
  69.          b = rect.bottom; 
  70.          
  71.          x0 = vertex0.x; 
  72.          y0 = vertex0.y; 
  73.          x1 = vertex1.x; 
  74.          y1 = vertex1.y; 
  75.          x2 = vertex2.x; 
  76.          y2 = vertex2.y; 
  77.          
  78.          
  79.          return lineRectangleIntersect(x0,y0,x1,y1) 
  80.                ||   lineRectangleIntersect(x1,y1,x2,y2) 
  81.                ||   lineRectangleIntersect(x2,y2,x0,y0);
  82.       
  83.       
  84.          
  85.       }
  86.       
  87.       
  88.       public function lineRectangleIntersect(x0: Number,y0: Number,x1: Number,y1: Number):Boolean //, left:Number, right:Number, top:Number, bottom:Number) : Boolean
  89.       {
  90.             
  91.             // Calculate m and c for the equation for the line (y = mx+c)
  92.             m = (y1-y0) / (x1-x0); 
  93.             c = y0 -(m*x0); 
  94.             // if the line is going up from right to left then the top intersect point is on the left
  95.             if(m>0)
  96.             {
  97.                top_intersection = (m*l  + c); 
  98.                bottom_intersection = (m*r  + c);
  99.             } 
  100.             // otherwise it's on the right
  101.             else 
  102.             {
  103.                top_intersection = (m*r  + c); 
  104.                bottom_intersection = (m*l  + c); 
  105.             }
  106.             
  107.             // work out the top and bottom extents for the triangle
  108.             if(y0<y1)
  109.             {
  110.                toptrianglepoint = y0; 
  111.                bottomtrianglepoint = y1; 
  112.             }
  113.             else
  114.             {
  115.                toptrianglepoint = y1; 
  116.                bottomtrianglepoint = y0; 
  117.             }
  118.             
  119.             var topoverlap : Number
  120.             var botoverlap : Number
  121.             
  122.             // and calculate the overlap between those two bounds
  123.             topoverlap = top_intersection>toptrianglepoint ? top_intersection : toptrianglepoint; 
  124.             botoverlap = bottom_intersection<bottomtrianglepoint ? bottom_intersection : bottomtrianglepoint; 
  125.             
  126.             // (topoverlap<botoverlap) : 
  127.             // if the intersection isn't the right way up then we have no overlap
  128.             
  129.             // (!((botoverlap<t) || (topoverlap>b)) :
  130.             // If the bottom overlap is higher than the top of the rectangle or the top overlap is 
  131.             // lower than the bottom of the rectangle we don't have intersection. So return the negative 
  132.             // of that. Much faster than checking each of the points is within the bounds of the rectangle. 
  133.             return (topoverlap<botoverlap) && (!((botoverlap<t) || (topoverlap>b)));
  134.          
  135.       }   
  136.    
  137.       public function startTest(e:MouseEvent = null) : void
  138.       {
  139.          var g : Graphics = graphics; 
  140.          g.clear(); 
  141.          var cullingRect : Rectangle = new Rectangle(-100,-100,200,200); 
  142.          
  143.          var iterations : int = 1
  144.          var timerIntersecting : int = 0 ; 
  145.          var timerNotIntersecting : int = 0 ; 
  146.                         var intersectCount : int = 0
  147.                         
  148.          var v0 : Point = new Point(); 
  149.          var v1 : Point = new Point(); 
  150.          var v2 : Point = new Point(); 
  151.          
  152.          var i : int = iterations; 
  153.          
  154.          while(--i>-1)
  155.          {
  156.              /*
  157.             v0.x = Math.random()*400 - 200; 
  158.             v0.y = Math.random()*400 - 200; 
  159.             v1.x = v0.x+ (Math.random()*300 -150); 
  160.             v1.y = v0.y+ (Math.random()*300 -150); 
  161.             v2.x = v0.x+ (Math.random()*300 -150); 
  162.             v2.y = v0.y+ (Math.random()*300 -150);             
  163.             */
  164.             
  165.             v0.x = -150
  166.             v0.y = 50
  167.             v1.x = 10
  168.             v1.y = -150;
  169.             v2.x = 150;
  170.             v2.y = 50
  171.             
  172.             var startTimer : int = getTimer(); 
  173.             var intersecting : Boolean = triangleAABBIntersectionTest(cullingRect, v0, v1, v2); 
  174.             if(intersecting)
  175.             {
  176.                timerIntersecting += (getTimer() - startTimer);
  177.                intersectCount++; 
  178.             }
  179.             else 
  180.             {
  181.                timerNotIntersecting += (getTimer() - startTimer);
  182.             }
  183.             if(i<300)
  184.             {
  185.                g.lineStyle(0,0x000000); 
  186.                g.beginFill(intersecting ? 0x006600 : 0x660000,0.5); 
  187.                g.moveTo(x0,y0); 
  188.                g.lineTo(x1,y1); 
  189.                g.lineTo(x2,y2); 
  190.                g.lineTo(x0,y0); 
  191.                g.endFill(); 
  192.             }
  193.          }
  194.          
  195.          g.lineStyle(0,0xffffff,0.5); 
  196.          g.drawRect(cullingRect.x, cullingRect.y, cullingRect.width, cullingRect.height); 
  197.          outputText.text = "Average time per triangle : "+(timerIntersecting+timerNotIntersecting)/iterations; 
  198.          outputText.appendText("\nAverage time per intersecting triangle : "+(timerIntersecting)/intersectCount); 
  199.          outputText.appendText("\nAverage time per non-intersecting triangle : "+(timerNotIntersecting)/(iterations-intersectCount)); 
  200.                   
  201.       }
  202.    
  203.    
  204.       
  205.       
  206.    }
  207. }
noswf
  1. // forked from sebleedelisle's flash on 2009-5-30
  2. package
  3. {
  4.    import flash.display.Graphics;
  5.    import flash.display.Sprite;
  6.    import flash.events.MouseEvent;
  7.    import flash.geom.Point;
  8.    import flash.geom.Rectangle;
  9.    import flash.text.TextField;
  10.    import flash.text.TextFormat;
  11.    import flash.utils.getTimer;
  12.    [SWF(frameRate="100", backgroundColor="0x000000", width="500", height="500")]
  13.    public class TriangleAABBSpeedTest extends Sprite
  14.    {
  15.       
  16.       
  17.       public var outputText : TextField;
  18.       
  19.       public function TriangleAABBSpeedTest()
  20.       {
  21.          super();
  22.          outputText = new TextField(); 
  23.          x = y = 250
  24.          outputText.defaultTextFormat = new TextFormat("Arial"); 
  25.          outputText.textColor = 0xffffff; 
  26.          outputText.text = "Click to start test"
  27.          outputText.x = outputText.y = -250
  28.          outputText.width = 500
  29.          
  30.          addChild(outputText); 
  31.          
  32.          stage.addEventListener(MouseEvent.MOUSE_DOWN, startTest); 
  33.          
  34.       }
  35.    
  36.       // I'm thinking that storing these values as properties rather than declaring them as local vars 
  37.       // should be faster...
  38.          
  39.       private  var x0 : Number
  40.       private  var y0 : Number
  41.       private  var x1 : Number
  42.       private  var y1 : Number
  43.       private  var x2 : Number
  44.       private  var y2 : Number
  45.       private  var l : Number
  46.       private  var r : Number
  47.       private  var t : Number
  48.       private  var b : Number
  49.       
  50.       private  var top_intersection:Number ;
  51.       private  var bottom_intersection : Number
  52.       private  var toptrianglepoint : Number
  53.       private  var bottomtrianglepoint : Number
  54.       
  55.       private  var m: Number;
  56.       private var c : Number   
  57.    
  58.    
  59.       // THESE ARE THE TWO FUNCTIONS YOU CAN OPTIMISE. 
  60.       // I realise that you could inline the lineRectangleIntersect method, but I'd rather not do that just yet... 
  61.       // I'm looking for maths/logic improvements. 
  62.    
  63.       public function triangleAABBIntersectionTest(rect:Rectangle,  vertex0:Point, vertex1:Point, vertex2:Point ) : Boolean
  64.       {
  65.          // YOU MUST LEAVE THESE DECLARATIONS; they simulate necessary data exchange within PV3D
  66.          l = rect.left; 
  67.          r = rect.right; 
  68.          t = rect.top; 
  69.          b = rect.bottom; 
  70.          
  71.          x0 = vertex0.x; 
  72.          y0 = vertex0.y; 
  73.          x1 = vertex1.x; 
  74.          y1 = vertex1.y; 
  75.          x2 = vertex2.x; 
  76.          y2 = vertex2.y; 
  77.          
  78.          
  79.          return lineRectangleIntersect(x0,y0,x1,y1) 
  80.                ||   lineRectangleIntersect(x1,y1,x2,y2) 
  81.                ||   lineRectangleIntersect(x2,y2,x0,y0);
  82.       
  83.       
  84.          
  85.       }
  86.       
  87.       
  88.       public function lineRectangleIntersect(x0: Number,y0: Number,x1: Number,y1: Number):Boolean //, left:Number, right:Number, top:Number, bottom:Number) : Boolean
  89.       {
  90.             
  91.             // Calculate m and c for the equation for the line (y = mx+c)
  92.             m = (y1-y0) / (x1-x0); 
  93.             c = y0 -(m*x0); 
  94.             // if the line is going up from right to left then the top intersect point is on the left
  95.             if(m>0)
  96.             {
  97.                top_intersection = (m*l  + c); 
  98.                bottom_intersection = (m*r  + c);
  99.             } 
  100.             // otherwise it's on the right
  101.             else 
  102.             {
  103.                top_intersection = (m*r  + c); 
  104.                bottom_intersection = (m*l  + c); 
  105.             }
  106.             
  107.             // work out the top and bottom extents for the triangle
  108.             if(y0<y1)
  109.             {
  110.                toptrianglepoint = y0; 
  111.                bottomtrianglepoint = y1; 
  112.             }
  113.             else
  114.             {
  115.                toptrianglepoint = y1; 
  116.                bottomtrianglepoint = y0; 
  117.             }
  118.             
  119.             var topoverlap : Number
  120.             var botoverlap : Number
  121.             
  122.             // and calculate the overlap between those two bounds
  123.             topoverlap = top_intersection>toptrianglepoint ? top_intersection : toptrianglepoint; 
  124.             botoverlap = bottom_intersection<bottomtrianglepoint ? bottom_intersection : bottomtrianglepoint; 
  125.             
  126.             // (topoverlap<botoverlap) : 
  127.             // if the intersection isn't the right way up then we have no overlap
  128.             
  129.             // (!((botoverlap<t) || (topoverlap>b)) :
  130.             // If the bottom overlap is higher than the top of the rectangle or the top overlap is 
  131.             // lower than the bottom of the rectangle we don't have intersection. So return the negative 
  132.             // of that. Much faster than checking each of the points is within the bounds of the rectangle. 
  133.             return (topoverlap<botoverlap) && (!((botoverlap<t) || (topoverlap>b)));
  134.          
  135.       }
  136.    
  137.    
  138.    
  139.       public function startTest(e:MouseEvent = null) : void
  140.       {
  141.          var g : Graphics = graphics; 
  142.          g.clear(); 
  143.          var cullingRect : Rectangle = new Rectangle(-100,-75,200,150); 
  144.          
  145.          var iterations : int = 100000
  146.          var timerIntersecting : int = 0 ; 
  147.          var timerNotIntersecting : int = 0 ; 
  148.                         var intersectCount : int = 0
  149.                         
  150.          var v0 : Point = new Point(); 
  151.          var v1 : Point = new Point(); 
  152.          var v2 : Point = new Point(); 
  153.          
  154.          var i : int = iterations; 
  155.          
  156.          while(--i>-1)
  157.          {
  158.             v0.x = Math.random()*400 - 200
  159.             v0.y = Math.random()*400 - 200
  160.             v1.x = v0.x+ (Math.random()*300 -150); 
  161.             v1.y = v0.y+ (Math.random()*300 -150); 
  162.             v2.x = v0.x+ (Math.random()*300 -150); 
  163.             v2.y = v0.y+ (Math.random()*300 -150);             
  164.             
  165.             var startTimer : int = getTimer(); 
  166.             var intersecting : Boolean = triangleAABBIntersectionTest(cullingRect, v0, v1, v2); 
  167.             if(intersecting)
  168.             {
  169.                timerIntersecting += (getTimer() - startTimer);
  170.                intersectCount++; 
  171.             }
  172.             else 
  173.             {
  174.                timerNotIntersecting += (getTimer() - startTimer);
  175.             }
  176.             if(i<300)
  177.             {
  178.                g.lineStyle(0,0x000000); 
  179.                g.beginFill(intersecting ? 0x006600 : 0x660000,0.5); 
  180.                g.moveTo(x0,y0); 
  181.                g.lineTo(x1,y1); 
  182.                g.lineTo(x2,y2); 
  183.                g.lineTo(x0,y0); 
  184.                g.endFill(); 
  185.             }
  186.          }
  187.          
  188.          g.lineStyle(0,0xffffff,0.5); 
  189.          g.drawRect(cullingRect.x, cullingRect.y, cullingRect.width, cullingRect.height); 
  190.          outputText.text = "Average time per triangle : "+(timerIntersecting+timerNotIntersecting)/iterations; 
  191.          outputText.appendText("\nAverage time per intersecting triangle : "+(timerIntersecting)/intersectCount); 
  192.          outputText.appendText("\nAverage time per non-intersecting triangle : "+(timerNotIntersecting)/(iterations-intersectCount)); 
  193.                   
  194.       }
  195.    
  196.    
  197.       
  198.       
  199.    }
  200. }
noswf
  1. // forked from sebleedelisle's flash on 2009-5-30
  2. package
  3. {
  4.    import flash.display.Graphics;
  5.    import flash.display.Sprite;
  6.    import flash.events.MouseEvent;
  7.    import flash.geom.Point;
  8.    import flash.geom.Rectangle;
  9.    import flash.text.TextField;
  10.    import flash.text.TextFormat;
  11.    import flash.utils.getTimer;
  12.    [SWF(frameRate="100", backgroundColor="0x000000", width="500", height="500")]
  13.    public class TriangleAABBSpeedTest extends Sprite
  14.    {
  15.       
  16.       
  17.       public var outputText : TextField;
  18.       
  19.       public function TriangleAABBSpeedTest()
  20.       {
  21.          super();
  22.          outputText = new TextField(); 
  23.          x = y = 250
  24.          outputText.defaultTextFormat = new TextFormat("Arial"); 
  25.          outputText.textColor = 0xffffff; 
  26.          outputText.text = "Click to start test"
  27.          outputText.x = outputText.y = -250
  28.          outputText.width = 500
  29.          
  30.          addChild(outputText); 
  31.          
  32.          stage.addEventListener(MouseEvent.MOUSE_DOWN, startTest); 
  33.          
  34.       }
  35.    
  36.       // I'm thinking that storing these values as properties rather than declaring them as local vars 
  37.       // should be faster...
  38.          
  39.       private  var x0 : Number
  40.       private  var y0 : Number
  41.       private  var x1 : Number
  42.       private  var y1 : Number
  43.       private  var x2 : Number
  44.       private  var y2 : Number
  45.       private  var l : Number
  46.       private  var r : Number
  47.       private  var t : Number
  48.       private  var b : Number
  49.       
  50.       private  var top_intersection:Number ;
  51.       private  var bottom_intersection : Number
  52.       private  var toptrianglepoint : Number
  53.       private  var bottomtrianglepoint : Number
  54.       
  55.       private  var m: Number;
  56.       private var c : Number   
  57.    
  58.    
  59.       // THESE ARE THE TWO FUNCTIONS YOU CAN OPTIMISE. 
  60.       // I realise that you could inline the lineRectangleIntersect method, but I'd rather not do that just yet... 
  61.       // I'm looking for maths/logic improvements. 
  62.    
  63.       public function triangleAABBIntersectionTest(rect:Rectangle,  vertex0:Point, vertex1:Point, vertex2:Point ) : Boolean
  64.       {
  65.          // YOU MUST LEAVE THESE DECLARATIONS; they simulate necessary data exchange within PV3D
  66.          l = rect.left; 
  67.          r = rect.right; 
  68.          t = rect.top; 
  69.          b = rect.bottom; 
  70.          
  71.          x0 = vertex0.x; 
  72.          y0 = vertex0.y; 
  73.          x1 = vertex1.x; 
  74.          y1 = vertex1.y; 
  75.          x2 = vertex2.x; 
  76.          y2 = vertex2.y; 
  77.          
  78.          //if all points of the triangle are on one side of the rect 
  79.          if((l>x0 && l>x1 && l>x2)
  80.          || (r<x0 && r<x1 && r<x2)
  81.          || (t>y0 && t>y1 && t>y2)
  82.          || (b<y0 && b<y1 && b<y2)
  83.          ){
  84.              return false;
  85.          }
  86.          //if there is a point of the triangle inside the rect
  87.          if((l<x0 && x0<r && t<y0 && y0<b)
  88.          || (l<x1 && x1<r && t<y1 && y1<b)
  89.          || (l<x2 && x2<r && t<y2 && y2<b)
  90.          ){
  91.              return true;
  92.          }
  93.          
  94.          return lineRectangleIntersect(x0,y0,x1,y1) 
  95.                ||   lineRectangleIntersect(x1,y1,x2,y2) 
  96.                ||   lineRectangleIntersect(x2,y2,x0,y0);
  97.       
  98.       
  99.          
  100.       }
  101.       
  102.       
  103.       public function lineRectangleIntersect(x0: Number,y0: Number,x1: Number,y1: Number):Boolean //, left:Number, right:Number, top:Number, bottom:Number) : Boolean
  104.       {
  105.             
  106.             // Calculate m and c for the equation for the line (y = mx+c)
  107.             m = (y1-y0) / (x1-x0); 
  108.             c = y0 -(m*x0); 
  109.             // if the line is going up from right to left then the top intersect point is on the left
  110.             if(m>0)
  111.             {
  112.                top_intersection = (m*l  + c); 
  113.                bottom_intersection = (m*r  + c);
  114.             } 
  115.             // otherwise it's on the right
  116.             else 
  117.             {
  118.                top_intersection = (m*r  + c); 
  119.                bottom_intersection = (m*l  + c); 
  120.             }
  121.             
  122.             // work out the top and bottom extents for the triangle
  123.             if(y0<y1)
  124.             {
  125.                toptrianglepoint = y0; 
  126.                bottomtrianglepoint = y1; 
  127.             }
  128.             else
  129.             {
  130.                toptrianglepoint = y1; 
  131.                bottomtrianglepoint = y0; 
  132.             }
  133.             
  134.             var topoverlap : Number
  135.             var botoverlap : Number
  136.             
  137.             // and calculate the overlap between those two bounds
  138.             topoverlap = top_intersection>toptrianglepoint ? top_intersection : toptrianglepoint; 
  139.             botoverlap = bottom_intersection<bottomtrianglepoint ? bottom_intersection : bottomtrianglepoint; 
  140.             
  141.             // (topoverlap<botoverlap) : 
  142.             // if the intersection isn't the right way up then we have no overlap
  143.             
  144.             // (!((botoverlap<t) || (topoverlap>b)) :
  145.             // If the bottom overlap is higher than the top of the rectangle or the top overlap is 
  146.             // lower than the bottom of the rectangle we don't have intersection. So return the negative 
  147.             // of that. Much faster than checking each of the points is within the bounds of the rectangle. 
  148.             return (topoverlap<botoverlap) && (!((botoverlap<t) || (topoverlap>b)));
  149.          
  150.       }
  151.    
  152.    
  153.    
  154.       public function startTest(e:MouseEvent = null) : void
  155.       {
  156.          var g : Graphics = graphics; 
  157.          g.clear(); 
  158.          var cullingRect : Rectangle = new Rectangle(-100,-75,200,150); 
  159.          
  160.          var iterations : int = 100000
  161.          var timerIntersecting : int = 0 ; 
  162.          var timerNotIntersecting : int = 0 ; 
  163.                         var intersectCount : int = 0
  164.                         
  165.          var v0 : Point = new Point(); 
  166.          var v1 : Point = new Point(); 
  167.          var v2 : Point = new Point(); 
  168.          
  169.          var i : int = iterations; 
  170.          
  171.          while(--i>-1)
  172.          {
  173.             v0.x = Math.random()*400 - 200
  174.             v0.y = Math.random()*400 - 200
  175.             v1.x = v0.x+ (Math.random()*300 -150); 
  176.             v1.y = v0.y+ (Math.random()*300 -150); 
  177.             v2.x = v0.x+ (Math.random()*300 -150); 
  178.             v2.y = v0.y+ (Math.random()*300 -150);             
  179.             
  180.             var startTimer : int = getTimer(); 
  181.             var intersecting : Boolean = triangleAABBIntersectionTest(cullingRect, v0, v1, v2); 
  182.             if(intersecting)
  183.             {
  184.                timerIntersecting += (getTimer() - startTimer);
  185.                intersectCount++; 
  186.             }
  187.             else 
  188.             {
  189.                timerNotIntersecting += (getTimer() - startTimer);
  190.             }
  191.             if(i<300)
  192.             {
  193.                g.lineStyle(0,0x000000); 
  194.                g.beginFill(intersecting ? 0x006600 : 0x660000,0.5); 
  195.                g.moveTo(x0,y0); 
  196.                g.lineTo(x1,y1); 
  197.                g.lineTo(x2,y2); 
  198.                g.lineTo(x0,y0); 
  199.                g.endFill(); 
  200.             }
  201.          }
  202.          
  203.          g.lineStyle(0,0xffffff,0.5); 
  204.          g.drawRect(cullingRect.x, cullingRect.y, cullingRect.width, cullingRect.height); 
  205.          outputText.text = "Average time per triangle : "+(timerIntersecting+timerNotIntersecting)/iterations; 
  206.          outputText.appendText("\nAverage time per intersecting triangle : "+(timerIntersecting)/intersectCount); 
  207.          outputText.appendText("\nAverage time per non-intersecting triangle : "+(timerNotIntersecting)/(iterations-intersectCount)); 
  208.                   
  209.       }
  210.    
  211.    
  212.       
  213.       
  214.    }
  215. }
noswf
  1. package
  2. {
  3.    import flash.display.Graphics;
  4.    import flash.display.Sprite;
  5.    import flash.events.MouseEvent;
  6.    import flash.geom.Point;
  7.    import flash.geom.Rectangle;
  8.    import flash.text.TextField;
  9.    import flash.text.TextFormat;
  10.    import flash.utils.getTimer;
  11.    [SWF(frameRate="100", backgroundColor="0x000000", width="500", height="500")]
  12.  
  13.    public class TriangleSpeedTest2 extends Sprite
  14.    {
  15.  
  16.  
  17.       public var outputText : TextField;
  18.  
  19.       public function TriangleSpeedTest2()
  20.       {
  21.          super();
  22.          outputText = new TextField(); 
  23.          x = y = 250
  24.          outputText.defaultTextFormat = new TextFormat("Arial"); 
  25.          outputText.textColor = 0xffffff; 
  26.          outputText.text = "Click to start test"
  27.          outputText.x = outputText.y = -250
  28.          outputText.width = 500
  29.  
  30.          addChild(outputText); 
  31.  
  32.          stage.addEventListener(MouseEvent.MOUSE_DOWN, startTest); 
  33.  
  34.       }
  35.  
  36.       // I'm thinking that storing these values as properties rather than declaring them as local vars 
  37.       // should be faster...
  38.  
  39.       private  var x0 : Number
  40.       private  var y0 : Number
  41.       private  var x1 : Number
  42.       private  var y1 : Number
  43.       private  var x2 : Number
  44.       private  var y2 : Number
  45.       private  var l : Number
  46.       private  var r : Number
  47.       private  var t : Number
  48.       private  var b : Number
  49.       
  50.       private var t0 : int;
  51.       private var t1 : int;
  52.       private var t2 : int;
  53.       
  54.       private var s : Number
  55.       
  56.        private var lm0 : Number
  57.       private var lm1 : Number
  58.       private var lm2 : Number
  59.       
  60.        private var rm0 : Number
  61.       private var rm1 : Number
  62.       private var rm2 : Number
  63.       
  64.       private var b0    :int;
  65.       private var b1    :int;
  66.       private var b2    :int;
  67.       
  68.        private var i0    :int;
  69.       private var i1    :int;
  70.       private var i2    :int;
  71.       
  72.       private  var top_intersection:Number ;
  73.       private  var bottom_intersection : Number
  74.       private  var toptrianglepoint : Number
  75.       private  var bottomtrianglepoint : Number
  76.  
  77.        private  var m: Number;
  78.       private var c : Number  
  79.       private  var m0: Number;
  80.       private var c0 : Number  
  81.       private  var m1: Number;
  82.       private var c1 : Number  
  83.       private  var m2: Number;
  84.       private var c2 : Number  
  85.  
  86.  
  87.       // THESE ARE THE TWO FUNCTIONS YOU CAN OPTIMISE. 
  88.       // I realise that you could inline the lineRectangleIntersect method, but I'd rather not do that just yet... 
  89.       // I'm looking for maths/logic improvements. 
  90.  
  91.       public function triangleTest(rect:Rectangle,  vertex0:Point, vertex1:Point, vertex2:Point ) : Boolean
  92.       {
  93.          // YOU MUST LEAVE THESE DECLARATIONS; they simulate necessary data exchange within PV3D
  94.          l = rect.left; 
  95.          r = rect.right; 
  96.          t = rect.top; 
  97.          b = rect.bottom; 
  98.  
  99.          x0 = vertex0.x; 
  100.          y0 = vertex0.y; 
  101.          x1 = vertex1.x; 
  102.          y1 = vertex1.y; 
  103.          x2 = vertex2.x; 
  104.          y2 = vertex2.y; 
  105.          
  106.         
  107.          
  108.          b0 =  int(x0 > l) | int(y0 > t) << 1 | int (x0 > r) << 2 | int (y0 > b) << 3;
  109.          if (b0 == 3return true;
  110.          b1 =  int(x1 > l) | int(y1 > t) << 1 | int (x1 > r) << 2 | int (y1 > b) << 3;
  111.          if (b1 == 3return true;
  112.          b2 =  int(x2 > l) | int(y2 > t) << 1 | int (x2 > r) << 2 | int (y2 > b) << 3;
  113.          if (b2 == 3return true;
  114.          
  115.          
  116.         
  117.         i0 = b0 ^ b1
  118.         if (i0 != 0)
  119.         {
  120.             m = (y1-y0) / (x1-x0); 
  121.             c = y0 -(m * x0);
  122.             if (Boolean(i0 & 1)) { s = m * l + c; if ( s > t && s < b) return true; }
  123.             if (Boolean(i0 & 2)) { s = (t - c) / m; if ( s > l && s < r) return true; }
  124.             if (Boolean(i0 & 4)) { s = m * r + c; if ( s > t && s < b) return true; }
  125.             if (Boolean(i0 & 8)) { s = (b - c) / m; if ( s > l && s < r) return true; }
  126.         }
  127.         
  128.         i1 = b1 ^ b2
  129.         if (i1 != 0)
  130.         {
  131.             m = (y2-y1) / (x2-x1); 
  132.             c = y1 -(m * x1);
  133.             if (Boolean(i1 & 1)) { s = m * l + c; if ( s > t && s < b) return true; }
  134.             if (Boolean(i1 & 2)) { s = (t - c) / m; if ( s > l && s < r) return true; }
  135.             if (Boolean(i1 & 4)) { s = m * r + c; if ( s > t && s < b) return true; }
  136.             if (Boolean(i1 & 8)) { s = (b - c) / m; if ( s > l && s < r) return true; }
  137.         }
  138.         
  139.         i2 = b0 ^ b2
  140.         if (i2 != 0)
  141.         {
  142.             m = (y2-y0) / (x2-x0); 
  143.             c = y0 -(m * x0);
  144.             if (Boolean(i2 & 1)) { s = m * l + c; if ( s > t && s < b) return true; }
  145.             if (Boolean(i2 & 2)) { s = (t - c) / m; if ( s > l && s < r) return true; }
  146.             if (Boolean(i2 & 4)) { s = m * r + c; if ( s > t && s < b) return true; }
  147.             if (Boolean(i2 & 8)) { s = (b - c) / m; if ( s > l && s < r) return true; }
  148.         }
  149.         
  150.         return false;
  151.       }
  152.        
  153.       public function startTest(e:MouseEvent = null) : void
  154.       {
  155.          var g : Graphics = graphics; 
  156.          g.clear(); 
  157.          var cullingRect : Rectangle = new Rectangle(-100,-75,200,150); 
  158.  
  159.          var iterations : int = 100000
  160.          var timerIntersecting : int = 0 ; 
  161.          var timerNotIntersecting : int = 0 ; 
  162.                         var intersectCount : int = 0
  163.  
  164.          var v0 : Point = new Point(); 
  165.          var v1 : Point = new Point(); 
  166.          var v2 : Point = new Point(); 
  167.  
  168.          var i : int = iterations; 
  169.  
  170.          while(--i>-1)
  171.          {
  172.             v0.x = Math.random()*400 - 200
  173.             v0.y = Math.random()*400 - 200
  174.             v1.x = v0.x+ (Math.random()*300 -150); 
  175.             v1.y = v0.y+ (Math.random()*300 -150); 
  176.             v2.x = v0.x+ (Math.random()*300 -150); 
  177.             v2.y = v0.y+ (Math.random()*300 -150);             
  178.  
  179.             var startTimer : int = getTimer(); 
  180.             var intersecting : Boolean = triangleTest(cullingRect, v0, v1, v2); 
  181.             if(intersecting)
  182.             {
  183.                timerIntersecting += (getTimer() - startTimer);
  184.                intersectCount++; 
  185.             }
  186.             else 
  187.             {
  188.                timerNotIntersecting += (getTimer() - startTimer);
  189.             }
  190.             if(i<300)
  191.             {
  192.                g.lineStyle(0,0x000000); 
  193.                g.beginFill(intersecting ? 0x006600 : 0x660000,0.5); 
  194.                g.moveTo(x0,y0); 
  195.                g.lineTo(x1,y1); 
  196.                g.lineTo(x2,y2); 
  197.                g.lineTo(x0,y0); 
  198.                g.endFill(); 
  199.             }
  200.          }
  201.  
  202.          g.lineStyle(0,0xffffff,0.5); 
  203.          g.drawRect(cullingRect.x, cullingRect.y, cullingRect.width, cullingRect.height); 
  204.          outputText.text = "Average time per triangle : "+(timerIntersecting+timerNotIntersecting)/iterations; 
  205.          outputText.appendText("\nAverage time per intersecting triangle : "+(timerIntersecting)/intersectCount); 
  206.          outputText.appendText("\nAverage time per non-intersecting triangle : "+(timerNotIntersecting)/(iterations-intersectCount)); 
  207.  
  208.       }
  209.  
  210.    }
  211. }
noswf
  1. package
  2. {
  3.    import flash.display.Graphics;
  4.    import flash.display.Sprite;
  5.    import flash.events.MouseEvent;
  6.    import flash.geom.Point;
  7.    import flash.geom.Rectangle;
  8.    import flash.text.TextField;
  9.    import flash.text.TextFormat;
  10.    import flash.utils.getTimer;
  11.    [SWF(frameRate="100", backgroundColor="0x000000", width="500", height="500")]
  12.  
  13.    public class TriangleSpeedTest extends Sprite
  14.    {
  15.  
  16.  
  17.       public var outputText : TextField;
  18.  
  19.       public function TriangleSpeedTest()
  20.       {
  21.          super();
  22.          outputText = new TextField(); 
  23.          x = y = 250
  24.          outputText.defaultTextFormat = new TextFormat("Arial"); 
  25.          outputText.textColor = 0xffffff; 
  26.          outputText.text = "Click to start test"
  27.          outputText.x = outputText.y = -250
  28.          outputText.width = 500
  29.  
  30.          addChild(outputText); 
  31.  
  32.          stage.addEventListener(MouseEvent.MOUSE_DOWN, startTest); 
  33.  
  34.       }
  35.  
  36.       // I'm thinking that storing these values as properties rather than declaring them as local vars 
  37.       // should be faster...
  38.  
  39.       private  var x0 : Number
  40.       private  var y0 : Number
  41.       private  var x1 : Number
  42.       private  var y1 : Number
  43.       private  var x2 : Number
  44.       private  var y2 : Number
  45.       private  var l : Number
  46.       private  var r : Number
  47.       private  var t : Number
  48.       private  var b : Number
  49.       
  50.       private var t0 : int;
  51.       private var t1 : int;
  52.       private var t2 : int;
  53.       
  54.       private var lm0 : Number
  55.       private var lm1 : Number
  56.       private var lm2 : Number
  57.        private var rm0 : Number
  58.       private var rm1 : Number
  59.       private var rm2 : Number
  60.       
  61.       private  var top_intersection:Number ;
  62.       private  var bottom_intersection : Number
  63.       private  var toptrianglepoint : Number
  64.       private  var bottomtrianglepoint : Number
  65.  
  66.       private  var m0: Number;
  67.       private var c0 : Number  
  68.       private  var m1: Number;
  69.       private var c1 : Number  
  70.       private  var m2: Number;
  71.       private var c2 : Number  
  72.  
  73.  
  74.       // THESE ARE THE TWO FUNCTIONS YOU CAN OPTIMISE. 
  75.       // I realise that you could inline the lineRectangleIntersect method, but I'd rather not do that just yet... 
  76.       // I'm looking for maths/logic improvements. 
  77.  
  78.       public function triangleTest(rect:Rectangle,  vertex0:Point, vertex1:Point, vertex2:Point ) : Boolean
  79.       {
  80.          // YOU MUST LEAVE THESE DECLARATIONS; they simulate necessary data exchange within PV3D
  81.          l = rect.left; 
  82.          r = rect.right; 
  83.          t = rect.top; 
  84.          b = rect.bottom; 
  85.  
  86.          x0 = vertex0.x; 
  87.          y0 = vertex0.y; 
  88.          x1 = vertex1.x; 
  89.          y1 = vertex1.y; 
  90.          x2 = vertex2.x; 
  91.          y2 = vertex2.y; 
  92.          
  93.          if (x0 > l && x0 < r && y0 > t && y0 < b) return true;
  94.          if (x1 > l && x1 < r && y1 > t && y1 < b) return true;
  95.          if (x2 > l && x2 < r && y2 > t && y2 < b) return true;
  96.          
  97.          return isInsideTriangle(x0, y0, x1, y1, x2, y2);
  98.       }
  99.  
  100.       public function isInsideTriangle(x0: Number,y0: Number,x1: Number,y1: Number,x2: Number,y2: Number):Boolean
  101.       { 
  102.             // Calculate m and c for the equation for the line (y = mx+c)
  103.             m0 = (y2-y1) / (x2-x1); 
  104.             c0 = y2 -(m0 * x2);
  105.             
  106.             m1 = (y2-y0) / (x2-x0); 
  107.             c1 = y0 -(m1 * x0);
  108.             
  109.             m2 = (y1-y0) / (x1-x0); 
  110.             c2 = y0 -(m2 * x0);
  111.             
  112.             t0 = int(x0 * m0 + c0 > y0);
  113.             t1 = int(x1 * m1 + c1 > y1);
  114.             t2 = int(x2 * m2 + c2 > y2);
  115.             
  116.             lm0 = l * m0 + c0;
  117.             lm1 = l * m1 + c1;
  118.             lm2 = l * m2 + c2;
  119.             
  120.             rm0 = r * m0 + c0;
  121.             rm1 = r * m1 + c1;
  122.             rm2 = r * m2 + c2;
  123.             
  124.             if ( !(t0 ^ int( lm0 > t)) && !(t1 ^ int(lm1 > t)) && !(t2 ^ int(lm2 > t))) return true;
  125.             
  126.             if (!(t0 ^ int( lm0 > b)) && !(t1 ^ int(lm1 > b)) && !(t2 ^ int(lm2 > b))) return true;
  127.             
  128.             if ( !(t0 ^ int( rm0 > t)) && !(t1 ^ int(rm1 > t)) && !(t2 ^ int(rm2 > t))) return true;
  129.             
  130.             if (!(t0 ^ int( rm0 > b)) && !(t1 ^ int(rm1 > b)) && !(t2 ^ int(rm2 > b))) return true;
  131.             
  132.             return false;
  133.  
  134.       }
  135.  
  136.       public function startTest(e:MouseEvent = null) : void
  137.       {
  138.          var g : Graphics = graphics; 
  139.          g.clear(); 
  140.          var cullingRect : Rectangle = new Rectangle(-100,-75,200,150); 
  141.  
  142.          var iterations : int = 100000
  143.          var timerIntersecting : int = 0 ; 
  144.          var timerNotIntersecting : int = 0 ; 
  145.                         var intersectCount : int = 0
  146.  
  147.          var v0 : Point = new Point(); 
  148.          var v1 : Point = new Point(); 
  149.          var v2 : Point = new Point(); 
  150.  
  151.          var i : int = iterations; 
  152.  
  153.          while(--i>-1)
  154.          {
  155.             v0.x = Math.random()*400 - 200
  156.             v0.y = Math.random()*400 - 200
  157.             v1.x = v0.x+ (Math.random()*300 -150); 
  158.             v1.y = v0.y+ (Math.random()*300 -150); 
  159.             v2.x = v0.x+ (Math.random()*300 -150); 
  160.             v2.y = v0.y+ (Math.random()*300 -150);             
  161.  
  162.             var startTimer : int = getTimer(); 
  163.             var intersecting : Boolean = triangleTest(cullingRect, v0, v1, v2); 
  164.             if(intersecting)
  165.             {
  166.                timerIntersecting += (getTimer() - startTimer);
  167.                intersectCount++; 
  168.             }
  169.             else 
  170.             {
  171.                timerNotIntersecting += (getTimer() - startTimer);
  172.             }
  173.             if(i<300)
  174.             {
  175.                g.lineStyle(0,0x000000); 
  176.                g.beginFill(intersecting ? 0x006600 : 0x660000,0.5); 
  177.                g.moveTo(x0,y0); 
  178.                g.lineTo(x1,y1); 
  179.                g.lineTo(x2,y2); 
  180.                g.lineTo(x0,y0); 
  181.                g.endFill(); 
  182.             }
  183.          }
  184.  
  185.          g.lineStyle(0,0xffffff,0.5); 
  186.          g.drawRect(cullingRect.x, cullingRect.y, cullingRect.width, cullingRect.height); 
  187.          outputText.text = "Average time per triangle : "+(timerIntersecting+timerNotIntersecting)/iterations; 
  188.          outputText.appendText("\nAverage time per intersecting triangle : "+(timerIntersecting)/intersectCount); 
  189.          outputText.appendText("\nAverage time per non-intersecting triangle : "+(timerNotIntersecting)/(iterations-intersectCount)); 
  190.  
  191.       }
  192.  
  193.    }
  194. }
  195. /*
  196. package  
  197. {
  198.     import flash.geom.Point;
  199.     
  200.     public class test 
  201.     {
  202.         
  203.         public function test() 
  204.         {
  205.             
  206.         }
  207.         
  208.         public function isSameSide(p1:Point, p2:Point, p3:Point, pI:Point):Boolean
  209.         {
  210.             var a:Number = (p2.y - p1.y) / (p2.x - p1.x);
  211.             var b:Number = p1.y - a * p1.x;            
  212.             return (int((p3.x * a + b) > 0) ^ int((pI.x * a + b) > 0))
  213.         }
  214.         
  215.         public function isInside(
  216.         
  217.     }
  218.     
  219. }*/
noswf
Get Adobe Flash Player