4 // ---------------------- Charges -------------------------
14 void operator init(pair center=(0,0), real q=1, real radius=2mm, pen outline=currentpen, pen fill=red, Label L="") {
18 this.outline = outline;
23 void draw(picture pic=currentpicture) {
25 path c = scale(radius)*unitcircle;
26 filldraw(picF, c, fill, outline);
28 add(pic, picF, center);
32 Charge pCharge(pair center=(0,0), real q=1, real radius=2mm, pen outline=currentpen, Label L="")
34 Charge c = Charge(center=center, q=q, radius=radius, outline=outline, L=L, fill=red);
38 Charge nCharge(pair center=(0,0), real q=-1, real radius=2mm, pen outline=currentpen, Label L="")
40 Charge c = Charge(center=center, q=q, radius=radius, outline=outline, L=L, fill=blue);
44 // ---------------------- Vectors -------------------------
46 Vector EField(pair center=(0,0), real mag=5mm, real dir=0, Label L="")
48 Vector v = Vector(center=center, mag=mag, dir=dir, L=L, outline=rgb(1,0.5,0.2)); // orange
52 Vector BField(pair center=(0,0), real mag=5mm, real dir=0, Label L="")
54 Vector v = Vector(center=center, mag=mag, dir=dir, L=L, outline=rgb(0.1,1,0.2)); // green
58 Vector Velocity(pair center=(0,0), real mag=5mm, real dir=0, Label L="")
60 Vector v = Vector(center=center, mag=mag, dir=dir, L=L, outline=rgb(1,0.1,0.2)); // red
64 // ---------------------- Forces -------------------------
66 Vector Force(pair center=(0,0), real mag=5mm, real dir=0, Label L="")
68 Vector v = Vector(center=center, mag=mag, dir=dir, L=L, outline=rgb(0.1,0.2,1)); // blue
73 Vector CoulombForce(Charge a, Charge b, Label L="", real scale=1mm, real unit=1mm)
75 pair r = b.center - a.center;
77 mag = ((a.q*b.q)*(scale/length(r))^2)*unit;
79 Vector v = Force(center=b.center, mag=mag, dir=dir, L=L);
83 void CoulombForces(Charge c[], real scale=1mm, real unit=1mm)
87 for (int i=0; i<c.length; i+=1) {
88 for(int j=0; j<c.length; j+=1) {
90 s = "$F_{" + format("%d", i+1) + "," + format("%d", j+1) + "}$";
91 F = CoulombForce(c[i], c[j], L=s, scale=scale, unit=unit);
97 // ---------------------- Measures -------------------------
99 // Distance derived from CAD.MeasuredLine
107 void operator init(pair pFrom=(0,0), pair pTo=(5mm,0), real scale=5mm, pen outline=currentpen, Label L="") {
110 this.outline = outline;
114 void draw(picture pic=currentpicture) {
118 pair pLabelSize = 1.2 * (max(picL)-min(picL));
119 pair pDiff = pTo - pFrom;
120 path p = (0,0)--pDiff;
121 draw(picF, p, outline, Arrows);
123 L = rotate(degrees(pDiff)) * L,
126 + unit(rotate(90)*pDiff) * pLabelSize.y / 2);
127 add(pic, picF, pFrom);
135 real radius; // radius < 0 for exterior angles.
139 void operator init(pair B, pair A, pair C, real radius=5mm, pen outline=currentpen, Label L="") {
143 this.radius = radius;
144 this.outline = outline;
148 void draw(picture pic=currentpicture) {
152 pair pLabelSize = 1.2 * (max(picL)-min(picL));
153 path p = arc(B-A, (0,0), C-A, radius);
154 real t = reltime(p, 0.5);
155 pair P = midpoint(p);
156 pair tang = dir(p, t);
158 draw(picF, p, outline);
160 L = rotate(tang) * L,
162 P + unit(P) * pLabelSize.y / 2);
169 // ---------------------- Shapes -------------------------
171 // TODO: ring, plate, block, cylinder, spring, table