Added TwoTerminal.centerto() method to Circ.asy
[course.git] / asymptote / Circ.asy
index 1b61e2d4a2818504a7faafa8f4450144f44e191f..dae2959639669d32d76ebb396ecf2caa037c4b77 100644 (file)
@@ -236,7 +236,20 @@ struct TwoTerminal {
     label(picL, lVal, pVal, aVal);
     add(pic, picL, (end-beg)/2);
   }
-  
+
+  /* Rather than placing the element with a point and direction (beg,
+   * ang), center an element between the pairs a and b.  The optional
+   * offset shifts the element in the direction rotated(90)*(b-a)
+   * (i.e. up for offset > 0 if b is directly right of a).
+   */
+  void centerto(pair a, pair b, real offset=0) {
+    this.ang = degrees(b-a);
+    this.beg = (a+b)/2
+      - unit(b-a)*this.len/2 + offset*dir(this.ang+90);
+    this.end = this.beg+rotate(ang)*(this.len,0);
+    this.mid = (this.beg + this.end)/2;
+  }
+
   void draw(picture pic=currentpicture) {
     picture picT;
     for (int i=0; i< pLine.length; i+=1) {
@@ -259,7 +272,8 @@ struct TwoTerminal {
 real rstlth=2mm;
 int normal=0, variable=2;
 
-TwoTerminal resistor(pair beg, int type, real ang, string name, string val)
+TwoTerminal resistor(pair beg, int type, real ang, string name, string val,
+                    bool draw=true)
 {
   path pLine, pMisc[]={};
   TwoTerminal term;
@@ -278,7 +292,8 @@ TwoTerminal resistor(pair beg, int type, real ang, string name, string val)
     write("Error, unrecognized resistor type",type);    
   }
   term = TwoTerminal(beg, 7rstlth, ang, .8rstlth, .8rstlth, name, val, pLine, pMisc);
-  term.draw();
+  if (draw == true)
+    term.draw();
   return term;
 }
 
@@ -287,7 +302,8 @@ TwoTerminal resistor(pair beg, int type, real ang, string name, string val)
 real coil=2mm;
 int Up=0, Down=1;
 
-TwoTerminal inductor(pair beg, int type, real ang, string name, string val)
+TwoTerminal inductor(pair beg, int type, real ang, string name, string val,
+                    bool draw=true)
 {
   path pLine;
   TwoTerminal term;
@@ -308,7 +324,8 @@ TwoTerminal inductor(pair beg, int type, real ang, string name, string val)
   }
   term = TwoTerminal(beg, 6coil, ang, coil, coil, name, val, pLine);
   // the original makecirc used .5coil for lcharv
-  term.draw();
+  if (draw == true)
+    term.draw();
   return term;
 }
 
@@ -317,7 +334,8 @@ TwoTerminal inductor(pair beg, int type, real ang, string name, string val)
 real platsep=1mm;
 int normal=0, electrolytic=1, variable=2, variant=3;
 
-TwoTerminal capacitor(pair beg, int type, real ang, string name, string val)
+TwoTerminal capacitor(pair beg, int type, real ang, string name, string val,
+                     bool draw=true)
 {
   path pLine[]={}, pMisc[]={};
   TwoTerminal term;
@@ -345,7 +363,8 @@ TwoTerminal capacitor(pair beg, int type, real ang, string name, string val)
     write("Error, unrecognized capacitor type",type);
   }
   term = TwoTerminal(beg, 7platsep, ang, 2.5platsep, 2.5platsep, name, val, pLine, pMisc);
-  term.draw();
+  if (draw == true)
+    term.draw();
   return term;
 }
 
@@ -354,10 +373,11 @@ TwoTerminal capacitor(pair beg, int type, real ang, string name, string val)
 real diodeht=3.5mm;
 int zener=1, LED=2;
 // I droped the pin parameters, since other device (e.g. electrolytic
