flex 画虚线矩形算法

来源:互联网 发布:四维彩超数据看男女 编辑:程序博客网 时间:2024/06/05 04:27

选定矩形的一个顶点为起点,开始画

x0:顶点横坐标

y0:顶点纵坐标

x1:水平线上另一顶点的横坐标

y1:水平线上另一丁点的纵坐标

 

 

 

 private function drawDottenRect(g:Graphics,
                   x0:Number,
                   y0:Number,
                   x1:Number,
                   y1:Number):void{
             
             drawDottedHorizontalLineTo(g,y0,x0,x1);
             drawDottedVerticalLineTo(g,x0,y0,y1);
             drawDottedHorizontalLineTo(g,y1,x1,x0);
             drawDottedVerticalLineTo(g,x1,y0,y1);
               
   }
   
   private  function drawDottedHorizontalLineTo(
    g:Graphics,
    y : Number,
    x0 : Number,
    x1 : Number
    ) : void
   {
    if( x0 < x1){
     g.moveTo(x0, y);
     for(var x:Number = x0 + start; x<x1; x+= dotLength + gap){
      if( x + dotLength + gap <= x1){
       g.lineTo(x + dotLength,y);
       g.moveTo(x + dotLength + gap, y);
      }else{
       if(x + dotLength > x1){
        g.lineTo(x1, y);
       }else{
        g.lineTo(x + dotLength,y);
        
       }
      }
      
     }
    }else{
     g.moveTo(x0, y);
     for(var xSecond:Number = x0  ; xSecond>x1; xSecond-= dotLength + gap){
      
      if(xSecond - (dotLength + gap )>=x1){
       g.lineTo(xSecond - dotLength,y);
       g.moveTo(xSecond - dotLength - gap, y);
      }else{
       if(xSecond - dotLength < x1){
        g.lineTo(x1, y);
       }else{
        g.lineTo( xSecond - dotLength, y);
       }
      }
      
     }
    }
    
   }  
   
   private  function drawDottedVerticalLineTo(
    g:Graphics,
    x : Number,
    y0 : Number,
    y1 : Number
    ) : void
   {
    if(y0 <= y1){
     g.moveTo(x, y0);
     for(var y:Number=y0; y <= y1; y+= dotLength + gap){
      if( y + dotLength + gap <= y1){
       g.lineTo(x, y + dotLength);
       g.moveTo(x, y + dotLength + gap);
      }else{
       if( y + dotLength > y1){
        g.lineTo(x, y1);
       }else{
        g.lineTo( x, y + dotLength );
       }
      }
      
     }
    }else{
     g.moveTo(x, y1);
     for(var ySecond:Number = y0; ySecond>= y1; ySecond-=dotLength + gap){
      if(ySecond - dotLength  - gap >= y1){
       g.lineTo(x, ySecond - dotLength);
       g.moveTo(x, ySecond - dotLength - gap);
      }else{
       if(ySecond - dotLength < y1){
        g.lineTo(x, y1);
       }else{
        g.lineTo(x, ySecond - dotLength );
       }
      }
      
     }
    }
   }