You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
36 lines
955 B
36 lines
955 B
export type SegmentId = string; |
|
|
|
export class Segment { |
|
constructor(private _id: SegmentId, public from: number, public to: number) { |
|
if(this._id === undefined) { |
|
throw new Error('id is undefined'); |
|
} |
|
if(isNaN(+from)) { |
|
throw new Error('from can`t be NaN'); |
|
} |
|
if(isNaN(+to)) { |
|
throw new Error('to can`t be NaN'); |
|
} |
|
} |
|
|
|
get id(): SegmentId { return this._id; } |
|
set id(value) { this._id = value; } |
|
|
|
get middle() { return (this.from + this.to) / 2; } |
|
|
|
get length() { |
|
return Math.max(this.from, this.to) - Math.min(this.from, this.to); |
|
} |
|
|
|
expandDist(allDist: number, portion: number): Segment { |
|
var p = Math.round(this.middle - allDist * portion / 2); |
|
var q = Math.round(this.middle + allDist * portion / 2); |
|
p = Math.min(p, this.from); |
|
q = Math.max(q, this.to); |
|
return new Segment(this._id, p, q); |
|
} |
|
|
|
equals(segment: Segment) { |
|
return this._id === segment._id; |
|
} |
|
}
|
|
|