SplitSegment.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import Coordinate from '../geom/Coordinate'
  2. export default class SplitSegment {
  3. constructor() {
  4. SplitSegment.constructor_.apply(this, arguments)
  5. }
  6. static constructor_() {
  7. this._seg = null
  8. this._segLen = null
  9. this._splitPt = null
  10. this._minimumLen = 0.0
  11. const seg = arguments[0]
  12. this._seg = seg
  13. this._segLen = seg.getLength()
  14. }
  15. static pointAlongReverse(seg, segmentLengthFraction) {
  16. const coord = new Coordinate()
  17. coord.x = seg.p1.x - segmentLengthFraction * (seg.p1.x - seg.p0.x)
  18. coord.y = seg.p1.y - segmentLengthFraction * (seg.p1.y - seg.p0.y)
  19. return coord
  20. }
  21. splitAt() {
  22. if (arguments.length === 1) {
  23. const pt = arguments[0]
  24. const minFrac = this._minimumLen / this._segLen
  25. if (pt.distance(this._seg.p0) < this._minimumLen) {
  26. this._splitPt = this._seg.pointAlong(minFrac)
  27. return null
  28. }
  29. if (pt.distance(this._seg.p1) < this._minimumLen) {
  30. this._splitPt = SplitSegment.pointAlongReverse(this._seg, minFrac)
  31. return null
  32. }
  33. this._splitPt = pt
  34. } else if (arguments.length === 2) {
  35. const length = arguments[0], endPt = arguments[1]
  36. const actualLen = this.getConstrainedLength(length)
  37. const frac = actualLen / this._segLen
  38. if (endPt.equals2D(this._seg.p0)) this._splitPt = this._seg.pointAlong(frac); else this._splitPt = SplitSegment.pointAlongReverse(this._seg, frac)
  39. }
  40. }
  41. setMinimumLength(minLen) {
  42. this._minimumLen = minLen
  43. }
  44. getConstrainedLength(len) {
  45. if (len < this._minimumLen) return this._minimumLen
  46. return len
  47. }
  48. getSplitPoint() {
  49. return this._splitPt
  50. }
  51. }