d37c73803ad13bf2e7110de49fa97e0125bef397
[course.git] / asymptote / ElectroMag.asy
1 /* Useful functions for drawing Physics 102 figures.
2  *
3  * Copyright (C) 2008-2009 W. Trevor King <wking@drexel.edu>
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 import geometry;
21 import Mechanics;
22
23
24 // ---------------------- Charges -------------------------
25
26 // charged particle
27 struct Charge {
28   LabeledCircle lc;
29   real q;
30   
31   void operator init(pair center=(0,0), real q=1, real radius=2mm,
32                      pen outline=currentpen, pen fill=red, Label L="") {
33     this.lc.operator init(center=center, radius=radius, outline=outline,
34                           fill=fill, L=L);
35     this.q = q;
36   }
37
38   pair center() { return this.lc.center; }
39   void set_center(pair center) { this.lc.center = center; }
40   void draw(picture pic=currentpicture) = this.lc.draw;
41 }
42
43 // positive charge
44 Charge pCharge(pair center=(0,0), real q=1, real radius=2mm,
45                pen outline=currentpen, Label L="")
46 {
47   return Charge(center=center, q=q, radius=radius, outline=outline, fill=red,
48                 L=L);
49 }
50
51 // negative charge
52 Charge nCharge(pair center=(0,0), real q=-1, real radius=2mm,
53                pen outline=currentpen, Label L="")
54 {
55   return Charge(center=center, q=q, radius=radius, outline=outline, fill=blue,
56                 L=L);
57 }
58
59 // neutral charge
60 Charge neutralCharge(pair center=(0,0), real radius=2mm,
61                      pen outline=currentpen, Label L="")
62 {
63   return Charge(center=center, q=0, radius=radius, outline=outline, fill=grey,
64                 L=L);
65 }
66
67 // auto-signed charge
68 Charge aCharge(pair center=(0,0), real q=1, real radius=2mm,
69                pen outline=currentpen, Label L="")
70 {
71   Charge c;
72   if (q > 0) {
73     c = pCharge(center, q, radius, outline, L);
74   } else if (q < 0) {
75     c = nCharge(center, q, radius, outline, L);
76   } else {
77     c = neutralCharge(center, radius, outline, L);
78   }
79   return c;
80 }
81
82 // ---------------------- Vectors -------------------------
83
84 pen EFieldPen = rgb(1,0.5,0.2);    // orange
85 pen BFieldPen = rgb(0.1,1,0.2);    // green
86 pen CurrentPen = rgb(0.8,0.1,0.8); // purple
87
88 // electric field
89 Vector EField(pair center=(0,0), real mag=5mm, real dir=0, real phi=0, Label L="")
90 {
91   Vector v = Vector(center=center, mag=mag, dir=dir, phi=phi, L=L, outline=EFieldPen);
92   return v;
93 }
94
95 // magnetic field
96 Vector BField(pair center=(0,0), real mag=5mm, real dir=0, real phi=0, Label L="")
97 {
98   Vector v = Vector(center=center, mag=mag, dir=dir, phi=phi, L=L, outline=BFieldPen);
99   return v;
100 }
101
102 Vector Current(pair center=(0,0), real mag=5mm, real dir=0, real phi=0, Label L="")
103 {
104   Vector v = Vector(center=center, mag=mag, dir=dir, phi=phi, L=L, outline=CurrentPen);
105   return v;
106 }
107
108 // ---------------------- Forces -------------------------
109
110 // Force of a on b
111 Vector CoulombForce(Charge a, Charge b, Label L="", real scale=1mm, real unit=1mm)
112 {
113   pair r = b.center() - a.center();
114   real mag, dir;
115   mag = ((a.q*b.q)*(scale/length(r))^2)*unit;
116   dir = degrees(r);
117   Vector v = Force(center=b.center(), mag=mag, dir=dir, L=L);
118   return v;
119 }
120
121 void CoulombForces(Charge c[], real scale=1mm, real unit=1mm)
122 {
123   Vector F;
124   string s;
125   for (int i=0; i<c.length; i+=1) {
126     for(int j=0; j<c.length; j+=1) {
127       if (i==j) continue;
128       s = "$F_{" + format("%d", i+1) + "," + format("%d", j+1) + "}$";
129       F = CoulombForce(c[i], c[j], L=s, scale=scale, unit=unit);
130       F.draw();
131     }
132   }
133 }
134
135