Skip to content

Commit

Permalink
use IEC gates in Germany
Browse files Browse the repository at this point in the history
fix box element to be useful
- draw as dotted rectangle
- don't create box if it's too small
add ohmmeter
fix bug where bounding box would shrink whenever an element is drawn
  • Loading branch information
pfalstad committed Jan 16, 2018
1 parent dca976e commit 3d4f273
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 63 deletions.
35 changes: 21 additions & 14 deletions src/com/lushprojects/circuitjs1/client/AndGateElm.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,34 @@ public AndGateElm(int xa, int ya, int xb, int yb, int f,
StringTokenizer st) {
super(xa, ya, xb, yb, f, st);
}

String getGateText() { return "&"; }

void setPoints() {
super.setPoints();

// 0=topleft, 1-10 = top curve, 11 = right, 12-21=bottom curve,
// 22 = bottom left
Point triPoints[] = newPointArray(23);
interpPoint2(lead1, lead2, triPoints[0], triPoints[22], 0, hs2);
int i;
for (i = 0; i != 10; i++) {
double a = i*.1;
double b = Math.sqrt(1-a*a);
interpPoint2(lead1, lead2,
triPoints[i+1], triPoints[21-i],
.5+a/2, b*hs2);

if (useEuroGates()) {
createEuroGatePolygon();
} else {
// 0=topleft, 1-10 = top curve, 11 = right, 12-21=bottom curve,
// 22 = bottom left
Point triPoints[] = newPointArray(23);
interpPoint2(lead1, lead2, triPoints[0], triPoints[22], 0, hs2);
int i;
for (i = 0; i != 10; i++) {
double a = i*.1;
double b = Math.sqrt(1-a*a);
interpPoint2(lead1, lead2,
triPoints[i+1], triPoints[21-i],
.5+a/2, b*hs2);
}
triPoints[11] = new Point(lead2);
gatePoly = createPolygon(triPoints);
}
triPoints[11] = new Point(lead2);
if (isInverting()) {
pcircle = interpPoint(point1, point2, .5+(ww+4)/dn);
lead2 = interpPoint(point1, point2, .5+(ww+8)/dn);
}
gatePoly = createPolygon(triPoints);
}
String getGateName() { return "AND gate"; }
boolean calcFunction() {
Expand Down
30 changes: 14 additions & 16 deletions src/com/lushprojects/circuitjs1/client/BoxElm.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class BoxElm extends GraphicElm {

public BoxElm(int xx, int yy) {
super(xx, yy);
x2 = xx + 16;
y2 = yy + 16;
x2 = xx;
y2 = yy;
setBbox(x, y, x2, y2);
}

Expand All @@ -35,14 +35,6 @@ public BoxElm(int xa, int ya, int xb, int yb, int f,
super(xa, ya, xb, yb, f);
x2 = xb;
y2 = yb;
/* if ( st.hasMoreTokens() )
x = new Integer(st.nextToken()).intValue();
if ( st.hasMoreTokens() )
y = new Integer(st.nextToken()).intValue();
if ( st.hasMoreTokens() )
x2 = new Integer(st.nextToken()).intValue();
if ( st.hasMoreTokens() )
y2 = new Integer(st.nextToken()).intValue();*/
setBbox(x, y, x2, y2);
}

Expand All @@ -53,22 +45,28 @@ String dump() {
int getDumpType() { return 'b'; }

void drag(int xx, int yy) {
x = xx;
y = yy;
x2 = xx;
y2 = yy;
}

boolean creationFailed() {
return Math.abs(x2-x) < 32 || Math.abs(y2-y) < 32;
}

void draw(Graphics g) {
//g.setColor(needsHighlight() ? selectColor : lightGrayColor);
g.setColor(needsHighlight() ? selectColor : Color.GRAY);
setBbox(x, y, x2, y2);
g.setLineDash(16, 6);
if ( x < x2 && y < y2 )
g.fillRect(x,y, x2-x, y2-y);
g.drawRect(x,y, x2-x, y2-y);
else if ( x > x2 && y < y2 )
g.fillRect(x2,y, x-x2, y2-y);
g.drawRect(x2,y, x-x2, y2-y);
else if ( x < x2 && y > y2 )
g.fillRect(x, y2, x2-x, y-y2);
g.drawRect(x, y2, x2-x, y-y2);
else
g.fillRect(x2, y2, x-x2, y-y2);
g.drawRect(x2, y2, x-x2, y-y2);
g.setLineDash(0, 0);
}

public EditInfo getEditInfo(int n) {
Expand Down
38 changes: 34 additions & 4 deletions src/com/lushprojects/circuitjs1/client/CirSim.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ public class CirSim implements MouseDownHandler, MouseMoveHandler, MouseUpHandle
CheckboxMenuItem showValuesCheckItem;
CheckboxMenuItem conductanceCheckItem;
CheckboxMenuItem euroResistorCheckItem;
CheckboxMenuItem euroGatesCheckItem;
CheckboxMenuItem printableCheckItem;
CheckboxMenuItem alternativeColorCheckItem;
CheckboxMenuItem conventionCheckItem;
Expand Down Expand Up @@ -368,6 +369,7 @@ else if (usRes)
euroSetting = false;
else
euroSetting = getOptionFromStorage("euroResistors", !weAreInUS());
boolean euroGates = getOptionFromStorage("euroGates", weAreInGermany());

transform = new double[6];
String os = Navigator.getPlatform();
Expand Down Expand Up @@ -485,6 +487,15 @@ else if (usRes)
}
}));
euroResistorCheckItem.setState(euroSetting);
m.addItem(euroGatesCheckItem = new CheckboxMenuItem(LS("IEC Gates"),
new Command() { public void execute(){
setOptionInStorage("euroGates", euroGatesCheckItem.getState());
int i;
for (i = 0; i != elmList.size(); i++)
getElm(i).setPoints();
}
}));
euroGatesCheckItem.setState(euroGates);
m.addItem(printableCheckItem = new CheckboxMenuItem(LS("White Background"),
new Command() { public void execute(){
int i;
Expand Down Expand Up @@ -797,6 +808,7 @@ public void composeMainMenu(MenuBar mainMenuBar) {
outputMenuBar.addItem(getClassCheckItem(LS("Add Text"), "TextElm"));
outputMenuBar.addItem(getClassCheckItem(LS("Add Box"), "BoxElm"));
outputMenuBar.addItem(getClassCheckItem(LS("Add Voltmeter/Scobe Probe"), "ProbeElm"));
outputMenuBar.addItem(getClassCheckItem(LS("Add Ohmmeter"), "OhmMeterElm"));
outputMenuBar.addItem(getClassCheckItem(LS("Add Labeled Node"), "LabeledNodeElm"));
outputMenuBar.addItem(getClassCheckItem(LS("Add Test Point"), "TestPointElm"));
outputMenuBar.addItem(getClassCheckItem(LS("Add Ammeter"), "AmmeterElm"));
Expand Down Expand Up @@ -1812,7 +1824,9 @@ void analyzeCircuit() {
CurrentElm cur = (CurrentElm) ce;
FindPathInfo fpi = new FindPathInfo(FindPathInfo.INDUCT, ce,
ce.getNode(1));
if (!fpi.findPath(ce.getNode(0))) {
// first try findPath with maximum depth of 5, to avoid slowdowns
if (!fpi.findPath(ce.getNode(0), 5) &&
!fpi.findPath(ce.getNode(0))) {
cur.stampCurrentSource(true);
} else
cur.stampCurrentSource(false);
Expand Down Expand Up @@ -3443,8 +3457,12 @@ public void mouseSelect(MouseEvent<?> e) {
newMouseElm = ce;
}
}
if (ce.getPostCount() == 0)
newMouseElm = ce;
// prefer selecting elements that have small bounding box area (for
// elements with no posts)
if (ce.getPostCount() == 0 && area <= bestArea) {
newMouseElm = ce;
bestArea = area;
}
}
} // for
}
Expand Down Expand Up @@ -3688,7 +3706,7 @@ public void onMouseUp(MouseUpEvent e) {
if (dragElm != null) {
// if the element is zero size then don't create it
// IES - and disable any previous selection
if (dragElm.x == dragElm.x2 && dragElm.y == dragElm.y2) {
if (dragElm.creationFailed()) {
dragElm.delete();
if (mouseMode == MODE_SELECT || mouseMode == MODE_DRAG_SELECTED)
clearSelection();
Expand Down Expand Up @@ -4452,6 +4470,8 @@ public static CircuitElm createCe(int tint, int x1, int y1, int x2, int y2, int
return (CircuitElm) new CCVSElm(x1, y1, x2, y2, f, st);
if (tint==215)
return (CircuitElm) new CCCSElm(x1, y1, x2, y2, f, st);
if (tint==216)
return (CircuitElm) new OhmMeterElm(x1, y1, x2, y2, f, st);
if (tint==368)
return new TestPointElm(x1, y1, x2, y2, f, st);
if (tint==370)
Expand Down Expand Up @@ -4667,6 +4687,8 @@ public static CircuitElm constructElement(String n, int x1, int y1){
return (CircuitElm) new CCVSElm(x1, y1);
if (n=="CCCSElm")
return (CircuitElm) new CCCSElm(x1, y1);
if (n=="OhmMeterElm")
return (CircuitElm) new OhmMeterElm(x1, y1);
return null;
}

Expand All @@ -4692,6 +4714,14 @@ native boolean weAreInUS() /*-{
} catch (e) { return 0;
}
}-*/;

native boolean weAreInGermany() /*-{
try {
l = navigator.languages ? navigator.languages[0] : (navigator.language || navigator.userLanguage) ;
return (l.toUpperCase().startsWith("DE"));
} catch (e) { return 0;
}
}-*/;

static String LS(String s) {
if (s == null)
Expand Down
9 changes: 7 additions & 2 deletions src/com/lushprojects/circuitjs1/client/CircuitElm.java
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,11 @@ void move(int dx, int dy) {
setPoints();
}

// called when an element is done being dragged out; returns true if it's zero size and should be deleted
boolean creationFailed() {
return (x == x2 && y == y2);
}

// determine if moving this element by (dx,dy) will put it on top of another element
boolean allowMove(int dx, int dy) {
int nx = x+dx;
Expand Down Expand Up @@ -450,8 +455,8 @@ void adjustBbox(int x1, int y1, int x2, int y2) {
if (y1 > y2) { int q = y1; y1 = y2; y2 = q; }
x1 = min(boundingBox.x, x1);
y1 = min(boundingBox.y, y1);
x2 = max(boundingBox.x+boundingBox.width-1, x2);
y2 = max(boundingBox.y+boundingBox.height-1, y2);
x2 = max(boundingBox.x+boundingBox.width, x2);
y2 = max(boundingBox.y+boundingBox.height, y2);
boundingBox.setBounds(x1, y1, x2-x1, y2-y1);
}
void adjustBbox(Point p1, Point p2) {
Expand Down
14 changes: 14 additions & 0 deletions src/com/lushprojects/circuitjs1/client/GateElm.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ void setPoints() {
schmittPoly = getSchmittPolygon(gsize, .47f);
}

void createEuroGatePolygon() {
Point pts[] = newPointArray(4);
interpPoint2(lead1, lead2, pts[0], pts[1], 0, hs2);
interpPoint2(lead1, lead2, pts[3], pts[2], 1, hs2);
gatePoly = createPolygon(pts);
}

String getGateText() { return null; }
static boolean useEuroGates() { return sim.euroGatesCheckItem.getState(); }

void draw(Graphics g) {
int i;
for (i = 0; i != inputCount; i++) {
Expand All @@ -109,6 +119,10 @@ void draw(Graphics g) {
drawThickLine(g, lead2, point2);
g.setColor(needsHighlight() ? selectColor : lightGrayColor);
drawThickPolygon(g, gatePoly);
if (useEuroGates()) {
Point center = interpPoint(point1, point2, .5);
drawCenteredText(g, getGateText(), center.x, center.y-6*gsize, true);
}
g.setLineWidth(2);
if (hasSchmittInputs())
drawPolygon(g, schmittPoly);
Expand Down
10 changes: 10 additions & 0 deletions src/com/lushprojects/circuitjs1/client/Graphics.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,14 @@ static int distanceSq(int x1, int y1, int x2, int y2) {
return x2*x2+y2*y2;
}

void setLineDash(int a, int b) {
setLineDash(context, a, b);
}

native static void setLineDash(Context2d context, int a, int b) /*-{
if (a == 0)
context.setLineDash([]);
else
context.setLineDash([a, b]);
}-*/;
}
23 changes: 19 additions & 4 deletions src/com/lushprojects/circuitjs1/client/InverterElm.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,16 @@ String dump() {
}

int getDumpType() { return 'I'; }

Point center;

void draw(Graphics g) {
drawPosts(g);
draw2Leads(g);
g.setColor(needsHighlight() ? selectColor : lightGrayColor);
drawThickPolygon(g, gatePoly);
if (GateElm.useEuroGates())
drawCenteredText(g, "1", center.x, center.y-6, true);
drawThickCircle(g, pcircle.x, pcircle.y, 3);
curcount = updateDotCount(current, curcount);
drawDots(g, lead2, point2, curcount);
Expand All @@ -67,10 +72,20 @@ void setPoints() {
lead1 = interpPoint(point1, point2, .5-ww/dn);
lead2 = interpPoint(point1, point2, .5+(ww+2)/dn);
pcircle = interpPoint(point1, point2, .5+(ww-2)/dn);
Point triPoints[] = newPointArray(3);
interpPoint2(lead1, lead2, triPoints[0], triPoints[1], 0, hs);
triPoints[2] = interpPoint(point1, point2, .5+(ww-5)/dn);
gatePoly = createPolygon(triPoints);

if (GateElm.useEuroGates()) {
Point pts[] = newPointArray(4);
Point l2 = interpPoint(point1, point2, .5+(ww-5)/dn); // make room for circle
interpPoint2(lead1, l2, pts[0], pts[1], 0, hs);
interpPoint2(lead1, l2, pts[3], pts[2], 1, hs);
gatePoly = createPolygon(pts);
center = interpPoint(lead1, l2, .5);
} else {
Point triPoints[] = newPointArray(3);
interpPoint2(lead1, lead2, triPoints[0], triPoints[1], 0, hs);
triPoints[2] = interpPoint(point1, point2, .5+(ww-5)/dn);
gatePoly = createPolygon(triPoints);
}
setBbox(point1, point2, hs);
}
int getVoltageSourceCount() { return 1; }
Expand Down
54 changes: 31 additions & 23 deletions src/com/lushprojects/circuitjs1/client/OrGateElm.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,36 +29,44 @@ public OrGateElm(int xa, int ya, int xb, int yb, int f,
void setPoints() {
super.setPoints();

// 0-15 = top curve, 16 = right, 17-32=bottom curve,
// 33-37 = left curve
Point triPoints[] = newPointArray(38);
if (this instanceof XorGateElm)
linePoints = new Point[5];
int i;
for (i = 0; i != 16; i++) {
double a = i/16.;
double b = 1-a*a;
interpPoint2(lead1, lead2,
triPoints[i], triPoints[32-i],
.5+a/2, b*hs2);
}
double ww2 = (ww == 0) ? dn*2 : ww*2;
for (i = 0; i != 5; i++) {
double a = (i-2)/2.;
double b = 4*(1-a*a)-2;
interpPoint(lead1, lead2,
triPoints[33+i], b/(ww2), a*hs2);
if (useEuroGates()) {
createEuroGatePolygon();
linePoints = null;
} else {
// 0-15 = top curve, 16 = right, 17-32=bottom curve,
// 33-37 = left curve
Point triPoints[] = newPointArray(38);
if (this instanceof XorGateElm)
linePoints[i] = interpPoint(lead1, lead2,
(b-5)/(ww2), a*hs2);
linePoints = new Point[5];
int i;
for (i = 0; i != 16; i++) {
double a = i/16.;
double b = 1-a*a;
interpPoint2(lead1, lead2,
triPoints[i], triPoints[32-i],
.5+a/2, b*hs2);
}
double ww2 = (ww == 0) ? dn*2 : ww*2;
for (i = 0; i != 5; i++) {
double a = (i-2)/2.;
double b = 4*(1-a*a)-2;
interpPoint(lead1, lead2,
triPoints[33+i], b/(ww2), a*hs2);
if (this instanceof XorGateElm)
linePoints[i] = interpPoint(lead1, lead2,
(b-5)/(ww2), a*hs2);
}
triPoints[16] = new Point(lead2);
gatePoly = createPolygon(triPoints);
}
triPoints[16] = new Point(lead2);
if (isInverting()) {
pcircle = interpPoint(point1, point2, .5+(ww+4)/dn);
lead2 = interpPoint(point1, point2, .5+(ww+8)/dn);
}
gatePoly = createPolygon(triPoints);
}

String getGateText() { return "\u22651"; }

boolean calcFunction() {
int i;
boolean f = false;
Expand Down
Loading

0 comments on commit 3d4f273

Please sign in to comment.