Octant.js 1.2 KB

12345678910111213141516171819202122232425262728293031
  1. import Coordinate from '../geom/Coordinate'
  2. import IllegalArgumentException from '../../../../java/lang/IllegalArgumentException'
  3. export default class Octant {
  4. static octant() {
  5. if (typeof arguments[0] === 'number' && typeof arguments[1] === 'number') {
  6. const dx = arguments[0], dy = arguments[1]
  7. if (dx === 0.0 && dy === 0.0) throw new IllegalArgumentException('Cannot compute the octant for point ( ' + dx + ', ' + dy + ' )')
  8. const adx = Math.abs(dx)
  9. const ady = Math.abs(dy)
  10. if (dx >= 0)
  11. if (dy >= 0)
  12. if (adx >= ady) return 0; else return 1
  13. else
  14. if (adx >= ady) return 7; else return 6
  15. else
  16. if (dy >= 0)
  17. if (adx >= ady) return 3; else return 2
  18. else
  19. if (adx >= ady) return 4; else return 5
  20. } else if (arguments[0] instanceof Coordinate && arguments[1] instanceof Coordinate) {
  21. const p0 = arguments[0], p1 = arguments[1]
  22. const dx = p1.x - p0.x
  23. const dy = p1.y - p0.y
  24. if (dx === 0.0 && dy === 0.0) throw new IllegalArgumentException('Cannot compute the octant for two identical points ' + p0)
  25. return Octant.octant(dx, dy)
  26. }
  27. }
  28. }