SHARE
TWEET

Untitled

a guest Oct 10th, 2019 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ////////////
  2. function left(a, b, c) {
  3.     const result = (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
  4.     if (result > 0) return -1;
  5.     else if (result < 0) return 1;
  6.     else return 0;
  7. }
  8. ////////////
  9.  
  10. points.sort((a, b) => a.y - b.y);
  11. const startPoint = points.shift();
  12.  
  13. points.sort((a, b) => left(startPoint, a, b));
  14. points.unshift(startPoint);
  15.  
  16. const N = points.length;
  17. let stack = new Array();
  18.  
  19. stack.push(points[0]);
  20. stack.push(points[1]);
  21. stack.push(points[2]);
  22.  
  23. for (let i = 3; i < N; i++) {
  24.     let top = stack.pop();
  25.     while (left(stack[stack.length - 1], top, points[i]) != -1) {
  26.         top = stack.pop();
  27.     }
  28.     stack.push(top);
  29.     stack.push(points[i]);
  30. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top