f0ab49d322a6f31df6ae22e8c0d2588be0f4886e.svn-base 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. if (!dojo._hasResource["dojox.math.curves"]) { // _hasResource checks added by
  2. // build. Do not use
  3. // _hasResource directly in your
  4. // code.
  5. dojo._hasResource["dojox.math.curves"] = true;
  6. dojo.provide("dojox.math.curves");
  7. dojo.mixin(dojox.math.curves, {
  8. Line : function(start, end) {
  9. this.start = start;
  10. this.end = end;
  11. this.dimensions = start.length;
  12. for (var i = 0; i < start.length; i++) {
  13. start[i] = Number(start[i]);
  14. }
  15. for (var i = 0; i < end.length; i++) {
  16. end[i] = Number(end[i]);
  17. }
  18. this.getValue = function(n) {
  19. var retVal = new Array(this.dimensions);
  20. for (var i = 0; i < this.dimensions; i++) {
  21. retVal[i] = ((this.end[i] - this.start[i]) * n)
  22. + this.start[i];
  23. }
  24. return retVal;
  25. };
  26. return this;
  27. },
  28. Bezier : function(pnts) {
  29. this.getValue = function(step) {
  30. if (step >= 1) {
  31. return this.p[this.p.length - 1];
  32. }
  33. if (step <= 0) {
  34. return this.p[0];
  35. }
  36. var retVal = new Array(this.p[0].length);
  37. for (var k = 0; j < this.p[0].length; k++) {
  38. retVal[k] = 0;
  39. }
  40. for (var j = 0; j < this.p[0].length; j++) {
  41. var C = 0;
  42. var D = 0;
  43. for (var i = 0; i < this.p.length; i++) {
  44. C += this.p[i][j] * this.p[this.p.length - 1][0]
  45. * dojox.math.bernstein(step, this.p.length, i);
  46. }
  47. for (var l = 0; l < this.p.length; l++) {
  48. D += this.p[this.p.length - 1][0]
  49. * dojox.math.bernstein(step, this.p.length, l);
  50. }
  51. retVal[j] = C / D;
  52. }
  53. return retVal;
  54. };
  55. this.p = pnts;
  56. return this;
  57. },
  58. CatmullRom : function(pnts, c) {
  59. this.getValue = function(step) {
  60. var percent = step * (this.p.length - 1);
  61. var node = Math.floor(percent);
  62. var progress = percent - node;
  63. var i0 = node - 1;
  64. if (i0 < 0) {
  65. i0 = 0;
  66. }
  67. var i = node;
  68. var i1 = node + 1;
  69. if (i1 >= this.p.length) {
  70. i1 = this.p.length - 1;
  71. }
  72. var i2 = node + 2;
  73. if (i2 >= this.p.length) {
  74. i2 = this.p.length - 1;
  75. }
  76. var u = progress;
  77. var u2 = progress * progress;
  78. var u3 = progress * progress * progress;
  79. var retVal = new Array(this.p[0].length);
  80. for (var k = 0; k < this.p[0].length; k++) {
  81. var x1 = (-this.c * this.p[i0][k])
  82. + ((2 - this.c) * this.p[i][k])
  83. + ((this.c - 2) * this.p[i1][k])
  84. + (this.c * this.p[i2][k]);
  85. var x2 = (2 * this.c * this.p[i0][k])
  86. + ((this.c - 3) * this.p[i][k])
  87. + ((3 - 2 * this.c) * this.p[i1][k])
  88. + (-this.c * this.p[i2][k]);
  89. var x3 = (-this.c * this.p[i0][k])
  90. + (this.c * this.p[i1][k]);
  91. var x4 = this.p[i][k];
  92. retVal[k] = x1 * u3 + x2 * u2 + x3 * u + x4;
  93. }
  94. return retVal;
  95. };
  96. if (!c) {
  97. this.c = 0.7;
  98. } else {
  99. this.c = c;
  100. }
  101. this.p = pnts;
  102. return this;
  103. },
  104. Arc : function(start, end, ccw) {
  105. function translate(a, b) {
  106. var c = new Array(a.length);
  107. for (var i = 0; i < a.length; i++) {
  108. c[i] = a[i] + b[i];
  109. }
  110. return c;
  111. }
  112. function invert(a) {
  113. var b = new Array(a.length);
  114. for (var i = 0; i < a.length; i++) {
  115. b[i] = -a[i];
  116. }
  117. return b;
  118. }
  119. var center = dojox.math.midpoint(start, end);
  120. var sides = translate(invert(center), start);
  121. var rad = Math.sqrt(Math.pow(sides[0], 2) + Math.pow(sides[1], 2));
  122. var theta = dojox.math.radiansToDegrees(Math.atan(sides[1]
  123. / sides[0]));
  124. if (sides[0] < 0) {
  125. theta -= 90;
  126. } else {
  127. theta += 90;
  128. }
  129. dojox.math.curves.CenteredArc.call(this, center, rad, theta, theta
  130. + (ccw ? -180 : 180));
  131. },
  132. CenteredArc : function(center, radius, start, end) {
  133. this.center = center;
  134. this.radius = radius;
  135. this.start = start || 0;
  136. this.end = end;
  137. this.getValue = function(n) {
  138. var retVal = new Array(2);
  139. var theta = dojox.math.degreesToRadians(this.start
  140. + ((this.end - this.start) * n));
  141. retVal[0] = this.center[0] + this.radius * Math.sin(theta);
  142. retVal[1] = this.center[1] - this.radius * Math.cos(theta);
  143. return retVal;
  144. };
  145. return this;
  146. },
  147. Circle : function(center, radius) {
  148. dojox.math.curves.CenteredArc.call(this, center, radius, 0, 360);
  149. return this;
  150. },
  151. Path : function() {
  152. var curves = [];
  153. var weights = [];
  154. var ranges = [];
  155. var totalWeight = 0;
  156. this.add = function(curve, weight) {
  157. if (weight < 0) {
  158. console
  159. .error("dojox.math.curves.Path.add: weight cannot be less than 0");
  160. }
  161. curves.push(curve);
  162. weights.push(weight);
  163. totalWeight += weight;
  164. computeRanges();
  165. };
  166. this.remove = function(curve) {
  167. for (var i = 0; i < curves.length; i++) {
  168. if (curves[i] == curve) {
  169. curves.splice(i, 1);
  170. totalWeight -= weights.splice(i, 1)[0];
  171. break;
  172. }
  173. }
  174. computeRanges();
  175. };
  176. this.removeAll = function() {
  177. curves = [];
  178. weights = [];
  179. totalWeight = 0;
  180. };
  181. this.getValue = function(n) {
  182. var found = false, value = 0;
  183. for (var i = 0; i < ranges.length; i++) {
  184. var r = ranges[i];
  185. if (n >= r[0] && n < r[1]) {
  186. var subN = (n - r[0]) / r[2];
  187. value = curves[i].getValue(subN);
  188. found = true;
  189. break;
  190. }
  191. }
  192. if (!found) {
  193. value = curves[curves.length - 1].getValue(1);
  194. }
  195. for (var j = 0; j < i; j++) {
  196. value = dojox.math.points.translate(value, curves[j]
  197. .getValue(1));
  198. }
  199. return value;
  200. };
  201. function computeRanges() {
  202. var start = 0;
  203. for (var i = 0; i < weights.length; i++) {
  204. var end = start + weights[i] / totalWeight;
  205. var len = end - start;
  206. ranges[i] = [start, end, len];
  207. start = end;
  208. }
  209. }
  210. return this;
  211. }
  212. });
  213. }