SegmentPointComparator.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import Assert from '../util/Assert'
  2. export default class SegmentPointComparator {
  3. static relativeSign(x0, x1) {
  4. if (x0 < x1) return -1
  5. if (x0 > x1) return 1
  6. return 0
  7. }
  8. static compare(octant, p0, p1) {
  9. if (p0.equals2D(p1)) return 0
  10. const xSign = SegmentPointComparator.relativeSign(p0.x, p1.x)
  11. const ySign = SegmentPointComparator.relativeSign(p0.y, p1.y)
  12. switch (octant) {
  13. case 0:
  14. return SegmentPointComparator.compareValue(xSign, ySign)
  15. case 1:
  16. return SegmentPointComparator.compareValue(ySign, xSign)
  17. case 2:
  18. return SegmentPointComparator.compareValue(ySign, -xSign)
  19. case 3:
  20. return SegmentPointComparator.compareValue(-xSign, ySign)
  21. case 4:
  22. return SegmentPointComparator.compareValue(-xSign, -ySign)
  23. case 5:
  24. return SegmentPointComparator.compareValue(-ySign, -xSign)
  25. case 6:
  26. return SegmentPointComparator.compareValue(-ySign, xSign)
  27. case 7:
  28. return SegmentPointComparator.compareValue(xSign, -ySign)
  29. }
  30. Assert.shouldNeverReachHere('invalid octant value')
  31. return 0
  32. }
  33. static compareValue(compareSign0, compareSign1) {
  34. if (compareSign0 < 0) return -1
  35. if (compareSign0 > 0) return 1
  36. if (compareSign1 < 0) return -1
  37. if (compareSign1 > 0) return 1
  38. return 0
  39. }
  40. }