Elektro/src/main/angular/src/app/editor/wire/Wire.ts

43 lines
1.0 KiB
TypeScript

import {Junction} from "../junction/Junction";
import {RESISTANCE_MIN} from '../circuit/Circuit';
export class Wire {
current: number = 0;
constructor(
readonly start: Junction,
readonly end: Junction,
public resistance: number,
public name: string | null = null,
) {
this.start.wires.push(this);
this.end.wires.push(this);
if (this.resistance === 0) {
this.resistance = RESISTANCE_MIN;
}
}
get absCurrent(): number {
return Math.abs(this.current);
}
toString() {
if (this.start.part === this.end.part && this.name !== null) {
return `'${this.start.part}' "${this.name}"`;
}
return `${this.name !== null ? this.name + ' ' : ''}${this.start.fullName} ==> ${this.end.fullName}`;
}
traverse(junction: Junction) {
if (junction === this.start) {
return this.end;
}
if (junction === this.end) {
return this.start;
}
throw new Error(`Wire is not connected to given Junction: wire=${this}, junction=${junction}`);
}
}