Added licensing information to asymptote files
[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 // ---------------------- Charges -------------------------
24
25 // charged particle
26 struct Charge {
27   pair center;
28   real q;
29   real radius;
30   pen outline;
31   pen fill;
32   Label L;
33   
34   void operator init(pair center=(0,0), real q=1, real radius=2mm, pen outline=currentpen, pen fill=red, Label L="") {
35     this.center = center;
36     this.q = q;
37     this.radius = radius;
38     this.outline = outline;
39     this.fill = fill;
40     this.L = L;
41   }
42   
43   void draw(picture pic=currentpicture) {
44     picture picF;
45     picture picL;
46     label(picL, L);
47     pair pLabelSize = 1.2 * (max(picL)-min(picL));
48     path c = scale(radius)*unitcircle;
49     filldraw(picF, c, fill, outline);
50     label(pic = picF,
51           L = L,
52           position = (0,-(radius+pLabelSize.y/2)));
53     add(pic, picF, center);
54   }
55 }
56
57 // positive charge
58 Charge pCharge(pair center=(0,0), real q=1, real radius=2mm, pen outline=currentpen, Label L="")
59 {
60   Charge c = Charge(center=center, q=q, radius=radius, outline=outline, L=L, fill=red);
61   return c;
62 }
63
64 // negative charge
65 Charge nCharge(pair center=(0,0), real q=-1, real radius=2mm, pen outline=currentpen, Label L="")
66 {
67   Charge c = Charge(center=center, q=q, radius=radius, outline=outline, L=L, fill=blue);
68   return c;
69 }
70
71 // neutral charge
72 Charge neutralCharge(pair center=(0,0), real radius=2mm, pen outline=currentpen, Label L="")
73 {
74   Charge c = Charge(center=center, q=0, radius=radius, outline=outline, L=L, fill=grey);
75   return c;
76 }
77
78 // auto-signed charge
79 Charge aCharge(pair center=(0,0), real q=1, real radius=2mm, pen outline=currentpen, Label L="")
80 {
81   Charge c;
82   if (q > 0) {
83     c = pCharge(center, q, radius, outline, L);
84   } else if (q < 0) {
85     c = nCharge(center, q, radius, outline, L);
86   } else {
87     c = neutralCharge(center, radius, outline, L);
88   }
89   return c;
90 }
91
92 // ---------------------- Vectors -------------------------
93
94 // electric field
95 Vector EField(pair center=(0,0), real mag=5mm, real dir=0, Label L="")
96 {
97   Vector v = Vector(center=center, mag=mag, dir=dir, L=L, outline=rgb(1,0.5,0.2)); // orange
98   return v;
99 }
100
101 // magnetic field
102 Vector BField(pair center=(0,0), real mag=5mm, real dir=0, Label L="")
103 {
104   Vector v = Vector(center=center, mag=mag, dir=dir, L=L, outline=rgb(0.1,1,0.2)); // green
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