From: W. Trevor King Date: Fri, 20 May 2011 17:57:05 +0000 (-0400) Subject: Add TwoTerminal.shift() and kirchhoff_loop() to Circ.asy. X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=66626df6e386476a56118e211e8c9bf4913d1ed5;p=course.git Add TwoTerminal.shift() and kirchhoff_loop() to Circ.asy. --- diff --git a/asymptote/Circ-test.asy b/asymptote/Circ-test.asy index 309dacc..e8030e0 100644 --- a/asymptote/Circ-test.asy +++ b/asymptote/Circ-test.asy @@ -23,9 +23,9 @@ real u = 2cm, v=0; write("resistor"); TwoTerminal Rn = resistor((0,v), 0, normal, "$R_{normal}$", "30\ohm"); -dot("beg", Rn.beg, NW); -dot("end", Rn.end, NE); -dot("mid", Rn.mid, S); +dot("beg", Rn.beg, NW, red); +dot("end", Rn.end, NE, red); +dot("mid", Rn.mid, S, red); TwoTerminal Rvb = resistor((2u,v), 0, variable, "$R_{variable}$", "30\kohm"); v -= u; @@ -98,3 +98,20 @@ dot("rlsq w/d", (4u,v), N); dot("b", (9u/2,v-u/2), S); wire((4u,v), (9u/2,v-u/2), rlsq, u/4); v -= u; + +write("circuit"); +v -= u/2; // this circuit takes up more height than the previous lines. +TwoTerminal Ccirc = capacitor((0,v), ang=90); +TwoTerminal Rcirc = resistor(draw=false); +Rcirc.centerto(Ccirc.beg, Ccirc.end); +Rcirc.shift((u,0)); // gratuitous use of shift, but whatever... +Rcirc.draw(); +// In the following, we assume the resistor is longer than the +// capacitor. If that is not true, you can find the corners of the +// circuit programatically and use those corners. +wire(Rcirc.end, Ccirc.end, rlsq); +wire(Rcirc.beg, Ccirc.beg, rlsq); +pair Pcirc[] = {(Ccirc.beg.x,Rcirc.beg.y), (Ccirc.end.x,Rcirc.end.y), + Rcirc.end, Rcirc.beg}; +kirchhoff_loop(Pcirc); +v -= u; diff --git a/asymptote/Circ.asy b/asymptote/Circ.asy index 42c889e..6a9c2db 100644 --- a/asymptote/Circ.asy +++ b/asymptote/Circ.asy @@ -40,8 +40,9 @@ int labeling = rotatelabel; real linewd = 0.25mm; -pen line = makepen(scale(0.5*linewd)*unitcircle); -pen misc = makepen(scale(0.4*linewd)*unitcircle); +pen line = linewidth(linewd); +pen misc = linewidth(0.8*linewd); +pen kirchhoff_pen = linewidth(10*linewd)+gray(0.7); // Arrowhead definitions @@ -238,6 +239,13 @@ struct TwoTerminal { add(pic, picL, (end-beg)/2); } + /* Shift position by a */ + void shift(pair a) { + this.beg += a; + this.end += a; + this.mid += a; + } + /* 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) @@ -983,3 +991,25 @@ vardef rheostat@#(expr z,type,ang)= enddef; */ + +// Loop symbols for Kirchhoff's rules. + +void kirchhoff_loop(picture pic=currentpicture, pair points[], + pen outline=kirchhoff_pen, real aspace=-1) { + // draw kirchhoff loop underneath currentpicture + picture newpic; + int i; + guide g; + pair a = points[0] - points[points.length-1]; // arrow distance + if (aspace < 0) + aspace = 3*linewidth(outline); // one dot diameter + real alength = max(0.5*length(a), length(a) - aspace - linewidth(outline)/2); + a = alength*unit(a); + pair astop = points[points.length-1] + a; + dot(newpic, points[0], scale(3)*outline); + for (int i=0; i < points.length; ++i) + g = g--points[i]; + g = g -- astop; + draw(newpic, g, outline, Arrow(size=3*linewidth(outline))); + add(pic, newpic, above=false); +}