arc.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.gfx.arc"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.gfx.arc"] = true;
  8. dojo.provide("dojox.gfx.arc");
  9. dojo.require("dojox.gfx.matrix");
  10. (function(){
  11. var m = dojox.gfx.matrix,
  12. twoPI = 2 * Math.PI, pi4 = Math.PI / 4, pi8 = Math.PI / 8,
  13. pi48 = pi4 + pi8, curvePI4 = unitArcAsBezier(pi8);
  14. function unitArcAsBezier(alpha){
  15. // summary: return a start point, 1st and 2nd control points, and an end point of
  16. // a an arc, which is reflected on the x axis
  17. // alpha: Number: angle in radians, the arc will be 2 * angle size
  18. var cosa = Math.cos(alpha), sina = Math.sin(alpha),
  19. p2 = {x: cosa + (4 / 3) * (1 - cosa), y: sina - (4 / 3) * cosa * (1 - cosa) / sina};
  20. return { // Object
  21. s: {x: cosa, y: -sina},
  22. c1: {x: p2.x, y: -p2.y},
  23. c2: p2,
  24. e: {x: cosa, y: sina}
  25. };
  26. }
  27. dojox.gfx.arc = {
  28. unitArcAsBezier: unitArcAsBezier,
  29. curvePI4: curvePI4,
  30. arcAsBezier: function(last, rx, ry, xRotg, large, sweep, x, y){
  31. // summary: calculates an arc as a series of Bezier curves
  32. // given the last point and a standard set of SVG arc parameters,
  33. // it returns an array of arrays of parameters to form a series of
  34. // absolute Bezier curves.
  35. // last: Object: a point-like object as a start of the arc
  36. // rx: Number: a horizontal radius for the virtual ellipse
  37. // ry: Number: a vertical radius for the virtual ellipse
  38. // xRotg: Number: a rotation of an x axis of the virtual ellipse in degrees
  39. // large: Boolean: which part of the ellipse will be used (the larger arc if true)
  40. // sweep: Boolean: direction of the arc (CW if true)
  41. // x: Number: the x coordinate of the end point of the arc
  42. // y: Number: the y coordinate of the end point of the arc
  43. // calculate parameters
  44. large = Boolean(large);
  45. sweep = Boolean(sweep);
  46. var xRot = m._degToRad(xRotg),
  47. rx2 = rx * rx, ry2 = ry * ry,
  48. pa = m.multiplyPoint(
  49. m.rotate(-xRot),
  50. {x: (last.x - x) / 2, y: (last.y - y) / 2}
  51. ),
  52. pax2 = pa.x * pa.x, pay2 = pa.y * pa.y,
  53. c1 = Math.sqrt((rx2 * ry2 - rx2 * pay2 - ry2 * pax2) / (rx2 * pay2 + ry2 * pax2));
  54. if(isNaN(c1)){ c1 = 0; }
  55. var ca = {
  56. x: c1 * rx * pa.y / ry,
  57. y: -c1 * ry * pa.x / rx
  58. };
  59. if(large == sweep){
  60. ca = {x: -ca.x, y: -ca.y};
  61. }
  62. // the center
  63. var c = m.multiplyPoint(
  64. [
  65. m.translate(
  66. (last.x + x) / 2,
  67. (last.y + y) / 2
  68. ),
  69. m.rotate(xRot)
  70. ],
  71. ca
  72. );
  73. // calculate the elliptic transformation
  74. var elliptic_transform = m.normalize([
  75. m.translate(c.x, c.y),
  76. m.rotate(xRot),
  77. m.scale(rx, ry)
  78. ]);
  79. // start, end, and size of our arc
  80. var inversed = m.invert(elliptic_transform),
  81. sp = m.multiplyPoint(inversed, last),
  82. ep = m.multiplyPoint(inversed, x, y),
  83. startAngle = Math.atan2(sp.y, sp.x),
  84. endAngle = Math.atan2(ep.y, ep.x),
  85. theta = startAngle - endAngle; // size of our arc in radians
  86. if(sweep){ theta = -theta; }
  87. if(theta < 0){
  88. theta += twoPI;
  89. }else if(theta > twoPI){
  90. theta -= twoPI;
  91. }
  92. // draw curve chunks
  93. var alpha = pi8, curve = curvePI4, step = sweep ? alpha : -alpha,
  94. result = [];
  95. for(var angle = theta; angle > 0; angle -= pi4){
  96. if(angle < pi48){
  97. alpha = angle / 2;
  98. curve = unitArcAsBezier(alpha);
  99. step = sweep ? alpha : -alpha;
  100. angle = 0; // stop the loop
  101. }
  102. var c1, c2, e,
  103. M = m.normalize([elliptic_transform, m.rotate(startAngle + step)]);
  104. if(sweep){
  105. c1 = m.multiplyPoint(M, curve.c1);
  106. c2 = m.multiplyPoint(M, curve.c2);
  107. e = m.multiplyPoint(M, curve.e );
  108. }else{
  109. c1 = m.multiplyPoint(M, curve.c2);
  110. c2 = m.multiplyPoint(M, curve.c1);
  111. e = m.multiplyPoint(M, curve.s );
  112. }
  113. // draw the curve
  114. result.push([c1.x, c1.y, c2.x, c2.y, e.x, e.y]);
  115. startAngle += 2 * step;
  116. }
  117. return result; // Array
  118. }
  119. };
  120. })();
  121. }