curves.js 5.2 KB

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