fork download
  1. let ex1 = [[0, 0], [1, 2], [2, 0], [0, 1], [2, 1]];
  2. let ex2 = [[0, 0], [1, 2], [2, 0], [0, 2], [2, 2]];
  3. let ex3 = [[0, 0], [0, 1], [1, 2], [2, 1], [2, 0]];
  4. let ex4 = [[0, 0], [5, 5], [0, 5], [1, 0], [1, 6]];
  5.  
  6. console.log(isStar(ex1));
  7. console.log(isStar(ex2));
  8. console.log(isStar(ex3));
  9. console.log(isStar(ex4));
  10.  
  11.  
  12.  
  13. function isStar(points) {
  14. return cross(points[0], points[1], points[2], points[3])
  15. && cross(points[1], points[2], points[3], points[4])
  16. && cross(points[2], points[3], points[4], points[0]);
  17. }
  18.  
  19.  
  20. // 線分a-bと線分c-dの交差判定
  21. function cross(a, b, c, d) {
  22. let s1 = (a[0] - b[0]) * (c[1] - a[1]) - (a[1] - b[1]) * (c[0] - a[0]);
  23. let t1 = (a[0] - b[0]) * (d[1] - a[1]) - (a[1] - b[1]) * (d[0] - a[0]);
  24. if (s1 * t1 > -1e-100)
  25. return false;
  26.  
  27. let s2 = (c[0] - d[0]) * (a[1] - c[1]) - (c[1] - d[1]) * (a[0] - c[0]);
  28. let t2 = (c[0] - d[0]) * (b[1] - c[1]) - (c[1] - d[1]) * (b[0] - c[0]);
  29. if (s2 * t2 > -1e-100)
  30. return false;
  31.  
  32. return true;
  33. }
Success #stdin #stdout 0.02s 16780KB
stdin
Standard input is empty
stdout
true
false
false
true