Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

refactor the reductions tools to use the new class inheritance. also …

…make series reduction call out to a new prettify resistor function that is more general and flexible. also make parallel reduction call out to prettify_resistor. closes #13.
  • Loading branch information...
commit b1671dc777bc5346d37a84d6ea065e99d2d0d794 1 parent 73b0b91
Daniel Thornton authored

Showing 2 changed files with 530 additions and 320 deletions. Show diff stats Hide diff stats

  1. +37 5 web/js/board.js
  2. +493 315 web/js/reductions.js
42 web/js/board.js
@@ -10,14 +10,14 @@ $(function() {
10 10 new Serializer(b);
11 11 new Deserializer(b);
12 12
13   - new RemoveTool(b);
14 13 new Undo(b);
15 14 new Redo(b);
16 15
17   - new SeriesTool(b);
18   - new ParallelTool(b);
19   -// new DeltaWyeTool(b);
20   -// new WyeDeltaTool(b);
  16 + new SeriesReduction(b);
  17 + new ParallelReduction(b);
  18 + new RemoveReduction(b);
  19 + new DeltaWyeReduction(b);
  20 + new WyeDeltaReduction(b);
21 21
22 22 b.set_tool(t);
23 23 });
@@ -595,6 +595,38 @@ var ProtoWire = ScreenObject.extend({
595 595 return distance < fuzzy_r;
596 596 },
597 597
  598 + nodes: function(self) {
  599 + return [self.n1, self.n2];
  600 + },
  601 +
  602 + // reassociate this element from the current n1 to n
  603 + n1_migrate: function(self, n) {
  604 + var index
  605 + if (self.n1.elements1) {
  606 + index = self.n1.elements1.indexOf(self);
  607 + if (index != -1) {
  608 + self.n1.elements1.splice(index, 1);
  609 + }
  610 + }
  611 +
  612 + n.elements1.push(self);
  613 + self.n1 = n;
  614 + },
  615 +
  616 + // reassociate this element from the current n2 to n
  617 + n2_migrate: function(self, n) {
  618 + var index
  619 + if (self.n2.elements2) {
  620 + index = self.n2.elements2.indexOf(self);
  621 + if (index != -1) {
  622 + self.n2.elements2.splice(index, 1);
  623 + }
  624 + }
  625 +
  626 + n.elements2.push(self);
  627 + self.n2 = n;
  628 + },
  629 +
598 630 remove: function(self) {
599 631 var index
600 632 index = self.board.elements.indexOf(self);
808 web/js/reductions.js
... ... @@ -1,386 +1,564 @@
1   -function SeriesTool(board) {
2   - this.type = 'series_tool';
3   - this.board = board;
  1 +var Reduction = Class.extend({
  2 + type: "reduction",
  3 + name: "Reduce",
  4 +
  5 + init: function(self, board) {
  6 + self.board = board;
  7 + self.make_elem();
  8 + },
  9 +
  10 + // @always:
  11 + // add a button to the reduction section for the given type of reduction
  12 + make_elem: function(self) {
  13 + self.elem = $('<div class="button" id="tool_series">'.concat(self.name.concat('</div>')))
  14 + .appendTo('#reductions')
  15 + .bind('click', function() {
  16 + self.reduce(self.board.selected_resistors());
  17 + });
  18 + },
  19 +
  20 + /*
  21 + @always:
  22 + logs the function call to the console
  23 + @args:
  24 + resistors: array of resistors to attempt to apply the reduction to
  25 + @return:
  26 + invalid reduction:
  27 + undef
  28 + valid reduction:
  29 + nodes[] : an array of edge nodes to build the new graph on
  30 + */
  31 + validate: function(self, resistors) {
  32 + console.log(self.type.concat(" validate"));
  33 + },
  34 +
  35 + /*
  36 + @always:
  37 + logs the funciton call to the console
  38 + pushes reduction to action stack
  39 + @args:
  40 + resistors: array of resistors to attempt to apply the reduction to
  41 + @return:
  42 + reduction failed:
  43 + false
  44 + reduction succeeded:
  45 + true
  46 + */
  47 + reduce: function(self, resistors) {
  48 + console.log(self.type.concat(" reduce"));
  49 + },
  50 +});
  51 +
  52 +// tool for series reductions
  53 +var SeriesReduction = Reduction.extend({
  54 + type: "series-reduction",
  55 + name: "Series",
  56 +
  57 + init: function(self, board) {
  58 + self._super(board);
  59 + },
  60 +
  61 + reduce: function series_reduce (self, resistors) {
  62 + var i, j; // iterator
  63 + var flag; // boolean
  64 + var r, s; // resistor
  65 + var res = []; // resistor array
  66 + var n0, n1, n2; // nodes
  67 + var nodes = []; // nodes array
  68 +
  69 + self._super(resistors);
  70 +
  71 + if (self.validate(resistors)) {
  72 + r = resistors[0];
  73 + for (i = 1; i < resistors.length; i++) {
  74 + s = resistors[i];
  75 + if (s.type == 'resistor') {
  76 + r.resistance += s.resistance;
  77 + new Wire(self.board, s.n1, s.n2);
  78 + s.remove();
  79 + }
  80 + }
  81 + prettify_resistor(r);
  82 + self.board.undoAdd();
  83 + return true;
  84 + } else {
  85 + return false;
  86 + }
  87 + },
  88 +
  89 + validate: function series_test (self, resistors) {
  90 + var i, j; // iterator
  91 + var nodes = []; // node array
  92 + var connected = [], uncleared = []; // resistor array
  93 + var r; // resistor
  94 + var n; // node
  95 + var flag; // boolean
4 96
5   - var node_tool = this;
  97 + self._super(resistors);
6 98
7   - this.elem = $('<div class="button" id="tool_series">Series</div>')
8   - .appendTo('#reductions')
9   - .bind('click', function() {
10   - series_reduce(board.selected_resistors());
11   - });
12   -}
13   -function ParallelTool(board) {
14   - this.type = 'parallel_tool';
15   - this.board = board;
  99 + uncleared = [];
  100 + for (i = 0; i < resistors.length; i++) {
  101 + uncleared.push(resistors[i]);
  102 + }
16 103
17   - var node_tool = this;
  104 + nodes = uncleared.pop().nodes();
  105 +
  106 + for (i = 0; i < nodes.length; i++) {
  107 + n = nodes[i];
  108 + flag = true;
  109 + while (flag && (uncleared.length > 0)) {
  110 + flag = false;
  111 + connected = n.resistors();
  112 + if (connected.length == 2) {
  113 + for (j = 0; j < connected.length; j++) {
  114 + r = connected[j];
  115 + index = uncleared.indexOf(r)
  116 + if (index != -1) {
  117 + uncleared.splice(index, 1);
  118 + if (n.wired(r.n1)) {
  119 + n = r.n2;
  120 + } else {
  121 + n = r.n1;
  122 + }
  123 + flag = true;
  124 + break;
  125 + }
  126 + }
  127 + }
  128 + }
  129 + nodes[i] = n; // commit the end node back to the nodes array
  130 + }
  131 + if (uncleared.length > 0) {
  132 + return undef;
  133 + } else {
  134 + return nodes;
  135 + }
  136 + },
  137 +});
18 138
19   - this.elem = $('<div class="button" id="tool_parallel">Parallel</div>')
20   - .appendTo('#reductions')
21   - .bind('click', function() {
22   - parallel_reduce(board.selected_resistors());
23   - });
24   -}
25   -function RemoveTool(board) {
26   - this.type = 'remove_tool';
27   - this.board = board;
  139 +var ParallelReduction = Reduction.extend({
  140 + type: "parallel-reduction",
  141 + name: "Parallel",
28 142
29   - var node_tool = this;
  143 + init: function(self, board) {
  144 + self._super(board);
  145 + },
30 146
31   - this.elem = $('<div class="button" id="tool_remove">Remove</div>')
32   - .appendTo('#reductions')
33   - .bind('click', function() {
34   - resistor_reduce(board.selected_resistors());
35   - });
36   -}
37   -function DeltaWyeTool(board) {
38   - this.type = 'delta_wye__tool';
39   - this.board = board;
  147 + reduce: function(self, resistors) {
  148 + var nodes; // node array
  149 + var i; // iterator
  150 + var r, s; // resistor
40 151
41   - var node_tool = this;
  152 + self._super(resistors);
42 153
43   - this.elem = $('<div class="button" id="tool_delta_wye">Delta-Wye</div>')
44   - .appendTo('#reductions')
45   - .bind('click', function() {
46   - series_reduce(board.selected_resistors());
47   - });
48   -}
49   -function WyeDeltaTool(board) {
50   - this.type = 'wye_delta_tool';
51   - this.board = board;
  154 + nodes = self.validate(resistors);
  155 + if (nodes) {
  156 + r = resistors[0];
  157 + for (i = 1; i < resistors.length; i++) {
  158 + s = resistors[i];
  159 + if (s.type == 'resistor') {
  160 + r.resistance = ((r.resistance*s.resistance)/(r.resistance+s.resistance));
  161 + s.remove();
  162 + }
  163 + }
  164 + prettify_resistor(r);
  165 + self.board.undoAdd();
  166 + return true;
  167 + } else {
  168 + return false;
  169 + }
  170 + },
52 171
53   - var node_tool = this;
  172 + validate: function(self, resistors) {
  173 + var i; // iterator
  174 + var nodes = [], uncleared = []; // node array
  175 + var n; // node
  176 + var r; // resistor
54 177
55   - this.elem = $('<div class="button" id="tool_wye_delta">Wye-Delta</div>')
56   - .appendTo('#reductions')
57   - .bind('click', function() {
58   - series_reduce(board.selected_resistors());
59   - });
60   -}
  178 + self._super(resistors);
61 179
62   -// returns true if valid, false if invalid
63   -function series_test (resistors) {
64   - console.log("series_test");
65   - var i, j; // iterators
66   - var nodes = []; // node array
67   - var connected = [], uncleared = []; // resistor array
68   - var r; // resistor
69   - var n; // node
70   - var flag; // boolean
  180 + nodes = resistors[0].nodes();
  181 + n = nodes[0];
  182 +
  183 + for (i = 1; i < resistors.length; i++) {
  184 + r = resistors[i];
  185 + if (n.wired(r.n1)) {
  186 + uncleared.push(r.n2);
  187 + } else if (n.wired(r.n2)) {
  188 + uncleared.push(r.n1);
  189 + } else {
  190 + console.log("circuit was not found to be valid parallel circuit");
  191 + return false;
  192 + }
  193 + }
  194 + for (i = 0; i < uncleared.length; i++) {
  195 + n = uncleared[i];
  196 + if (! nodes[1].wired(n)) {
  197 + console.log("circuit was not found to be valid parallel circuit");
  198 + return false;
  199 + }
  200 + }
  201 + console.log("circuit found to be valid parallel circuit");
  202 + return true;
  203 + },
  204 +});
71 205
72   - uncleared = [];
73   - for (i = 0; i < resistors.length; i++) {
74   - uncleared.push(resistors[i]);
75   - }
  206 +var RemoveReduction = Reduction.extend({
  207 + type: "remove-reduction",
  208 + name: "Remove",
76 209
  210 + init: function(self, board) {
  211 + self._super(board);
  212 + },
77 213
78   - r = uncleared.pop();
79   - nodes = [r.n1, r.n2];
  214 + reduce: function(self, resistors) {
  215 + var nodes; // node array
  216 + var i;
80 217
81   - for (i = 0; i < nodes.length; i++) {
82   - n = nodes[i];
83   - flag = true;
84   - while (flag && (uncleared.length > 0)) {
85   - flag = false;
86   - connected = n.resistors();
87   - if (connected.length == 2) {
88   - for (j = 0; j < connected.length; j++) {
89   - r = connected[j];
90   - index = uncleared.indexOf(r)
91   - if (index != -1) {
92   - uncleared.splice(index, 1);
93   - if (n.wired(r.n1)) {
94   - n = r.n2;
95   - } else {
96   - n = r.n1;
97   - }
98   - flag = true;
99   - break;
100   - }
  218 + self._super(resistors);
  219 +
  220 + nodes = self.validate(resistors);
  221 + if (nodes) {
  222 + for (i = 0; i < resistors.length; i++) {
  223 + if (resistors[i].type == 'resistor') {
  224 + resistors[i].remove();
101 225 }
102 226 }
  227 + self.board.undoAdd();
  228 + return true;
  229 + } else {
  230 + return false;
103 231 }
104   - nodes[i] = n; // commit the end node back to the nodes array
105   - }
106   - if (uncleared.length > 0) {
107   - return undef;
108   - } else {
109   - return nodes;
110   - }
111   -}
  232 + },
112 233
113   -function series_collision (nodes, resistors) {
114   - var i; // iterator
  234 + validate: function(self, resistors) {
  235 + self._super(resistors);
  236 + return true;
  237 + },
  238 +});
  239 +
  240 +var DeltaWyeReduction = Reduction.extend({
  241 + type: "delta-wye-reduction",
  242 + name: "Delta-Wye",
  243 +
  244 + init: function(self, board) {
  245 + self._super(board);
  246 + },
  247 +
  248 + reduce: function(self, resistors) {
  249 + var nodes; // node array
  250 +
  251 + self._super(resistors);
  252 +
  253 + nodes = self.validate(resistors);
  254 + if (nodes) {
  255 + self.board.undoAdd();
  256 + return true;
  257 + } else {
  258 + return false;
  259 + }
  260 + },
  261 +
  262 + validate: function(self, resistors) {
  263 + self._super(resistors);
  264 + },
  265 +});
  266 +
  267 +var WyeDeltaReduction = Reduction.extend({
  268 + type: "wye-delta-reduction",
  269 + name: "Wye-Delta",
  270 +
  271 + init: function(self, board) {
  272 + self._super(board);
  273 + },
  274 +
  275 + reduce: function(self, resistors) {
  276 + var nodes; // node array
  277 +
  278 + self._super(resistors);
  279 +
  280 + nodes = self.validate(resistors);
  281 + if (nodes) {
  282 + self.board.undoAdd();
  283 + return true;
  284 + } else {
  285 + return false;
  286 + }
  287 + },
  288 +
  289 + validate: function(self, resistors) {
  290 + var i, j; // iterators
  291 + var nodes = []; // node array
  292 + var n; // node
  293 + var r; // resistor
  294 + var flag; // boolean
  295 +
  296 + self._super(resistors);
  297 +
  298 + if (resistors.length != 3) {
  299 + return false
  300 + }
  301 +
  302 + nodes = resistors[0].nodes();
  303 +
  304 + for (i = 0; i < nodes.length; i++) {
  305 + n = nodes[i];
  306 + flag = true;
  307 + if (n.resistors().length == 3) {
  308 + for (j = 0; j < resistors.length; j++) {
  309 + r = resistors[j]
  310 + if (! ( r.n1.wired(n) || r.n2.wired(n))) {
  311 + flag = false;
  312 + }
  313 + }
  314 + if (flag) {
  315 + // collect the outside nodes for the new graph
  316 + return nodes;
  317 + }
  318 + }
  319 + }
  320 + return false;
  321 + },
  322 +});
  323 +
  324 +
  325 +/*
  326 +@args:
  327 + a: node, or object with a.x and a.y
  328 + b: node, or object with a.x and a.y
  329 + ignore_elements: a list of elements to ignore collisions from
  330 +@return:
  331 + collision:
  332 + true
  333 + no-collision:
  334 + false
  335 +*/
  336 +function prettify_collision(a, b, ignore_elements) {
  337 + var i, j; // iterator
115 338 var e; // element
116   - var elementsi = []; // element array
117   - var nodes_in_circuit = [], all_nodes = []; // node array
  339 + var n; // node
  340 + var board_elements = []; // element array
  341 + var nodes = [], ignore_nodes = [], board_nodes = []; // node array
118 342 var lx, ly, rx, ry, distance, index; // integers
  343 + console.log("prettify_collision");
119 344
120   - // find all nodes connected to an element in resistors
121   - for (i = 0; i < resistors.length; i++) {
122   - e = resistors[i];
123   - if (nodes_in_circuit.indexOf(e.n1) == -1) {
124   - nodes_in_circuit.push(e.n1);
125   - }
126   - if (nodes_in_circuit.indexOf(e.n2) == -1) {
127   - nodes_in_circuit.push(e.n2);
  345 + // find all nodes connected to an element in elements
  346 + for (i = 0; i < ignore_elements.length; i++) {
  347 + e = ignore_elements[i];
  348 + nodes = e.nodes();
  349 + for (j = 0; j < nodes.length; j++) {
  350 + if (ignore_nodes.indexOf(nodes[j]) == -1) {
  351 + ignore_nodes.push(nodes[j]);
  352 + }
128 353 }
129 354 }
130 355
131   - //find any nodes that collide with the line from nodes[0] to nodes[1]
132   - all_nodes = nodes[0].board.nodes;
133   - for (i = 0; i < all_nodes.length; i++) {
134   - n = all_nodes[i];
135   - index = nodes_in_circuit.indexOf(n);
136   - if (index == -1) {
  356 + //find any nodes that collide with the line from a to b
  357 + board_nodes = a.board.nodes;
  358 + for (i = 0; i < board_nodes.length; i++) {
  359 + n = board_nodes[i];
  360 + if (ignore_nodes.indexOf(n) == -1) {
137 361 // This magic geometry is from http://mathworld.wolfram.com/Point-LineDistance2-Dimensional.html
138   - lx = nodes[1].x - nodes[0].x;
139   - ly = nodes[1].y - nodes[0].y;
140   - rx = nodes[0].x - n.x;
141   - ry = nodes[0].y - n.y;
  362 + lx = b.x - a.x;
  363 + ly = b.y - a.y;
  364 + rx = a.x - n.x;
  365 + ry = a.y - n.y;
142 366
143 367 distance = Math.abs((lx * ry) - (ly * rx)) / Math.sqrt(lx * lx + ly * ly);
144 368 if (distance < 5) {
  369 + console.log("node collision detected");
145 370 return true;
146 371 }
147 372 }
148 373 }
149 374
150   - elements = nodes[0].elements();
151   - for (i = 0; i < elements.length; i++){
152   - e = elements[i];
153   - if (((e.n1 === nodes[0]) && (e.n2 === nodes[1])) ||
154   - ((e.n2 === nodes[0]) && (e.n1 === nodes[1]))) {
155   - if (resistors.indexOf(e) == -1) {
  375 + board_elements = a.board.elements;
  376 + for (i = 0; i < board_elements.length; i++){
  377 + e = board_elements[i];
  378 + if (((e.n1 === a) && (e.n2 === b)) ||
  379 + ((e.n2 === a) && (e.n1 === b))) {
  380 + if (ignore_elements.indexOf(e) == -1) {
  381 + console.log("element collision detected");
156 382 return true;
157 383 }
158 384 }
159 385 }
160 386
  387 + console.log("clear to prettify");
161 388 return false;
162 389 }
163 390
164   -// returns true if valid and successfully transformed, and false if invalid or unsuccessful
165   -function series_reduce (resistors) {
166   - console.log("series_reduce");
  391 +/*
  392 +@args:
  393 + resistor: a single resistor that may need the wires around it cleaned
  394 +@return:
  395 + no pretty-fying occured:
  396 + false
  397 + pretty-fying occured:
  398 + true
  399 +*/
  400 +function prettify_resistor(resistor) {
167 401 var i, j; // iterator
168 402 var flag; // boolean
169   - var r, s; // resistor
170   - var res = []; // resistor array
171   - var n0, n1, n2; // nodes
172   - var nodes = []; // nodes array
173   -
174   - nodes = series_test(resistors);
175   - if (nodes) {
176   - if (series_collision(nodes, resistors)) {
177   - n0 = nodes[0];
178   - n2 = nodes[1];
179   - res = n0.resistors();
180   - for (j = 0; j < res.length; j ++) {
181   - if (resistors.indexOf(res[j]) != -1) {
182   - if (res[j].n1 === n0) {
183   - n1 = res[j].n2;
184   - } else {
185   - n1 = res[j].n1;
186   - }
187   - break;
188   - }
189   - }
190   - flag = series_collision([n1, n2], resistors);
191   - if (flag) {
192   - res = n2.resistors();
193   - for (j = 0; j < res.length; j ++) {
194   - if (resistors.indexOf(res[j]) != -1) {
195   - if (res[j].n1 === n2) {
196   - n1 = res[j].n2;
  403 + var n; // node
  404 + var nodes = []; // node array
  405 + var e; // element
  406 + var elements = []; // element array
  407 + var reduction_elements = [[],[]]; // array of element arrays
  408 +
  409 + nodes = resistor.nodes();
  410 + for (i = 0; i < nodes.length; i++) {
  411 + n = nodes[i];
  412 + prettify_trim_wire(n);
  413 +
  414 + e = resistor;
  415 + while (true) {
  416 + elements = n.elements();
  417 + if (elements.length == 2) {
  418 + flag = false;
  419 + for (j = 0; j < elements.length; j++) {
  420 + if (! (elements[j] === e)) {
  421 + if (! (elements[j].type == "wire")) {
  422 + flag = true;
197 423 } else {
198   - n1 = res[j].n1;
  424 + e = elements[j];
  425 + reduction_elements[i].push(e);
  426 + if (e.n1 === n) {
  427 + n = e.n2;
  428 + } else {
  429 + n = e.n1;
  430 + }
199 431 }
200   - break;
201 432 }
202 433 }
203   - flag = series_collision([n0, n1], resistors);
204   - }
205   - if (flag) {
206   - r = resistors[0];
207   - for (i = 1; i < resistors.length; i++) {
208   - s = resistors[i];
209   - if (s.type == 'resistor') {
210   - r.resistance += s.resistance;
211   - new Wire(s.board, s.n1, s.n2);
212   - s.remove();
213   - }
  434 + if (flag) {
  435 + break;
214 436 }
215 437 } else {
216   - nodes.push(n1);
217   - r = new Resistor(resistors[0].board, n0, n1, 0);
218   - for (i = 0; i < resistors.length; i++) {
219   - s = resistors[i];
220   - if (s.type == 'resistor') {
221   - r.resistance += s.resistance;
222   - }
223   - s.remove();
224   - if (nodes.indexOf(s.n1) == -1) {
225   - s.n1.remove();
226   - }
227   - if (nodes.indexOf(s.n2) == -1) {
228   - s.n2.remove();
229   - }
230   - }
231   - new Wire(resistors[0].board, n1, n2);
232   - }
233   - } else {
234   - r = new Resistor(resistors[0].board, nodes[0], nodes[1], 0);
235   - for (i = 0; i < resistors.length; i++) {
236   - s = resistors[i];
237   - if (s.type == 'resistor') {
238   - r.resistance += s.resistance;
239   - }
240   - s.remove();
241   - if (nodes.indexOf(s.n1) == -1) {
242   - s.n1.remove();
243   - }
244   - if (nodes.indexOf(s.n2) == -1) {
245   - s.n2.remove();
246   - }
  438 + break;
247 439 }
248 440 }
249   - r.board.undoAdd();
250   - return true;
251   - } else {
252   - return false;
253   - }
254   -}
255   -
256   -// returns true if valid, false if invalid
257   -function parallel_test (resistors) {
258   - console.log("parallel_test");
259   - var i; // iterator
260   - var nodes = [], uncleared = []; // node array
261   - var n; // node
262   - var r; // resistor
263 441
264   - r = resistors[0];
265   - nodes = [r.n1, r.n2];
266   - n = nodes[0];
  442 + nodes[i] = n;
  443 + }
267 444
268   - for (i = 1; i < resistors.length; i++) {
269   - r = resistors[i];
270   - if (n.wired(r.n1)) {
271   - uncleared.push(r.n2);
272   - } else if (n.wired(r.n2)) {
273   - uncleared.push(r.n1);
  445 + while (true) {
  446 + flag = prettify_collision(nodes[0], nodes[1],
  447 + reduction_elements[0].concat(reduction_elements[1]).concat([resistor]));
  448 + if (! flag) {
  449 + break;
274 450 } else {
275   - console.log("circuit was not found to be valid parallel circuit");
276   - return true;
277   - }
278   - }
279   - for (i = 0; i < uncleared.length; i++) {
280   - n = uncleared[i];
281   - if (! nodes[1].wired(n)) {
282   - console.log("circuit was not found to be valid parallel circuit");
283   - return true;
284   - }
285   - }
286   - console.log("circuit found to be valid parallel circuit");
287   - return true;
288   -}
  451 + //bring nodes[0] in one
  452 + n = nodes[0];
  453 + e = reduction_elements[0].pop();
  454 + if (e) {
  455 + if (e.n1 === nodes[0]) {
  456 + nodes[0] = e.n2;
  457 + } else {
  458 + nodes[0] = e.n1;
  459 + }
  460 + }
289 461
290   -// returns true if valid and successfully transformed, and false if invalid or unsuccessful
291   -function parallel_reduce (resistors) {
292   - console.log("parallel_reduce");
293   - var i;
294   - var r, s;
  462 + flag = prettify_collision(nodes[0], nodes[1],
  463 + reduction_elements[0].concat(reduction_elements[1]).concat([resistor]));
  464 + if (! flag) {
  465 + break;
  466 + } else {
  467 + // push nodes[0] out one
  468 + if (e) {
  469 + reduction_elements[0].push(e);
  470 + nodes[0] = n;
  471 + }
295 472
296   - if (parallel_test(resistors)) {
297   - r = resistors[0];
298   - for (i = 1; i < resistors.length; i++) {
299   - s = resistors[i];
300   - if (s.type == 'resistor') {
301   - r.resistance = ((r.resistance*s.resistance)/(r.resistance+s.resistance));
302   - s.remove();
303   - }
304   - }
305   - return true;
306   - } else {
307   - return false;
308   - }
309   -}
310   -function resistor_test (resistors) {
311   - console.log("resistor_test");
312   - console.log("resistor not tested for reducability");
313   - return true;
314   - ;
315   -}
316   -function resistor_reduce (resistors) {
317   - console.log("resistor_reduce");
318   - var i;
  473 + // bring nodes[1] in one
  474 + e = reduction_elements[1].pop();
  475 + if (e) {
  476 + if (e.n1 === nodes[1]) {
  477 + nodes[1] = e.n2;
  478 + } else {
  479 + nodes[1] = e.n1;
  480 + }
  481 + }
319 482
320   - if (resistor_test(resistors)) {
321   - for (i = 0; i < resistors.length; i++) {
322   - if (resistors[i].type == 'resistor') {
323   - resistors[i].remove();
  483 + flag = prettify_collision(nodes[0], nodes[1],
  484 + reduction_elements[0].concat(reduction_elements[1]).concat([resistor]));
  485 + if (! flag) {
  486 + break;
  487 + } else {
  488 + // bring nodes[0] in one
  489 + e = reduction_elements[0].pop();
  490 + if (e) {
  491 + if (e.n1 === nodes[0]) {
  492 + nodes[0] = e.n2;
  493 + } else {
  494 + nodes[0] = e.n1;
  495 + }
  496 + }
  497 +
  498 + if ((reduction_elements[0].length == 0) && (reduction_elements[1].length == 0)) {
  499 + return false;
  500 + }
  501 + }
324 502 }
325 503 }
326   - return true;
327   - } else {
328   - return false;
329 504 }
330   -}
331   -function delta_wye_test (resistors) {
332   - console.log("delta_wye_test");
333   - ;
334   -}
335   -function delta_wye_reduce (resistors) {
336   - console.log("delta_wye_reduce");
337   - ;
338   -}
339 505
340   -// Returns the center node if valid, and false if invalid
341   -function wye_delta_test (resistors) {
342   - console.log("wye_delta_test");
343   - var i, j; // iterators
344   - var nodes = []; // node array
345   - var n; // node
346   - var r; // resistor
347   - var flag; // boolean
348   -
349   - if (resistors.length != 3) {
350   - return false
351   - }
352   -
353   - r = resistors[0];
354   - nodes = [r.n1, r.n2]
355   -
356   - for (i = 0; i < nodes.length; i++) {
357   - n = nodes[1];
358   - flag = true;
359   - if (n.resistors.length == 3) {
360   - for (i = 0; i < resistors.length; i++) {
361   - r = resistors[i]
362   - if ((! r.n1.wired(n)) && (! r.n2.connected)) {
363   - flag = false;
364   - }
  506 + resistor.selected = true;
  507 + resistor.n1_migrate(nodes[0]);
  508 + resistor.n2_migrate(nodes[1]);
  509 +
  510 + for (i = 0; i < reduction_elements.length; i++) {
  511 + elements = reduction_elements[i];
  512 + for (j = 0; j < elements.length; j++) {
  513 + e = elements[j];
  514 + e.remove();
  515 + if (nodes.indexOf(e.n1) == -1) {
  516 + e.n1.remove();
365 517 }
366   - if (flag) {
367   - return n;
  518 + if (nodes.indexOf(e.n2) == -1) {
  519 + e.n2.remove();
368 520 }
369 521 }
370 522 }
371   - return false;
  523 +}
372 524
  525 +/*
  526 +@args:
  527 + resistors: an array of *three* resistors that make up a delta circuit,
  528 + and may need wires around them cleaned
  529 +@return:
  530 + no pretty-fying occured:
  531 + false
  532 + pretty-fying occured:
  533 + true
  534 +*/
  535 +function prettify_delta(resistors) {
373 536 }
374 537
375   -// returns true if valid and successfully transformed, and false if invalid or unsuccessful
376   -function wye_delta_reduce (resistors) {
377   - console.log("wye_delta_reduce");
378   - var n = wye_delta_test(resistors); // false or the center node
  538 +/*
  539 +@args
  540 + resistors: an array of *three* resistors that make up a wye circuit,
  541 + and may need wires around them cleaned
  542 +@return:
  543 + no pretty-fying occured:
  544 + false
  545 + pretty-fying occured:
  546 + true
  547 +*/
  548 +function prettify_wye(resistors) {
  549 +}
379 550
380   - if (n != false) {
381   -// reduce;
382   - return true;
383   - } else {
384   - return false;
385   - }
  551 +/*
  552 +@args
  553 + n: a node whose wires may have extraneous branches to purge
  554 +@return:
  555 + no pretty-fying occured:
  556 + false
  557 + pretty-fying occured:
  558 + true
  559 +*/
  560 +function prettify_trim_wire(n) {
  561 + var i; // iterator
  562 +
  563 +
386 564 }

0 comments on commit b1671dc

Please sign in to comment.
Something went wrong with that request. Please try again.