GraphicsUtils.as 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*Copyright (c) 2006 Adobe Systems Incorporated
  2. Permission is hereby granted, free of charge, to any person
  3. obtaining a copy of this software and associated documentation
  4. files (the "Software"), to deal in the Software without
  5. restriction, including without limitation the rights to use,
  6. copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the
  8. Software is furnished to do so, subject to the following
  9. conditions:
  10. The above copyright notice and this permission notice shall be
  11. included in all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  14. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  16. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  17. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  19. OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21. package net.srirangan.utils
  22. {
  23. import mx.graphics.IStroke;
  24. import flash.display.Graphics;
  25. import mx.controls.Alert;
  26. public class GraphicsUtils
  27. {
  28. public static function _drawDashedLine(target:Graphics,stroke:IStroke,pattern:Array,
  29. drawingState:DashStruct,
  30. x0:Number,y0:Number,x1:Number,y1:Number):void
  31. {
  32. var dX:Number = x1 - x0;
  33. var dY:Number = y1 - y0;
  34. var len:Number = Math.sqrt(dX*dX + dY*dY);
  35. dX /= len;
  36. dY /= len;
  37. var tMax:Number = len;
  38. var t:Number = -drawingState.offset;
  39. var bDrawing:Boolean = drawingState.drawing;
  40. var patternIndex:int = drawingState.patternIndex;
  41. var styleInited:Boolean = drawingState.styleInited;
  42. while(t < tMax)
  43. {
  44. t += pattern[patternIndex];
  45. if(t < 0)
  46. {
  47. var x:int = 5;
  48. }
  49. if(t >= tMax)
  50. {
  51. drawingState.offset = pattern[patternIndex] - (t - tMax);
  52. drawingState.patternIndex = patternIndex;
  53. drawingState.drawing = bDrawing;
  54. drawingState.styleInited = true;
  55. t = tMax;
  56. }
  57. if(styleInited == false)
  58. {
  59. if(bDrawing)
  60. stroke.apply(target);
  61. else
  62. target.lineStyle(0,0,0);
  63. }
  64. else
  65. {
  66. styleInited = false;
  67. }
  68. target.lineTo(x0 + t*dX,y0 + t*dY);
  69. bDrawing = !bDrawing;
  70. patternIndex = (patternIndex + 1) % pattern.length;
  71. }
  72. }
  73. public static function drawDashedLine(target:Graphics,stroke:IStroke,pattern:Array,x0:Number,y0:Number,x1:Number,y1:Number):void
  74. {
  75. target.moveTo(x0,y0);
  76. var struct:DashStruct = new DashStruct();
  77. _drawDashedLine(target,stroke,pattern,struct,x0,y0,x1,y1);
  78. }
  79. public static function drawDashedPolyLine(target:Graphics,stroke:IStroke,pattern:Array,points:Array):void
  80. {
  81. if(points.length == 0)
  82. return;
  83. var prev:Object = points[0];
  84. var struct:DashStruct = new DashStruct();
  85. target.moveTo(prev.x,prev.y);
  86. for(var i:int = 1;i<points.length;i++)
  87. {
  88. var current:Object = points[i];
  89. _drawDashedLine(target,stroke,pattern,struct,prev.x,prev.y,current.x,current.y);
  90. prev = current;
  91. }
  92. }
  93. }
  94. }
  95. class DashStruct
  96. {
  97. public function init():void
  98. {
  99. drawing = true;
  100. patternIndex = 0;
  101. offset = 0;
  102. }
  103. public var drawing:Boolean = true;
  104. public var patternIndex:int = 0;
  105. public var offset:Number = 0;
  106. public var styleInited:Boolean = false;
  107. }