Skip to content

Commit

Permalink
when dragging terminals of tristate buffers and analog switches (afte…
Browse files Browse the repository at this point in the history
…r placing them), the middle terminal was sometimes impossible to connect (#92)
  • Loading branch information
pfalstad committed Oct 25, 2024
1 parent 0e16845 commit 83650c7
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 29 deletions.
3 changes: 2 additions & 1 deletion src/com/lushprojects/circuitjs1/client/AnalogSwitch2Elm.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ public AnalogSwitch2Elm(int xa, int ya, int xb, int yb, int f,
void setPoints() {
super.setPoints();
calcLeads(32);
adjustLeadsToGrid();
swposts = newPointArray(2);
swpoles = newPointArray(2);
interpPoint2(lead1, lead2, swpoles[0], swpoles[1], 1, openhs);
interpPoint2(point1, point2, swposts[0], swposts[1], 1, openhs);
ctlPoint = interpPoint(point1, point2, .5, openhs);
ctlPoint = interpPoint(lead1, lead2, .5, openhs);
}
int getPostCount() { return 4; }

Expand Down
21 changes: 5 additions & 16 deletions src/com/lushprojects/circuitjs1/client/AnalogSwitchElm.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ public AnalogSwitchElm(int xx, int yy) {
super(xx, yy);
r_on = 20;
r_off = 1e10;
noDiagonal = true;
}
public AnalogSwitchElm(int xa, int ya, int xb, int yb, int f,
StringTokenizer st) {
super(xa, ya, xb, yb, f);
r_on = 20;
r_off = 1e10;
noDiagonal = true;
try {
r_on = new Double(st.nextToken()).doubleValue();
r_off = new Double(st.nextToken()).doubleValue();
Expand All @@ -49,10 +51,11 @@ String dump() {
void setPoints() {
super.setPoints();
calcLeads(32);
adjustLeadsToGrid();
ps = new Point();
int openhs = 16;
point3 = interpPoint(point1, point2, .5, -openhs);
lead3 = interpPoint(point1, point2, .5, -openhs/2);
point3 = interpPoint(lead1, lead2, .5, -openhs);
lead3 = interpPoint(lead1, lead2, .5, -openhs/2);
}

void draw(Graphics g) {
Expand Down Expand Up @@ -91,20 +94,6 @@ void doStep() {
resistance = (open) ? r_off : r_on;
sim.stampResistor(nodes[0], nodes[1], resistance);
}
void drag(int xx, int yy) {
xx = sim.snapGrid(xx);
yy = sim.snapGrid(yy);
if (abs(x-xx) < abs(y-yy))
xx = x;
else
yy = y;
int q1 = abs(x-xx)+abs(y-yy);
int q2 = (q1/2) % sim.gridSize;
if (q2 != 0)
return;
x2 = xx; y2 = yy;
setPoints();
}
int getPostCount() { return 3; }
Point getPost(int n) {
return (n == 0) ? point1 : (n == 1) ? point2 : point3;
Expand Down
10 changes: 10 additions & 0 deletions src/com/lushprojects/circuitjs1/client/CircuitElm.java
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,16 @@ void calcLeads(int len) {
lead2 = interpPoint(point1, point2, (dn+len)/(2*dn));
}

// adjust leads so that the point exactly between them is a grid point (so we can place a terminal there)
void adjustLeadsToGrid() {
int cx = (point1.x+point2.x)/2;
int cy = (point1.y+point2.y)/2;
int adjx = sim.snapGrid(cx)-cx;
int adjy = sim.snapGrid(cy)-cy;
lead1.move(adjx, adjy);
lead2.move(adjx, adjy);
}

// calculate point fraction f between a and b, linearly interpolated
Point interpPoint(Point a, Point b, double f) {
Point p = new Point();
Expand Down
7 changes: 6 additions & 1 deletion src/com/lushprojects/circuitjs1/client/Point.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,9 @@ public void setLocation(Point p) {
@Override public int hashCode() {
return (41 * (41 + x) + y);
}
}

public void move(int dx, int dy) {
x += dx;
y += dy;
}
}
21 changes: 10 additions & 11 deletions src/com/lushprojects/circuitjs1/client/TriStateElm.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ public TriStateElm(int xx, int yy) {
r_on = 0.1;
r_off = 1e10;
r_off_ground = 1e8;
noDiagonal = true;
}

public TriStateElm(int xa, int ya, int xb, int yb, int f, StringTokenizer st) {
super(xa, ya, xb, yb, f);
r_on = 0.1;
r_off = 1e10;
r_off_ground = 0;
noDiagonal = true;
try {
r_on = new Double(st.nextToken()).doubleValue();
r_off = new Double(st.nextToken()).doubleValue();
Expand All @@ -64,7 +66,10 @@ int getDumpType() {

void setPoints() {
super.setPoints();
calcLeads(32);
int len = 32;
calcLeads(len);
adjustLeadsToGrid();

ps = new Point();
int hs = 16;

Expand All @@ -73,12 +78,12 @@ void setPoints() {
ww = (int) (dn / 2);
Point triPoints[] = newPointArray(3);
interpPoint2(lead1, lead2, triPoints[0], triPoints[1], 0, hs + 2);
triPoints[2] = interpPoint(point1, point2, .5 + (ww - 2) / dn);
triPoints[2] = interpPoint(lead1, lead2, .5 + (ww - 2) / (double)len);
gatePoly = createPolygon(triPoints);

int sign = ((flags & FLAG_FLIP) == 0) ? -1 : 1;
point3 = interpPoint(point1, point2, .5, sign*hs);
lead3 = interpPoint(point1, point2, .5, sign*hs/2);
point3 = interpPoint(lead1, lead2, .5, sign*hs);
lead3 = interpPoint(lead1, lead2, .5, sign*hs/2);
}

void draw(Graphics g) {
Expand Down Expand Up @@ -155,14 +160,8 @@ void drag(int xx, int yy) {
flip = !flip;
yy = y;
}
int q1 = abs(x - xx) + abs(y - yy);
int q2 = (q1 / 2) % sim.gridSize;
if (q2 != 0)
return;
x2 = xx;
y2 = yy;
flags = flip ? (flags | FLAG_FLIP) : (flags & ~FLAG_FLIP);
setPoints();
super.drag(xx, yy);
}

int getPostCount() {
Expand Down

0 comments on commit 83650c7

Please sign in to comment.