Guest User

Untitled

a guest
Jun 21st, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. function Bezier(a, b, c, d) {
  2. this.a = a;
  3. this.b = b;
  4. this.c = c;
  5. this.d = d;
  6.  
  7. this.len = 100;
  8. this.arcLengths = new Array(this.len + 1);
  9. this.arcLengths[0] = 0;
  10.  
  11. var ox = this.x(0), oy = this.y(0), clen = 0;
  12. for(var i = 1; i <= this.len; i += 1) {
  13. var x = this.x(i * 0.01), y = this.y(i * 0.01);
  14. var dx = ox - x, dy = oy - y;
  15. clen += Math.sqrt(dx * dx + dy * dy);
  16. this.arcLengths[i] = clen;
  17. ox = x, oy = y;
  18. }
  19. this.length = clen;
  20. }
  21.  
  22. Bezier.prototype = {
  23. map: function(u) {
  24. var targetLength = u * this.arcLengths[this.len];
  25. var low = 0, high = this.len, index = 0;
  26. while (low < high) {
  27. index = low + (((high - low) / 2) | 0);
  28. if (this.arcLengths[index] < targetLength) {
  29. low = index + 1;
  30.  
  31. } else {
  32. high = index;
  33. }
  34. }
  35. if (this.arcLengths[index] > targetLength) {
  36. index--;
  37. }
  38.  
  39. var lengthBefore = this.arcLengths[index];
  40. if (lengthBefore === targetLength) {
  41. return index / this.len;
  42.  
  43. } else {
  44. return (index + (targetLength - lengthBefore) / (this.arcLengths[index + 1] - lengthBefore)) / this.len;
  45. }
  46. },
  47.  
  48. mx: function (u) {
  49. return this.x(this.map(u));
  50. },
  51.  
  52. my: function (u) {
  53. return this.y(this.map(u));
  54. },
  55.  
  56. x: function (t) {
  57. return ((1 - t) * (1 - t) * (1 - t)) * this.a.x
  58. + 3 * ((1 - t) * (1 - t)) * t * this.b.x
  59. + 3 * (1 - t) * (t * t) * this.c.x
  60. + (t * t * t) * this.d.x;
  61. },
  62.  
  63. y: function (t) {
  64. return ((1 - t) * (1 - t) * (1 - t)) * this.a.y
  65. + 3 * ((1 - t) * (1 - t)) * t * this.b.y
  66. + 3 * (1 - t) * (t * t) * this.c.y
  67. + (t * t * t) * this.d.y;
  68. }
  69. };
Add Comment
Please, Sign In to add comment