Rendering works
This commit is contained in:
parent
19af0555cd
commit
bb76e3dcc7
203
main.js
203
main.js
@ -22,52 +22,30 @@ class Resistor {
|
|||||||
return `${'--'.repeat(depth)}${this.toString()}`
|
return `${'--'.repeat(depth)}${this.toString()}`
|
||||||
}
|
}
|
||||||
parallel(other) {
|
parallel(other) {
|
||||||
return NewParallelResistor.smart(this, other);
|
return new ParallelResistor(this, other);
|
||||||
}
|
}
|
||||||
series(other) {
|
series(other) {
|
||||||
return NewSeriesResistor.smart(this, other);
|
return new SeriesResistor(this, other);
|
||||||
}
|
}
|
||||||
}
|
getSVG() {
|
||||||
class ComplexResistor extends Resistor {
|
var svg = `<svg width="50" heigh="100"><rect x="0" y="0" width="50" height="100" fill="white" stroke="black"/>
|
||||||
left;
|
<text x="25" y="50" dominant-baseline="middle" text-anchor="middle">${this.getOhmage()}Ω</text></svg>`
|
||||||
constructor(other) {
|
var [width, height] = [50, 100]
|
||||||
super(other.getOhmage());
|
return [svg, width, height]
|
||||||
this.left = other;
|
|
||||||
}
|
}
|
||||||
}
|
draw() {
|
||||||
class SeriesResistor extends Resistor {
|
var [svg, width, height] = this.getSVG();
|
||||||
left;
|
var maxHeight = height + 2*25
|
||||||
right;
|
var maxWidth = width
|
||||||
constructor(r1, r2) {
|
var midPoint = width / 2
|
||||||
super(r1.getOhmage() + r2.getOhmage());
|
var thesvg = `<svg width="${maxWidth}" height="${maxHeight}" viewbox="0 0 ${maxWidth} ${maxHeight}">
|
||||||
this.left = r1;
|
<line x1="${midPoint}" x2="${midPoint}" y1="${0}" y2="${25}" stroke="black"/>
|
||||||
this.right = r2;
|
<line x1="${midPoint}" x2="${midPoint}" y1="${maxHeight - 25}" y2="${maxHeight}" stroke="black"/>
|
||||||
}
|
<g transform="translate(0, 25)">
|
||||||
size() {
|
${svg}
|
||||||
return this.left.size() + this.right.size();
|
</g>
|
||||||
}
|
</svg>`
|
||||||
prettyString(depth = 0) {
|
document.getElementsByTagName("body")[0].innerHTML = thesvg;
|
||||||
var leftString = this.left.prettyString(depth + 1)
|
|
||||||
var leftString = this.left.constructor.name === "SeriesResistor" ? `${this.left.left.prettyString(depth + 1)}\n${this.left.right.prettyString(depth + 1)}` : this.left.prettyString(depth + 1);
|
|
||||||
var rightString = this.right.constructor.name === "SeriesResistor" ? `${this.right.left.prettyString(depth + 1)}\n${this.right.right.prettyString(depth + 1)}` : this.right.prettyString(depth + 1);
|
|
||||||
return `${'--'.repeat(depth)}${this.toString()}\n${leftString}\n${rightString}`;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
class ParallelResistor extends Resistor {
|
|
||||||
left
|
|
||||||
right
|
|
||||||
constructor(r1, r2) {
|
|
||||||
super(1 / ((1/r1.getOhmage()) + (1/r2.getOhmage())));
|
|
||||||
this.left = r1;
|
|
||||||
this.right = r2;
|
|
||||||
}
|
|
||||||
size() {
|
|
||||||
return this.left.size() + this.right.size();
|
|
||||||
}
|
|
||||||
prettyString(depth = 0) {
|
|
||||||
return `${'--'.repeat(depth)}${this.toString()}
|
|
||||||
${this.left.prettyString(depth + 1)}
|
|
||||||
${this.right.prettyString(depth + 1)}`
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -81,49 +59,18 @@ function harm(list) {
|
|||||||
list.forEach(e => temp += 1 / e);
|
list.forEach(e => temp += 1 / e);
|
||||||
return 1 / temp;
|
return 1 / temp;
|
||||||
}
|
}
|
||||||
|
function flatten(list, flattable) {
|
||||||
|
return list.reduce( (acc, cur) => acc.concat(cur.constructor.name === flattable ? cur.children : [cur]), []);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class NestedResistor extends Resistor {
|
class NestedResistor extends Resistor {
|
||||||
children = []
|
children = []
|
||||||
|
|
||||||
constructor(l, func) {
|
constructor(l, func, instance) {
|
||||||
super(func(l.map(e => e.getOhmage())));
|
super(func(l.map(e => e.getOhmage())));
|
||||||
this.func = func
|
this.func = func
|
||||||
this.children = l;
|
this.children = flatten(l, instance.constructor.name);
|
||||||
}
|
|
||||||
cost() {
|
|
||||||
return func(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 NewParallelResistor.smart(this, other);
|
|
||||||
}
|
|
||||||
series(other) {
|
|
||||||
return NewSeriesResistor.smart(this, other);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
class NewSeriesResistor extends Resistor {
|
|
||||||
children = []
|
|
||||||
|
|
||||||
static smart(...list) {
|
|
||||||
var thelist = []
|
|
||||||
list.forEach(e => {
|
|
||||||
if (e.constructor.name === "NewSeriesResistor") {
|
|
||||||
e.children.forEach(e => thelist.push(e));
|
|
||||||
} else {
|
|
||||||
thelist.push(e);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return new NewSeriesResistor(thelist);
|
|
||||||
}
|
|
||||||
constructor(l) {
|
|
||||||
super(sum(l.map(e => e.getOhmage())));
|
|
||||||
this.children = l;
|
|
||||||
}
|
}
|
||||||
cost() {
|
cost() {
|
||||||
return sum(this.children.map(e => e.cost()));
|
return sum(this.children.map(e => e.cost()));
|
||||||
@ -133,49 +80,83 @@ class NewSeriesResistor extends Resistor {
|
|||||||
return `${'--'.repeat(depth)}${this.toString()}\n${childrenStrings}`
|
return `${'--'.repeat(depth)}${this.toString()}\n${childrenStrings}`
|
||||||
}
|
}
|
||||||
parallel(other) {
|
parallel(other) {
|
||||||
return NewParallelResistor.smart(this, other);
|
return new ParallelResistor(this, other);
|
||||||
}
|
}
|
||||||
series(other) {
|
series(other) {
|
||||||
return NewSeriesResistor.smart(this, other);
|
return new SeriesResistor(this, other);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class NewParallelResistor extends Resistor {
|
class SeriesResistor extends NestedResistor {
|
||||||
children = []
|
constructor(...l) {
|
||||||
|
super(l, sum, "SeriesResistor");
|
||||||
static smart(...list) {
|
}
|
||||||
var thelist = []
|
getSVG() {
|
||||||
list.forEach(e => {
|
var svgs = this.children.map(e => e.getSVG()); // triplets [svg, width, height]
|
||||||
if (e.constructor.name === "NewParallelResistor") {
|
var n = svgs.length;
|
||||||
e.children.forEach(e => thelist.push(e));
|
var maxHeight = sum(svgs.map(([svg, width, height]) => height)) + 25 * (n - 1)
|
||||||
} else {
|
var maxWidth = Math.max(...svgs.map(([svg, width, height]) => width))
|
||||||
thelist.push(e);
|
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 = `<g transform="translate(${xStart}, ${yOffset})">${svg}</g>`
|
||||||
|
if (i < n - 1)
|
||||||
|
partialInnerSVG += `<line x1="${centerLine}" x2="${centerLine}" y1="${yOffset + height}" y2="${yOffset + height + 25}" stroke="black"/>`
|
||||||
|
// finally
|
||||||
|
yOffset += 25 + height
|
||||||
|
i ++;
|
||||||
|
innerSVGs.push(partialInnerSVG)
|
||||||
});
|
});
|
||||||
return new NewParallelResistor(thelist);
|
var svg = innerSVGs.join("")
|
||||||
|
return [svg, maxWidth, maxHeight]
|
||||||
}
|
}
|
||||||
constructor(l) {
|
}
|
||||||
super(harm(l.map(e => e.getOhmage())));
|
|
||||||
this.children = l;
|
class ParallelResistor extends NestedResistor {
|
||||||
|
constructor(...l) {
|
||||||
|
super(l, harm, "ParallelResistor");
|
||||||
}
|
}
|
||||||
cost() {
|
getSVG() {
|
||||||
return sum(this.children.map(e => e.cost()));
|
var svgs = this.children.map(e => e.getSVG()); // triplets [svg, width, height]
|
||||||
}
|
var n = svgs.length;
|
||||||
prettyString(depth = 0) {
|
var maxHeight = Math.max(...svgs.map(([svg, width, height]) => height)) + 2 * 25
|
||||||
var childrenStrings = this.children.map(c => c.prettyString(depth + 1)).join("\n")
|
var maxWidth = sum(svgs.map(([svg, width, height]) => width)) + 25 * (n - 1)
|
||||||
return `${'--'.repeat(depth)}${this.toString()}\n${childrenStrings}`
|
var bottomLineStart = svgs[0][1] / 2
|
||||||
}
|
var bottomLineEnd = maxWidth - svgs[n - 1][1] / 2
|
||||||
parallel(other) {
|
var xOffset = 0; // where to start the next boundin
|
||||||
return NewParallelResistor.smart(this, other);
|
|
||||||
}
|
var innerSVGs = []
|
||||||
series(other) {
|
svgs.forEach(([svg, width, height]) => {
|
||||||
return NewSeriesResistor.smart(this, other);
|
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 = `<g transform="translate(${xOffset}, ${antennaOneEnd})">${svg}</g>
|
||||||
|
<line x1="${antennaX}" x2="${antennaX}" y1="${antennaOneStart}" y2="${antennaOneEnd}" stroke="black"/>
|
||||||
|
<line x1="${antennaX}" x2="${antennaX}" y1="${antennaTwoStart}" y2="${antennaTwoEnd}" stroke="black"/> `
|
||||||
|
// finally
|
||||||
|
xOffset += 25 + width
|
||||||
|
innerSVGs.push(partialInnerSVG)
|
||||||
|
});
|
||||||
|
var innerSVG = innerSVGs.join("")
|
||||||
|
var svg = `<line x1="${bottomLineStart}" x2="${bottomLineEnd}" y1="0" y2="0" stroke="black"/>
|
||||||
|
<line x1="${bottomLineStart}" x2="${bottomLineEnd}" y1="${maxHeight}" y2="${maxHeight}" stroke="black"/>
|
||||||
|
${innerSVG}`
|
||||||
|
return [svg, maxWidth, maxHeight]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
var resistors = [100]
|
var resistors = [100, 1000]
|
||||||
var N = 4
|
var N = 4
|
||||||
var maxValue = Math.max(...resistors)
|
var maxValue = Math.max(...resistors)
|
||||||
|
|
||||||
@ -202,8 +183,8 @@ function iterSet() {
|
|||||||
addition = new Set()
|
addition = new Set()
|
||||||
set.forEach(e1 => {
|
set.forEach(e1 => {
|
||||||
set.forEach(e2 => {
|
set.forEach(e2 => {
|
||||||
r1 = NewSeriesResistor.smart(e1, e2);
|
r1 = new SeriesResistor(e1, e2);
|
||||||
r2 = NewParallelResistor.smart(e1, e2);
|
r2 = new ParallelResistor(e1, e2);
|
||||||
addition.add(r1);
|
addition.add(r1);
|
||||||
addition.add(r2);
|
addition.add(r2);
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user