Skip to content

Commit

Permalink
junctions added
Browse files Browse the repository at this point in the history
  • Loading branch information
espmaniac authored Sep 22, 2024
1 parent 1c41e9f commit fbc6c8b
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 8 deletions.
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ <h2 id="result">Answer: 42</h2>
<script src="parallelseries.js" defer></script>
<script src="component.js" defer></script>
<script src="main.js" defer></script>
<script src="junction.js" defer></script>
<script src="node.js" defer></script>
<script src="wire.js" defer></script>
</body>
Expand Down
43 changes: 43 additions & 0 deletions junction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@


class Junction {
constructor() {
this.x = null;
this.y = null;

this.nodes = [];
}

draw() {
ctx.save();
ctx.fillStyle = "#000000";
ctx.beginPath();

ctx.arc(this.x, this.y,
3, // radius
0, 2 * Math.PI
);

ctx.fill();
ctx.closePath();
ctx.restore();
}

hitTest(x,y) {
return (x === this.x && y === this.y) ? true : false;
}

onDelete() {
for (let i = 0; i < this.nodes.length; ++i) {
let node = this.nodes[i];
for (let j = 0; j < node.junctions.length; ++j)
if (node.junctions[j] === this) {
node.junctions.splice(j, 1);
break;
}
}

this.nodes = [];
}

}
125 changes: 117 additions & 8 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var components = [];
var selectedComponents = [];
var wires = [];
var selectedWires = [];
var junctions = [];

const cellSize = 20; // grid size

Expand Down Expand Up @@ -250,13 +251,16 @@ function renderAll() {
ctx.scale(zoom, zoom);
ctx.translate(offsetX, offsetY);

for (var i in components) {
for (var i in components)
components[i].draw();
}


for (let i = 0; i < wires.length; ++i)
wires[i].draw();


for (let i = 0; i < junctions.length; ++i)
junctions[i].draw();

}

Expand Down Expand Up @@ -287,8 +291,8 @@ function deleteSelected() {


for (let i = 0, j = 0; i < selectedWires.length; ++i) {
let index = selectedWires[i] - j;
wires[index].onDelete();
let index = selectedWires[i] - j;
wires[index].onDelete();
wires.splice(index, 1);

++j;
Expand Down Expand Up @@ -453,19 +457,49 @@ function connectComponentComponent(compn1, compn2) {
let c2leftNodePos = compn2.getNodeLeft();
let hitNodeLeft = compn1.hitNode(c2leftNodePos.x, c2leftNodePos.y);

if (hitNodeLeft !== null){
if (hitNodeLeft != null){
connectNodes(hitNodeLeft, compn2.node1);
}

let c2rightNodePos = compn2.getNodeRight();
let hitNodeRight = compn1.hitNode(c2rightNodePos.x, c2rightNodePos.y);

if (hitNodeRight !== null) {
if (hitNodeRight != null) {
connectNodes(hitNodeRight, compn2.node2);
}

}

function connectJunctionNode(j, n) {
for (let i = 0; i < n.junctions.length; ++i) {
if (n.junctions[i] === j) return;
}
n.junctions.push(j);
for (let i = 0; i < j.nodes.length; ++i) {
if (j.nodes[i] === n) return;
}
j.nodes.push(n);
}

function deleteJunction(j) {
if (!j) return;
for (let i = 0; i < junctions.length; ++i) {
if (junctions[i] === j) {
j.onDelete();
junctions.splice(i, 1);
return;
}
}
}

function junctionAt(x,y, jArr) {
for (let i = 0; i < jArr.length; ++i) {
if (jArr[i].hitTest(x,y)) return jArr[i];
}

return null;
}

function connectWireWire(wire, w) {
if (wire === w) return;

Expand All @@ -477,20 +511,95 @@ function connectWireWire(wire, w) {

if (hitRight) {
let node = w.hitNode(wire.x2, wire.y2); // whether the point wire.x2 wire.y2 lies on (w.x1 w.y1) || (w.x2 w.y2)
if (node != null)
if (node != null) {
connectNodes(wire.node2, node);

if ((node.connections.length - w.nodesOnLineCount) >= 3) { // junction
let hitJunctionNode = junctionAt(wire.x2, wire.y2, node.junctions);
let hitJunctionWireNode = junctionAt(wire.x2, wire.y2, wire.node2.junctions);
if (hitJunctionNode) {
connectJunctionNode(hitJunctionNode, wire.node2);
}
else if (hitJunctionWireNode)
connectJunctionNode(hitJunctionWireNode, node);
else {

let junction = new Junction();
junction.x = wire.x2;
junction.y = wire.y2;
connectJunctionNode(junction, wire.node2);
connectJunctionNode(junction, node);
junctions.push(junction);

}
}
}
else { // junction // T like connection
connectNodes(wire.node2, w.node1);
connectNodes(wire.node2, w.node2);

let hitJunction = junctionAt(wire.x2, wire.y2, w.node1.junctions);

if (hitJunction) {
connectJunctionNode(hitJunction, wire.node2);
}
else {
let junction = new Junction();
junction.x = wire.x2;
junction.y = wire.y2;
connectJunctionNode(junction, wire.node2);
connectJunctionNode(junction, w.node1);
connectJunctionNode(junction, w.node2);
junctions.push(junction);
}
w.nodesOnLineCount++;
}
}
else if (hitLeft) {
let node = w.hitNode(wire.x1, wire.y1); // whether the point wire.x1 wire.y1 lies on (w.x1 w.y1) || (w.x2 w.y2)
if (node != null)
if (node != null) {
connectNodes(wire.node1, node);
if ((node.connections.length - w.nodesOnLineCount) >= 3) { // junction
let hitJunctionNode = junctionAt(wire.x1, wire.y1, node.junctions);
let hitJunctionWireNode = junctionAt(wire.x1, wire.y1, wire.node1.junctions);
if (hitJunctionNode) {
connectJunctionNode(hitJunctionNode, wire.node1);
}
else if (hitJunctionWireNode)
connectJunctionNode(hitJunctionWireNode, node);
else {

let junction = new Junction();
junction.x = wire.x1;
junction.y = wire.y1;
connectJunctionNode(junction, wire.node1);
connectJunctionNode(junction, node);
junctions.push(junction);

}
}
}
else { // junction // T like connection
connectNodes(wire.node1, w.node1);
connectNodes(wire.node1, w.node2);

let hitJunction = junctionAt(wire.x1, wire.y1, w.node1.junctions);

if (hitJunction) {
connectJunctionNode(hitJunction, wire.node1);
}

else {
let junction = new Junction();
junction.x = wire.x1;
junction.y = wire.y1;
connectJunctionNode(junction, wire.node1);
connectJunctionNode(junction, w.node1);
connectJunctionNode(junction, w.node2);
junctions.push(junction);
}

w.nodesOnLineCount++;
}
}
}
Expand Down
25 changes: 25 additions & 0 deletions node.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Node {

this.parent = null;
this.connections = []; // {.value}
this.junctions = [];
}
}

Expand All @@ -28,6 +29,17 @@ function findNode(node1, node2) {
return -1;
}

function checkJunctionConnection(node1, node2) {
for (let i = 0; i < node1.junctions.length; ++i) {
for (let n = 0; n < node1.junctions[i].nodes.length; ++n) {
if (node1.junctions[i].nodes[n] === node2) {
return {jInd: i, nInd: n};
}
}
}
return {jInd: -1, nInd: -1};
}

function disconnectNodes(node1, node2) {
let ind1 = findNode(node1, node2);
let ind2 = findNode(node2, node1);
Expand All @@ -45,6 +57,19 @@ function deleteNode(node) {
let ind = findNode(goodNode.node, node);
goodNode.node.connections.splice(ind, 1);

if (goodNode.node.junctions.length > 0) {
let junction = checkJunctionConnection(goodNode.node, node);

if (junction.jInd >= 0 && junction.nInd >= 0) {
if ((goodNode.node.junctions[junction.jInd].nodes.length - 1) >= 3) {
goodNode.node.junctions[junction.jInd].nodes.splice(junction.nInd, 1);
} else {
//goodNode.node.junction[junction.jInd].onDelete();
deleteJunction(goodNode.node.junctions[junction.jInd]);
}
}
}

}
node.connections.length = 0; // clear
}
1 change: 1 addition & 0 deletions wire.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Wire {
this.node2.parent = this;

this.nodesOnLine = [];
this.nodesOnLineCount = 0;

connectNodes(this.node1, this.node2, "0");
}
Expand Down

0 comments on commit fbc6c8b

Please sign in to comment.