Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. class Point {
  2. constructor(x, y) {
  3. this.x = x;
  4. this.y = y;
  5. }
  6. distance(point) {
  7. const diffX = Math.abs(point.x - this.x);
  8. const diffY = Math.abs(point.y - this.y);
  9. return Math.sqrt(Math.pow(diffX, 2) + Math.pow(diffY, 2));
  10. }
  11. }
  12. const coordinates = [
  13. new Point(46, 88),
  14. new Point(13, 68),
  15. new Point(18, 43),
  16. new Point(64, 21),
  17. new Point(22, 96),
  18. new Point(86, 76),
  19. new Point(92, 13),
  20. new Point(46, 53),
  21. new Point(33, 42)
  22. ];
  23. const matrixDistance = coordinates.map(point1 =>
  24. coordinates.map(point2 => point1.distance(point2))
  25. );
  26. const MaxDistance = Math.max(...matrixDistance.map(arr => Math.max(...arr)));
  27. const MinDistance = Math.min(
  28. ...matrixDistance.map(arr =>
  29. Math.min(...arr.filter(distance => distance !== 0))
  30. )
  31. );
  32.  
  33. console.log(matrixDistance); // 距离矩阵
  34. console.log(MinDistance); // 最近的距离
  35. console.log(MaxDistance); // 最远的距离
  36.  
  37. // TODO: 在一堆坐标系中, 从一个点, 到达另一个点, 最快的路线
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement