Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. class GEO_Line {
  2.  
  3. /**
  4. * Get coordinates of point at line's position
  5. *
  6. * @param {Object} line - line object
  7. * @param {number} percentage - position on line in percents
  8. *
  9. * @return {Object} - point with coordinates
  10. */
  11. getPoint(line, position) {
  12. let equation = this.getLineEquation(line);
  13.  
  14. let dx = line.end.x - line.start.x;
  15.  
  16. let x = line.start.x + (dx * position / 100);
  17. let y = equation(x);
  18.  
  19. return {x, y};
  20. }
  21.  
  22. /**
  23. * Get length of a given line
  24. *
  25. * @param {Object} line - line object
  26. *
  27. * @return {number} - absolute value of line's length
  28. */
  29. getLength(line) {
  30. let dx = line.end.x - line.start.x;
  31. let dy = line.end.y - line.start.y;
  32.  
  33. return Math.abs( Math.sqrt(dx*dx + dy*dy) );
  34. }
  35.  
  36. /**
  37. * Get line equation for given line
  38. *
  39. * @param {Object} line - line object
  40. *
  41. * @return {function}
  42. */
  43. getLineEquation(line) {
  44. let m = this.getSlope(line);
  45.  
  46. /**
  47. * Get y coordinate from x coordinate
  48. *
  49. * @param {number} x - x coordinate
  50. *
  51. * @return {number} - y coordinate
  52. */
  53. let equation = function(x) {
  54. return m*x - m*line.start.x + line.start.y;
  55. }
  56.  
  57. return equation;
  58. }
  59.  
  60. /**
  61. * Get line's slope for line equation
  62. *
  63. * @param {Object} line - line object
  64. *
  65. * @return {number} - slope
  66. */
  67. getSlope(line) {
  68. return (line.end.y - line.start.y)/(line.end.x - line.start.x);
  69. }
  70.  
  71. /**
  72. * Creates proper line object
  73. *
  74. * @param {number} x1 - start x
  75. * @param {number} y1 - start y
  76. * @param {number} x2 - end x
  77. * @param {number} y2 - end y
  78. *
  79. * @return {Object} - line object
  80. */
  81. new(x1, y1, x2, y2) {
  82. return {
  83. start: {x: x1, y: y1},
  84. end: {x: x2, y: y2}
  85. }
  86. }
  87. }
  88.  
  89. const Line = new GEO_Line();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement