-
Notifications
You must be signed in to change notification settings - Fork 6
/
cpdiodes.js
148 lines (137 loc) · 4.66 KB
/
cpdiodes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// File: cpdiodes.js
/*
Deletes all diodes that are not connected to two or more segments
*/
function deleteInvalidDiodes() {
for (let j = diodes.length - 1; j >= 0; j--) {
if (wirePoints(diodes[j].x, diodes[j].y, -1).length < 2) {
diodes.splice(j, 1);
}
}
}
/*
Deletes all conpoints that are not connected to three or more segments
*/
function deleteInvalidConpoints() {
for (let j = conpoints.length - 1; j >= 0; j--) {
if (wirePoints(conpoints[j].x, conpoints[j].y, -1).length < 3 || isDiode(conpoints[j].x, conpoints[j].y) >= 0) {
conpoints.splice(j, 1);
}
}
}
function getGroup(seg) {
for (let i = 0; i < groups.length; i++) {
for (let j = 0; j < groups[i].segments.length; j++) {
if ((groups[i].segments[j].startX === seg.startX) && (groups[i].segments[j].startY === seg.startY) && (groups[i].segments[j].direction === seg.direction)) {
return i;
}
}
}
return -1;
}
function segmentStartsIn(x, y) {
for (let i = 0; i < segments.length; i++) {
if (segments[i].startX === x && segments[i].startY === y) {
return i;
}
}
return -1;
}
function segmentEndsIn(x, y) {
for (let i = 0; i < segments.length; i++) {
if (segments[i].endX === x && segments[i].endY === y) {
return i;
}
}
return -1;
}
/*
Creates a new connection point for group g at position x, y
Only creates if not existing and no diode at the point
*/
function createConpoint(x, y, state, g) {
if ((isConPoint(x, y) < 0) && (isDiode(x, y) < 0)) {
conpoints.push(new ConPoint(x, y, state, g));
}
}
/*
Creates a new diode if the point meets the requirements
gA: Group A (horizontal, not influenced by the vertical wire)
gB: Group B (vertical, synced to group A)
Nice-to-have-TODO: Diodes can only be put in places where horizontal + vertical wires are
*/
function createDiode(x, y, state) {
// If there is no diode and a diode can be set
if ((isDiode(x, y) < 0) && (wirePoints(x, y, -1).length > 1)) {
diodes.push(new Diode(x, y, state, transform));
diodes[diodes.length - 1].updateClickBox();
pushUndoAction('addDi', [], diodes[diodes.length - 1]);
}
let cp = isConPoint(x, y);
if (cp >= 0) {
conpoints.splice(cp, 1);
}
}
/*
Checks if a connection point is at the given position
*/
function isConPoint(x, y) {
for (let i = 0; i < conpoints.length; i++) {
if (conpoints[i].x === x && conpoints[i].y === y) {
return i;
}
}
return -1;
}
function isDiode(x, y) {
for (let i = 0; i < diodes.length; i++) {
if (diodes[i].x === x && diodes[i].y === y) {
return i;
}
}
return -1;
}
/*
Updates all ConPoints, including deleting
*/
function doConpoints() {
for (let i = 0; i < segments.length; i++) {
// Get all segments starting or ending in the point
let wp1 = wirePoints(segments[i].startX, segments[i].startY, -1);
let wp2 = wirePoints(segments[i].endX, segments[i].endY, -1);
// If there are 3 segments connecting
if (wp1.length === 3) {
if (isConPoint(segments[i].startX, segments[i].startY) < 0) {
createConpoint(segments[i].startX, segments[i].startY, false, -1);
}
} else if (wp1.length === 4) {
if (isConPoint(segments[i].startX, segments[i].startY) < 0) {
for (let k = 0; k < wp1.length; k++) {
if (segments[wp1[k]].direction === segments[i].direction) { // If they have the same direction
let s = wp1[k];
wp1 = []; // Only explore in this segment
wp1.push(s);
}
}
} // else explore every segment
}
// Same thing for the other direction
if (wp2.length === 3) {
if (isConPoint(segments[i].endX, segments[i].endY) < 0) {
createConpoint(segments[i].endX, segments[i].endY, false, -1);
}
} else if (wp2.length === 4) {
if (isConPoint(segments[i].endX, segments[i].endY) < 0) {
for (let k = 0; k < wp2.length; k++) {
if (segments[wp2[k]].direction === segments[i].direction) { // If they have the same direction
let s = wp2[k];
wp2 = []; // Only explore in this segment
wp2.push(s);
}
}
} // else explore every segment
}
}
deleteInvalidConpoints();
deleteInvalidDiodes();
}