-// capacitors) are also polarized.  The positioning macros centerof,
-// etc provide enough flexibility.
+// capacitors) are also polarized.  The positioning method centerto(),
+// provides enough flexibility.
 
-TwoTerminal diode(pair beg, int type, real ang, string name, string val)
+TwoTerminal diode(pair beg, int type, real ang, string name, string val,
+                 bool draw=true)
 {
   path pLine[]={}, pMisc[]={};
   real lchar, lcharv;
@@ -384,8 +404,9 @@ TwoTerminal diode(pair beg, int type, real ang, string name, string val)
   } else {
     write("Error, unrecognized capacitor type",type);
   }
-  term = TwoTerminal(beg, 3diodeht, ang, lchar, lcharv, name, val, pLine, pMisc);
-  term.draw();
+  term = TwoTerminal(beg, 3diodeht, ang, lchar, lcharv, name, val,pLine,pMisc);
+  if (draw == true)
+    term.draw();
   return term;
 }
 
@@ -393,7 +414,8 @@ TwoTerminal diode(pair beg, int type, real ang, string name, string val)
 
 real bsize = 6mm;
 
-TwoTerminal battery(pair beg, real ang, string name, string val)
+TwoTerminal battery(pair beg, real ang, string name, string val,
+                   bool draw=true)
 {
   path pLine[]={}, pMisc[]={};
   real lchar, lcharv;
@@ -404,12 +426,13 @@ TwoTerminal battery(pair beg, real ang, string name, string val)
   
   for (int i=0; i<3; i+=1) {
     pLine.push(((0.4+0.4i)*bsize, -0.2bsize)--((0.4+0.4i)*bsize, 0.2bsize));
-    pLine.push(((0.6+0.4i)*bsize, -0.6bsize)--((0.6+0.4i)*bsize, 0.6bsize));    
+    pLine.push(((0.6+0.4i)*bsize, -0.6bsize)--((0.6+0.4i)*bsize, 0.6bsize));
   }
   lchar = 0.6bsize; lcharv = 0.6bsize;
 
-  term = TwoTerminal(beg, 1.8bsize, ang, lchar, lcharv, name, val, pLine, pMisc);
-  term.draw();
+  term = TwoTerminal(beg, 1.8bsize, ang, lchar, lcharv, name, val,pLine,pMisc);
+  if (draw == true)
+    term.draw();
   return term;
 }
 
@@ -421,7 +444,8 @@ real ssep=3mm, swt=1.2ssep; // TODO remove swt?
 /* `switch' is a Asymptote keyword (or it should be), so append SPST
  * for Single Pole Single Throw.
  */
-TwoTerminal switchSPST(pair beg, int type, real ang, string name, string val)
+TwoTerminal switchSPST(pair beg, int type, real ang, string name, string val,
+                      bool draw=true)
 {
   path pLine[]={}, pMisc[]={};
   real lchar, lcharv;
@@ -438,8 +462,9 @@ TwoTerminal switchSPST(pair beg, int type, real ang, string name, string val)
   } else {
     write("Error, unrecognized switchSPST type",type);
   }
-  term = TwoTerminal(beg, 2.4ssep, ang, lchar, lcharv, name, val, pLine, pMisc);
-  term.draw();
+  term = TwoTerminal(beg, 2.4ssep, ang, lchar, lcharv, name, val, pLine,pMisc);
+  if (draw == true)
+    term.draw();
   return term;
 }
 
@@ -448,7 +473,8 @@ TwoTerminal switchSPST(pair beg, int type, real ang, string name, string val)
 real isize=2mm;
 
 // adjusted from makecirc original to center arrowhead under text
