utils.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. ;(function(undefined) {
  2. 'use strict';
  3. if (typeof sigma === 'undefined')
  4. throw 'sigma is not declared';
  5. var _root = this;
  6. // Initialize packages:
  7. sigma.utils = sigma.utils || {};
  8. /**
  9. * Return the control point coordinates for a quadratic bezier curve.
  10. *
  11. * @param {number} x1 The X coordinate of the start point.
  12. * @param {number} y1 The Y coordinate of the start point.
  13. * @param {number} x2 The X coordinate of the end point.
  14. * @param {number} y2 The Y coordinate of the end point.
  15. * @param {number} a Modifier for the amplitude of the curve.
  16. * @return {x,y} The control point coordinates.
  17. */
  18. sigma.utils.getQuadraticControlPoint = function(x1, y1, x2, y2, a) {
  19. a = a || 0;
  20. return {
  21. x: (x1 + x2) / 2 + (y2 - y1) / (60 / (15 + a)),
  22. y: (y1 + y2) / 2 + (x1 - x2) / (60 / (15 + a))
  23. };
  24. };
  25. /**
  26. * Return the coordinates of the two control points for a self loop (i.e.
  27. * where the start point is also the end point) computed as a cubic bezier
  28. * curve.
  29. *
  30. * @param {number} x The X coordinate of the node.
  31. * @param {number} y The Y coordinate of the node.
  32. * @param {number} size The node size.
  33. * @param {number} a Modifier to the loop size.
  34. * @return {x1,y1,x2,y2} The coordinates of the two control points.
  35. */
  36. sigma.utils.getSelfLoopControlPoints = function(x , y, size, a) {
  37. a = a || 0;
  38. return {
  39. x1: x - (size + a) * 7,
  40. y1: y,
  41. x2: x,
  42. y2: y + (size + a) * 7
  43. };
  44. };
  45. }).call(this);