curves.js 5.4 KB

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