flex画箭头算法【收藏】

flex画箭头算法【收藏】
源自网友,非原创。
       //箭头的大小
        public var Radius:int=6;
        //线性的颜色
        public var LineColor:uint=0x000000;
        //是否需要画箭头
        public var NeedArrow:Boolean=true;

       private function GetAngle():int
        {
            var tmpx:int=endX-startX ;
            var tmpy:int=startY -endY ;
            var angle:int= Math.atan2(tmpy,tmpx)*(180/Math.PI);
            return angle;
        }

  //画直线--包括是否画箭头
  public function draw(): void
  {
   this.graphics.clear();//清除前面画的东东
   
   //画线
   this.graphics.lineStyle(thickness, lineColor);
   this.graphics.moveTo(startX, startY);
   this.graphics.lineTo(endX, endY);

   
    /**-------------------画箭头算法----------------------*/
            if(NeedArrow)
            {
                var angle:int= GetAngle();
                var centerX:int=endX-Radius * Math.cos(angle *(Math.PI/180)) ;
                var centerY:int=endY+Radius * Math.sin(angle *(Math.PI/180)) ;
                var topX:int=endX ;
                var topY:int=endY  ;
               
                var leftX:int=centerX + Radius * Math.cos((angle +120) *(Math.PI/180))  ;
                var leftY:int=centerY - Radius * Math.sin((angle +120) *(Math.PI/180))  ;
               
                var rightX:int=centerX + Radius * Math.cos((angle +240) *(Math.PI/180))  ;
                var rightY:int=centerY - Radius * Math.sin((angle +240) *(Math.PI/180))  ;
               
                this.graphics.beginFill(LineColor,1);
               
                this.graphics.lineStyle(1,LineColor,1);
               
                this.graphics.moveTo(topX,topY);
                this.graphics.lineTo(leftX,leftY);
               
                this.graphics.lineTo(centerX,centerY);
               
                this.graphics.lineTo(rightX,rightY);
                this.graphics.lineTo(topX,topY);
                this.graphics.endFill();
            }

  }

你可能感兴趣的:(flex画箭头算法【收藏】)