Compare commits
6 Commits
23b48709db
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
562e2ea9dc | ||
|
|
7644a5cf30 | ||
| 215eb20a55 | |||
| 7364b2c4c5 | |||
|
|
32e10a3cf4 | ||
|
|
6facbd4c93 |
17
README.md
17
README.md
@@ -3,18 +3,29 @@ ResistorJS is a Javascript library intended to compose electronical resistors.
|
|||||||
|
|
||||||
## Using Resistors in Code
|
## Using Resistors in Code
|
||||||
In order to instantiate an 100 Ohm resistor, we simply call `new Resistor(100)`. In order to compose several resistors, the composite resistor types `SeriesResistor` and `ParallelResistor` are available.
|
In order to instantiate an 100 Ohm resistor, we simply call `new Resistor(100)`. In order to compose several resistors, the composite resistor types `SeriesResistor` and `ParallelResistor` are available.
|
||||||
```
|
```JS
|
||||||
var r1 = new Resistor(100);
|
var r1 = new Resistor(100);
|
||||||
var r2 = new Resistor(150);
|
var r2 = new Resistor(150);
|
||||||
// Combining resistors
|
// Combining resistors
|
||||||
var inSeries = r1.series(r2); // type: SeriesResistor
|
var inSeries = r1.series(r2); // type: SeriesResistor
|
||||||
var inParallel = r1.parallel(r2); // type: ParallelResistor
|
var inParallel = r1.parallel(r2); // type: ParallelResistor
|
||||||
|
|
||||||
|
// Computing resulting resistors
|
||||||
|
inSeries.getOhmage() // 250
|
||||||
|
inParallel.getOhmage() // 60
|
||||||
|
|
||||||
// Drawing to DOM
|
// Drawing to DOM
|
||||||
r1.draw(div1);
|
r1.draw(div1);
|
||||||
r2.draw(div2);
|
r2.draw(div2);
|
||||||
inSeries.draw(div3);
|
inSeries.draw(div3);
|
||||||
inParallel.draw(div4);
|
inParallel.draw(div4);
|
||||||
```
|
```
|
||||||
Calling `Resistor.draw(element)` generates an SVG from the constructed Resistor object. Above code would render something like this:
|
Calling `Resistor.draw(element)` generates an SVG from the constructed Resistor object. Above code will render something like this:
|
||||||

|

|
||||||
|
|
||||||
|
## Computing approximations
|
||||||
|
Sometimes, one wants to use ohmages in an electronical circuit that simply are not available. By cleverly combining a sufficient number of available resistors, one can approximate the needed values. For instance, using only 100Ohm and 100Ohm resistors, one can construct a (sufficiently close to) 127Ohm resistor like so:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
`main.js` contains an example application using dynamic programming that computes sufficiently good approximations for resistor values within a set range.
|
||||||
BIN
imgs/127ohm.png
BIN
imgs/127ohm.png
Binary file not shown.
|
Before Width: | Height: | Size: 2.6 MiB After Width: | Height: | Size: 2.3 MiB |
BIN
imgs/tutorial.png
Normal file
BIN
imgs/tutorial.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.0 MiB |
8
main.js
8
main.js
@@ -1,5 +1,9 @@
|
|||||||
import { Resistor, SeriesResistor, ParallelResistor } from './modules/resistors.js';
|
import { Resistor, SeriesResistor, ParallelResistor } from './modules/resistors.js';
|
||||||
|
|
||||||
|
window.Resistor = Resistor
|
||||||
|
window.SeriesResistor = SeriesResistor
|
||||||
|
window.ParallelResistor = ParallelResistor
|
||||||
|
|
||||||
var resistorValues = [100, 1000]
|
var resistorValues = [100, 1000]
|
||||||
var N = 4
|
var N = 4
|
||||||
var maxValue = Math.max(...resistorValues)
|
var maxValue = Math.max(...resistorValues)
|
||||||
@@ -41,6 +45,7 @@ console.log(`Parameters: N = ${N}, values = ${resistorValues} `);
|
|||||||
for (i = 0; i < N; i++)
|
for (i = 0; i < N; i++)
|
||||||
bottomUpDP();
|
bottomUpDP();
|
||||||
console.log("Finished computation of DP array");
|
console.log("Finished computation of DP array");
|
||||||
|
window.numberline = numberline
|
||||||
|
|
||||||
function missings() {
|
function missings() {
|
||||||
var missing = new Set()
|
var missing = new Set()
|
||||||
@@ -52,6 +57,3 @@ function missings() {
|
|||||||
}
|
}
|
||||||
console.log(`We found a composite resistor for every value in the range [1, ${N * maxValue + 1}] but for: ${[...missings()].join(",")}`)
|
console.log(`We found a composite resistor for every value in the range [1, ${N * maxValue + 1}] but for: ${[...missings()].join(",")}`)
|
||||||
|
|
||||||
numberline[420].draw() // Draw the Resistor diagram for 420 Ohm as SVG
|
|
||||||
|
|
||||||
console.log(numberline)
|
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ class Resistor {
|
|||||||
var [width, height] = [50, 100]
|
var [width, height] = [50, 100]
|
||||||
return [svg, width, height]
|
return [svg, width, height]
|
||||||
}
|
}
|
||||||
draw() {
|
draw(element = document.getElementsByTagName("body")[0]) {
|
||||||
var [svg, width, height] = this.getSVG();
|
var [svg, width, height] = this.getSVG();
|
||||||
var maxHeight = height + 2*25
|
var maxHeight = height + 2*25
|
||||||
var maxWidth = width
|
var maxWidth = width
|
||||||
@@ -47,7 +47,7 @@ class Resistor {
|
|||||||
${svg}
|
${svg}
|
||||||
</g>
|
</g>
|
||||||
</svg>`
|
</svg>`
|
||||||
document.getElementsByTagName("body")[0].innerHTML = thesvg;
|
element.innerHTML = thesvg;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -57,7 +57,7 @@ class NestedResistor extends Resistor {
|
|||||||
constructor(l, func, instance) {
|
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 = flatten(l, instance.constructor.name);
|
this.children = flatten(l, instance);
|
||||||
}
|
}
|
||||||
cost() {
|
cost() {
|
||||||
return sum(this.children.map(e => e.cost()));
|
return sum(this.children.map(e => e.cost()));
|
||||||
|
|||||||
Reference in New Issue
Block a user