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.
37 lines
955 B
37 lines
955 B
6 years ago
|
export type SegmentId = string;
|
||
6 years ago
|
|
||
|
export class Segment {
|
||
6 years ago
|
constructor(private _id: SegmentId, public from: number, public to: number) {
|
||
6 years ago
|
if(this._id === undefined) {
|
||
|
throw new Error('id is undefined');
|
||
6 years ago
|
}
|
||
|
if(isNaN(+from)) {
|
||
|
throw new Error('from can`t be NaN');
|
||
|
}
|
||
|
if(isNaN(+to)) {
|
||
|
throw new Error('to can`t be NaN');
|
||
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
get id(): SegmentId { return this._id; }
|
||
|
set id(value) { this._id = value; }
|
||
6 years ago
|
|
||
|
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);
|
||
6 years ago
|
return new Segment(this._id, p, q);
|
||
6 years ago
|
}
|
||
|
|
||
|
equals(segment: Segment) {
|
||
6 years ago
|
return this._id === segment._id;
|
||
6 years ago
|
}
|
||
6 years ago
|
}
|