Piotr Labudda 7 lat temu
rodzic
commit
b478d9d975
1 zmienionych plików z 32 dodań i 12 usunięć
  1. 32 12
      SE/se-lib/Route/Test/Neuron.php.view.js

+ 32 - 12
SE/se-lib/Route/Test/Neuron.php.view.js

@@ -8,10 +8,17 @@ if (!HTML_ID) throw "Missing HTML_ID";
 var DBG = DBG || false;
 var DBG1 = true;
 
-// <svg height="100" width="100">
-//     <circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" />
-// </svg>
+// Receptor is special Neuron which reads input (letters in this case)
+//  Receptor.ui: { cx, cy, r } // circle
+// Neuron: { charge, label, draw, source }
+//  Neuron.ui: { cx, cy, rx, ry } // eclipse
+//  Neuron.source: [ { neuron: Neuron | Receptor, atCharge }, ... ]
 
+// Receptor.neighbours: [] list of other receptors with max(receptors.charge)
+
+// makeNeuron cases:
+// stream: AAAAA and only A has charge then create Neuron over A and discharge and lower max charge
+// stream: AAAAA and receptor B has charge then create Neuron between A and B and discharge (closer to receptor with higher charge)
 
 var DEFAULT_CONFIG = {
 	ui_output_width: 800,
@@ -153,7 +160,7 @@ function state__fromOverchargedNeurons(state, overchargedNeurons) {
 			var alreadyExistNeuron = toChangeReceptor.next.filter(function (nextNeuron) {
 				return (nextNeuron.source.length === 1);
 			})
-			DBG1 && console.log("DBG:overcharge.2:", { alreadyExistNeuron });
+			DBG1 && console.log("DBG:overcharge.2:", { alreadyExistNeuron: Object.assign({}, alreadyExistNeuron) });
 			if (alreadyExistNeuron.length) {
 				alreadyExistNeuron[0].charge += toChangeReceptor.charge
 				toChangeReceptor.charge = 0
@@ -234,14 +241,31 @@ function makeNeuronFromOneNode(state, sourceNode) {
 }
 function makeNeuronFromTwoNodes(state, first, second) {
 	var value = '' + first.value + second.value;
+	var newNeuronX = (first.ui.cx + second.ui.cx) / 2 // center between nodes
+	{ // closer to node with more charge (first)
+		var xDiff = Math.abs(first.ui.cx - second.ui.cx)
+		var isFirstOnRight = (first.ui.cx > second.ui.cx)
+		// first.charge + second.charge -- xDiff
+		// second.charge -- x ==> x = second.charge * 100 / (first.charge + second.charge)
+		var xToFirst = (second.charge * 100) / (first.charge + second.charge)
+		var newNeuronX = first.ui.cx + (isFirstOnRight ? -1 : 1) * xToFirst
+	}
+	var newNeuronY = state.neuron.reduce(function (ret, neuron) {
+		if (newNeuronX < neuron.ui.cx - 10) return ret;
+		if (newNeuronX > neuron.ui.cx + 10) return ret;
+		if (ret < neuron.ui.cy - 10) return ret;
+		if (ret > neuron.ui.cy + 10) return ret;
+		return ret + 10 + state.ui_space_y;
+	}, first.ui.cy + 10 + state.ui_space_y)
+
 	return Object.assign({}, DEFAULT_NEURON, {
 		value: value,
 		charge: 0, // (first.charge + second.charge) / 2,
 		maxCharge: ((first.maxCharge + second.maxCharge) / 2) * state.config_discharge_max_in_new_neuron_from_one,
 		uiShape: "ellipse",
 		ui: {
-			cx: (first.ui.cx + second.ui.cx) / 2,
-			cy: first.ui.cy + 10 + state.ui_space_y,
+			cx: newNeuronX,
+			cy: newNeuronY,
 			rx: 10 * value.length,
 			ry: 10,
 		},
@@ -394,12 +418,7 @@ var NeuronView = createReactClass({
 				neuron: [],
 				doAnim: true,
 			}
-		), function () {
-			console.warn({
-				receptor: this.state.receptor.map(function (n) { return n }),
-				neuron: this.state.receptor.map(function (n) { return n }),
-			})
-		}.bind(this));
+		));
 
 		setTimeout(this.forwardAnim, this.state.config_read_speed)
 	},
@@ -734,6 +753,7 @@ var NeuronView = createReactClass({
 	}
 });
 
+
 ReactDOM.render(h(NeuronView, {
 	initialData: INITIAL_DATA || "",
 }), document.getElementById(HTML_ID))