在表格中动态呈现数据的原理

比如一些股票证券的曲线走势曲线 我们在用flex开发的时候需要获得曲线点上各个地方的信息

下面是最基本的模型 是用两线碰撞实现的! 最后显示的是坐标 !我们在具体的项目中可以将该坐标映射成具体的数据

 

demo : http://3556.8b8b.info/swf/flex.html

 

用到了碰撞类: 

 

 

// hitter     myTarget   是两条线

// hitter     myTarget   是两条线

import utils.HitTest;
import flash.events.Event;
import flash.geom.Rectangle;

this.addEventListener(Event.ENTER_FRAME,onFrame,false,1,true);

function onFrame(e:Event):void{
 hitter.x=mouseX-hitter.width/2;
 hitter.y=mouseY-hitter.height/2;
 
 if(HitTest.complexHitTestObject(hitter,myTarget)){
  
  
  var rect:Rectangle=HitTest.complexIntersectionRectangle(myTarget,hitter);
  
  showText.text="true!"+"/n X="+rect.x+"/nY="+rect.y;
  this.graphics.clear();
  this.graphics.lineStyle(1,0x336699);
  
  this.graphics.moveTo(rect.x,0);
  this.graphics.lineTo(rect.x,600);
  
  
  }if(!HitTest.complexHitTestObject(hitter,myTarget)){
   showText.text="false!"
   this.graphics.clear();
   
   }
 
 }

 

 

碰撞类:

 

/*
方法说明  :
  http://www.tink.ws/blog/as-30-hittest/
 
 
  一般我只要用 ComplexHitTestObject()就可以了
调用  : HitTest.ComplexHitTestObject(db1,db2);//判断是否发生碰撞  返回布尔值


*/

 

package utils{

 import flash.display.BitmapData;
 import flash.display.BlendMode;
 import flash.display.DisplayObject;
 import flash.display.Sprite;

 import flash.geom.ColorTransform;
 import flash.geom.Matrix;
 import flash.geom.Point;
 import flash.geom.Rectangle;

 public class HitTest {
  //下面这个方法常用


  public static function complexHitTestObject(target1:DisplayObject,target2:DisplayObject,accurracy:Number=1):Boolean {
   return complexIntersectionRectangle(target1,target2,accurracy).width!=0;
  }

 

  public static function intersectionRectangle(target1:DisplayObject,target2:DisplayObject):Rectangle {
   // If either of the items don't have a reference to stage, then they are not in a display list
   // or if a simple hitTestObject is false, they cannot be intersecting.
   if (! target1.root||! target2.root||! target1.hitTestObject(target2)) {
    return new Rectangle  ;
   }

   // Get the bounds of each DisplayObject.
   var bounds1:Rectangle=target1.getBounds(target1.root);
   var bounds2:Rectangle=target2.getBounds(target2.root);

   // Determine test area boundaries.
   var intersection:Rectangle=new Rectangle  ;
   intersection.x=Math.max(bounds1.x,bounds2.x);//X坐标
   intersection.y=Math.max(bounds1.y,bounds2.y);//y坐标 ========= 这两个信息很重要
   intersection.width=Math.min(bounds1.x+bounds1.width-intersection.x,bounds2.x+bounds2.width-intersection.x);
   intersection.height=Math.min(bounds1.y+bounds1.height-intersection.y,bounds2.y+bounds2.height-intersection.y);

   return intersection;
  }

  public static function complexIntersectionRectangle(target1:DisplayObject,target2:DisplayObject,accurracy:Number=1):Rectangle {

   if (accurracy<=0) {

    throw new Error("ArgumentError: Error #5001: Invalid value for accurracy",5001);

   }

   // If a simple hitTestObject is false, they cannot be intersecting.


   if (! target1.hitTestObject(target2)) {
    return new Rectangle  ;
   }

   var hitRectangle:Rectangle=intersectionRectangle(target1,target2);
   // If their boundaries are no interesecting, they cannot be intersecting.
   if (hitRectangle.width*accurracy<1||hitRectangle.height*accurracy<1) {
    return new Rectangle  ;
   }

   var bitmapData:BitmapData=new BitmapData(hitRectangle.width*accurracy,hitRectangle.height*accurracy,false,0x000000);

   // Draw the first target.
   bitmapData.draw(target1,HitTest.getDrawMatrix(target1,hitRectangle,accurracy),new ColorTransform(1,1,1,1,255,-255,-255,255));
   // Overlay the second target.
   bitmapData.draw(target2,HitTest.getDrawMatrix(target2,hitRectangle,accurracy),new ColorTransform(1,1,1,1,255,255,255,255),BlendMode.DIFFERENCE);

   // Find the intersection.
   var intersection:Rectangle=bitmapData.getColorBoundsRect(0xFFFFFFFF,0xFF00FFFF);

   bitmapData.dispose();

   // Alter width and positions to compensate for accurracy
   if (accurracy!=1) {
    intersection.x/=accurracy;
    intersection.y/=accurracy;
    intersection.width/=accurracy;
    intersection.height/=accurracy;
   }

   intersection.x+=hitRectangle.x;
   intersection.y+=hitRectangle.y;

   return intersection;
  }


  protected static function getDrawMatrix(target:DisplayObject,hitRectangle:Rectangle,accurracy:Number):Matrix {
   var localToGlobal:Point;

   var matrix:Matrix;

   var rootConcatenatedMatrix:Matrix=target.root.transform.concatenatedMatrix;

   localToGlobal=target.localToGlobal(new Point  );
   matrix=target.transform.concatenatedMatrix;
   matrix.tx=localToGlobal.x-hitRectangle.x;
   matrix.ty=localToGlobal.y-hitRectangle.y;

   matrix.a=matrix.a/rootConcatenatedMatrix.a;
   matrix.d=matrix.d/rootConcatenatedMatrix.d;
   if (accurracy!=1) {
    matrix.scale(accurracy,accurracy);
   }

   return matrix;
  }

 }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值