arc.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. define("dojox/gfx/arc", ["./_base", "dojo/_base/lang", "./matrix"],
  2. function(g, lang, m){
  3. /*=====
  4. g = dojox.gfx;
  5. dojox.gfx.arc = {
  6. // summary:
  7. // This module contains the core graphics Arc functions.
  8. };
  9. =====*/
  10. var twoPI = 2 * Math.PI, pi4 = Math.PI / 4, pi8 = Math.PI / 8,
  11. pi48 = pi4 + pi8, curvePI4 = unitArcAsBezier(pi8);
  12. function unitArcAsBezier(alpha){
  13. // summary: return a start point, 1st and 2nd control points, and an end point of
  14. // a an arc, which is reflected on the x axis
  15. // alpha: Number
  16. // angle in radians, the arc will be 2 * angle size
  17. var cosa = Math.cos(alpha), sina = Math.sin(alpha),
  18. p2 = {x: cosa + (4 / 3) * (1 - cosa), y: sina - (4 / 3) * cosa * (1 - cosa) / sina};
  19. return { // Object
  20. s: {x: cosa, y: -sina},
  21. c1: {x: p2.x, y: -p2.y},
  22. c2: p2,
  23. e: {x: cosa, y: sina}
  24. };
  25. }
  26. var arc = g.arc = {
  27. unitArcAsBezier: unitArcAsBezier,
  28. /*=====
  29. unitArcAsBezier: function(alpha) {
  30. // summary: return a start point, 1st and 2nd control points, and an end point of
  31. // a an arc, which is reflected on the x axis
  32. // alpha: Number
  33. // angle in radians, the arc will be 2 * angle size
  34. },
  35. =====*/
  36. curvePI4: curvePI4,
  37. // curvePI4: Object
  38. // an object with properties of an arc around a unit circle from 0 to pi/4
  39. arcAsBezier: function(last, rx, ry, xRotg, large, sweep, x, y){
  40. // summary: calculates an arc as a series of Bezier curves
  41. // given the last point and a standard set of SVG arc parameters,
  42. // it returns an array of arrays of parameters to form a series of
  43. // absolute Bezier curves.
  44. // last: Object
  45. // a point-like object as a start of the arc
  46. // rx: Number
  47. // a horizontal radius for the virtual ellipse
  48. // ry: Number
  49. // a vertical radius for the virtual ellipse
  50. // xRotg: Number
  51. // a rotation of an x axis of the virtual ellipse in degrees
  52. // large: Boolean
  53. // which part of the ellipse will be used (the larger arc if true)
  54. // sweep: Boolean
  55. // direction of the arc (CW if true)
  56. // x: Number
  57. // the x coordinate of the end point of the arc
  58. // y: Number
  59. // the y coordinate of the end point of the arc
  60. // calculate parameters
  61. large = Boolean(large);
  62. sweep = Boolean(sweep);
  63. var xRot = m._degToRad(xRotg),
  64. rx2 = rx * rx, ry2 = ry * ry,
  65. pa = m.multiplyPoint(
  66. m.rotate(-xRot),
  67. {x: (last.x - x) / 2, y: (last.y - y) / 2}
  68. ),
  69. pax2 = pa.x * pa.x, pay2 = pa.y * pa.y,
  70. c1 = Math.sqrt((rx2 * ry2 - rx2 * pay2 - ry2 * pax2) / (rx2 * pay2 + ry2 * pax2));
  71. if(isNaN(c1)){ c1 = 0; }
  72. var ca = {
  73. x: c1 * rx * pa.y / ry,
  74. y: -c1 * ry * pa.x / rx
  75. };
  76. if(large == sweep){
  77. ca = {x: -ca.x, y: -ca.y};
  78. }
  79. // the center
  80. var c = m.multiplyPoint(
  81. [
  82. m.translate(
  83. (last.x + x) / 2,
  84. (last.y + y) / 2
  85. ),
  86. m.rotate(xRot)
  87. ],
  88. ca
  89. );
  90. // calculate the elliptic transformation
  91. var elliptic_transform = m.normalize([
  92. m.translate(c.x, c.y),
  93. m.rotate(xRot),
  94. m.scale(rx, ry)
  95. ]);
  96. // start, end, and size of our arc
  97. var inversed = m.invert(elliptic_transform),
  98. sp = m.multiplyPoint(inversed, last),
  99. ep = m.multiplyPoint(inversed, x, y),
  100. startAngle = Math.atan2(sp.y, sp.x),
  101. endAngle = Math.atan2(ep.y, ep.x),
  102. theta = startAngle - endAngle; // size of our arc in radians
  103. if(sweep){ theta = -theta; }
  104. if(theta < 0){
  105. theta += twoPI;
  106. }else if(theta > twoPI){
  107. theta -= twoPI;
  108. }
  109. // draw curve chunks
  110. var alpha = pi8, curve = curvePI4, step = sweep ? alpha : -alpha,
  111. result = [];
  112. for(var angle = theta; angle > 0; angle -= pi4){
  113. if(angle < pi48){
  114. alpha = angle / 2;
  115. curve = unitArcAsBezier(alpha);
  116. step = sweep ? alpha : -alpha;
  117. angle = 0; // stop the loop
  118. }
  119. var c2, e, M = m.normalize([elliptic_transform, m.rotate(startAngle + step)]);
  120. if(sweep){
  121. c1 = m.multiplyPoint(M, curve.c1);
  122. c2 = m.multiplyPoint(M, curve.c2);
  123. e = m.multiplyPoint(M, curve.e );
  124. }else{
  125. c1 = m.multiplyPoint(M, curve.c2);
  126. c2 = m.multiplyPoint(M, curve.c1);
  127. e = m.multiplyPoint(M, curve.s );
  128. }
  129. // draw the curve
  130. result.push([c1.x, c1.y, c2.x, c2.y, e.x, e.y]);
  131. startAngle += 2 * step;
  132. }
  133. return result; // Array
  134. }
  135. };
  136. return arc;
  137. });