Length.js 513 B

1234567891011121314151617181920212223
  1. import Coordinate from '../geom/Coordinate'
  2. export default class Length {
  3. static ofLine(pts) {
  4. const n = pts.size()
  5. if (n <= 1) return 0.0
  6. let len = 0.0
  7. const p = new Coordinate()
  8. pts.getCoordinate(0, p)
  9. let x0 = p.x
  10. let y0 = p.y
  11. for (let i = 1; i < n; i++) {
  12. pts.getCoordinate(i, p)
  13. const x1 = p.x
  14. const y1 = p.y
  15. const dx = x1 - x0
  16. const dy = y1 - y0
  17. len += Math.sqrt(dx * dx + dy * dy)
  18. x0 = x1
  19. y0 = y1
  20. }
  21. return len
  22. }
  23. }