-TwoTerminal current(pair beg, real ang, string name, string val)
+TwoTerminal current(pair beg, real ang, string name, string val,
+                   bool draw=true)
 {
   path pLine[]={}, pMisc[]={};
   real lchar, lcharv;
@@ -458,7 +484,8 @@ TwoTerminal current(pair beg, real ang, string name, string val)
   pLine.push((1.5isize,0)--(2isize,0));
   pMisc.push(fullhead(pLine[0], isize, 45));
   term = TwoTerminal(beg, 2isize, ang, lchar, lcharv, name, val, pLine, pMisc);
-  term.draw();
+  if (draw == true)
+    term.draw();
   return term;
 }
 
@@ -467,7 +494,8 @@ TwoTerminal current(pair beg, real ang, string name, string val)
 real ssize=6mm;
 int AC=0,DC=1,I=2,V=3;
 
-TwoTerminal source(pair beg, int type, real ang, string name, string val)
+TwoTerminal source(pair beg, int type, real ang, string name, string val,
+                  bool draw=true)
 {
   path pLine[]={}, pMisc[]={};
   real len, lchar, lcharv;
@@ -499,7 +527,8 @@ TwoTerminal source(pair beg, int type, real ang, string name, string val)
     pLine.push((.6ssize,-.6ssize)--(.6ssize,.6ssize));
   }
   term = TwoTerminal(beg, len, ang, lchar, lcharv, name, val, pLine, pMisc);
-  term.draw();
+  if (draw == true)
+    term.draw();
   return term;
 }
 
@@ -943,65 +972,4 @@ vardef rheostat@#(expr z,type,ang)=
        draw reo rotatedaround(z,ang) withpen line;
 enddef;
 
-// Definiciones de las longitudes de centrado,
-// las hago como macros para que cambien de
-// valor cuando lo hagan las dimensiones
-// caracter'isticas de cada s'imbolo.
-
-def res = 3.5rstlth enddef;            % 1
-def ind = 3coil enddef;                        % 2
-def cap = 3.5platsep enddef;   % 3
-def sac = ssize enddef;                        % 4
-def sv = ssize enddef;                 % 5
-def si = ssize enddef;                 % 6
-def sdc = .5ssize enddef;              % 7
-def mot = ssize enddef;                        % 8
-def gen = ssize enddef;                        % 9
-def tra = 4coil enddef;                        %10
-def ins = ssize enddef;                        %11
-def dio = 1.5diodeht enddef;   %12
-def bjt = .5bjtlth enddef;             %13
-def imp = .75implth enddef;            %14
-def lam = ssize enddef;                        %15
-def swt = 1.2ssep enddef;              %16
-def bat = .9ssize enddef;              %17
-def cur = 1.5platsep enddef;   %18
-
-// Variable to find the center of two pins, for aligning symbols. It returns the point
-// and the input angle for the symbol that we want to place.
-
-struct Align {
-  pair c;
-  numeric phi;
-}
-
-Align centreof@#(pair pin_beg, pair pin_end, lchar)=
-       pair c@#; numeric phi@#;
-               c@#= -lchar*dir angle(xpart(pin_end)-xpart(pin_beg),
-               ypart(pin_end)-ypart(pin_beg)) shifted .5[pin_beg,pin_end];
-               phi@#=angle(xpart(pin_end)-xpart(pin_beg),
-               ypart(pin_end)-ypart(pin_beg));
-enddef;
-
-// Variable to center an element regarding the pins of another, at 
-// a distance «dist» of the same one. It returns the point in which we should
-// place the symbol that we want to center.
-
-vardef centerto.@#(expr pinref_beg,pinref_end)(expr dist,elem)=
-       pair @#;
-       if (xpart(pinref_beg)=xpart(pinref_end)):
-               @#=(.5[pinref_beg,pinref_end]+(dist,-elem))
-       elseif (ypart(pinref_beg)=ypart(pinref_end)):
-               @#=(.5[pinref_beg,pinref_end]+(-elem,dist))
-       fi
-enddef;
-
-// Macro to scale the circuit when it is concluded.
-
-def scalecirc expr factor =
-       currentpicture=currentpicture scaled factor
-enddef;
-
-// END
-
 */