import { sum, harmonicMean as harm, flatten } from './utils.js'; class Resistor { ohmage; constructor(ohmage) { this.ohmage = ohmage; } getOhmage() { return this.ohmage; } equals(other) { return this.getOhmage() === other.getOhmage(); } hash() { return this.ohmage; } cost() { return 1; } toString() { return `${this.constructor.name}(${this.getOhmage()})`; } prettyString(depth = 0) { return `${'--'.repeat(depth)}${this.toString()}` } parallel(other) { return new ParallelResistor(this, other); } series(other) { return new SeriesResistor(this, other); } getSVG() { var svg = ` ${this.getOhmage()}Ω` var [width, height] = [50, 100] return [svg, width, height] } draw(element = document.getElementsByTagName("body")[0]) { var [svg, width, height] = this.getSVG(); var maxHeight = height + 2*25 var maxWidth = width var midPoint = width / 2 var thesvg = ` ${svg} ` element.innerHTML += thesvg; } } class NestedResistor extends Resistor { children = [] constructor(l, func, instance) { super(func(l.map(e => e.getOhmage()))); this.func = func this.children = flatten(l, instance.constructor.name); } cost() { return sum(this.children.map(e => e.cost())); } prettyString(depth = 0) { var childrenStrings = this.children.map(c => c.prettyString(depth + 1)).join("\n") return `${'--'.repeat(depth)}${this.toString()}\n${childrenStrings}` } parallel(other) { return new ParallelResistor(this, other); } series(other) { return new SeriesResistor(this, other); } } class SeriesResistor extends NestedResistor { constructor(...l) { super(l, sum, "SeriesResistor"); } getSVG() { var svgs = this.children.map(e => e.getSVG()); // triplets [svg, width, height] var n = svgs.length; var maxHeight = sum(svgs.map(([svg, width, height]) => height)) + 25 * (n - 1) var maxWidth = Math.max(...svgs.map(([svg, width, height]) => width)) var yOffset = 0; // where to start the next boundin var centerLine = maxWidth / 2; var innerSVGs = [] var i = 0; svgs.forEach(([svg, width, height]) => { var xStart = (maxWidth - width) / 2 var partialInnerSVG = `${svg}` if (i < n - 1) partialInnerSVG += `` // finally yOffset += 25 + height i ++; innerSVGs.push(partialInnerSVG) }); var svg = innerSVGs.join("") return [svg, maxWidth, maxHeight] } } class ParallelResistor extends NestedResistor { constructor(...l) { super(l, harm, "ParallelResistor"); } getSVG() { var svgs = this.children.map(e => e.getSVG()); // triplets [svg, width, height] var n = svgs.length; var maxHeight = Math.max(...svgs.map(([svg, width, height]) => height)) + 2 * 25 var maxWidth = sum(svgs.map(([svg, width, height]) => width)) + 25 * (n - 1) var bottomLineStart = svgs[0][1] / 2 var bottomLineEnd = maxWidth - svgs[n - 1][1] / 2 var xOffset = 0; // where to start the next boundin var innerSVGs = [] svgs.forEach(([svg, width, height]) => { var antennaX = xOffset + width / 2 var antennaLength = (maxHeight - height) / 2 var antennaOneStart = 0 var antennaOneEnd = antennaLength var antennaTwoStart = antennaLength + height var antennaTwoEnd = maxHeight var partialInnerSVG = `${svg} ` // finally xOffset += 25 + width innerSVGs.push(partialInnerSVG) }); var innerSVG = innerSVGs.join("") var svg = ` ${innerSVG}` return [svg, maxWidth, maxHeight] } } export { Resistor, ParallelResistor, SeriesResistor }