1 /* Useful functions for drawing Physics 102 figures.
3 * Copyright (C) 2008-2009 W. Trevor King <wking@drexel.edu>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 // ---------------------- Charges -------------------------
35 void operator init(pair center=(0,0), real q=1, real radius=2mm, pen outline=currentpen, pen fill=red, Label L="") {
39 this.outline = outline;
44 void draw(picture pic=currentpicture) {
48 pair pLabelSize = 1.2 * (max(picL)-min(picL));
49 path c = scale(radius)*unitcircle;
50 filldraw(picF, c, fill, outline);
53 position = (0,-(radius+pLabelSize.y/2)));
54 add(pic, picF, center);
59 Charge pCharge(pair center=(0,0), real q=1, real radius=2mm, pen outline=currentpen, Label L="")
61 Charge c = Charge(center=center, q=q, radius=radius, outline=outline, L=L, fill=red);
66 Charge nCharge(pair center=(0,0), real q=-1, real radius=2mm, pen outline=currentpen, Label L="")
68 Charge c = Charge(center=center, q=q, radius=radius, outline=outline, L=L, fill=blue);
73 Charge neutralCharge(pair center=(0,0), real radius=2mm, pen outline=currentpen, Label L="")
75 Charge c = Charge(center=center, q=0, radius=radius, outline=outline, L=L, fill=grey);
80 Charge aCharge(pair center=(0,0), real q=1, real radius=2mm, pen outline=currentpen, Label L="")
84 c = pCharge(center, q, radius, outline, L);
86 c = nCharge(center, q, radius, outline, L);
88 c = neutralCharge(center, radius, outline, L);
93 // ---------------------- Vectors -------------------------
95 pen EFieldPen = rgb(1,0.5,0.2); // orange
96 pen BFieldPen = rgb(0.1,1,0.2); // green
97 pen CurrentPen = rgb(0.8,0.1,0.8); // purple
100 Vector EField(pair center=(0,0), real mag=5mm, real dir=0, real phi=0, Label L="")
102 Vector v = Vector(center=center, mag=mag, dir=dir, phi=phi, L=L, outline=EFieldPen);
107 Vector BField(pair center=(0,0), real mag=5mm, real dir=0, real phi=0, Label L="")
109 Vector v = Vector(center=center, mag=mag, dir=dir, phi=phi, L=L, outline=BFieldPen);
113 Vector Current(pair center=(0,0), real mag=5mm, real dir=0, real phi=0, Label L="")
115 Vector v = Vector(center=center, mag=mag, dir=dir, phi=phi, L=L, outline=CurrentPen);
119 // ---------------------- Forces -------------------------
122 Vector CoulombForce(Charge a, Charge b, Label L="", real scale=1mm, real unit=1mm)
124 pair r = b.center - a.center;
126 mag = ((a.q*b.q)*(scale/length(r))^2)*unit;
128 Vector v = Force(center=b.center, mag=mag, dir=dir, L=L);
132 void CoulombForces(Charge c[], real scale=1mm, real unit=1mm)
136 for (int i=0; i<c.length; i+=1) {
137 for(int j=0; j<c.length; j+=1) {
139 s = "$F_{" + format("%d", i+1) + "," + format("%d", j+1) + "}$";
140 F = CoulombForce(c[i], c[j], L=s, scale=scale, unit=unit);