Debian Codegraph @

texmf-dist/doc/metapost/featpost/example/fieldlinesnormapaper.mp

raw
1 % fieldlinesnormapaper.mp
2 % L. Nobre G.
3 % 2009
4
5 input featpost3Dplus2D;
6
7 Spread := 25;
8 f := (20,0,0);
9
10 color posa, posb, perpd;
11 numeric limy, limz, bord;
12 posa = (0,-3,3); %%%%%%%% allways make Y(posa) < Y(posb) %%%%%%%%%%%
13 posb = (0,3,-2); %%%%%%%%%%%% never make Y(posa) = Y(posb) %%%%%%%%%
14 perpd = (1,0,0);
15 limy = 7;
16 limz = 5;
17 bord = 2.5;
18
19 def vecfunc( expr posit )=
20 begingroup
21 save field;
22 color field, horiz, verti, vech, vecv;
23 numeric modula, modulb, ypos, zpos, hd, hf, vd, vf, zref, yref;
24 modula = conorm( posit-posa );
25 modulb = conorm( posit-posb );
26 field = N( ( posit-posa )/(modula**3)-( posit-posb )/(modulb**3) );
27 ypos = Y( posit );
28 zpos = Z( posit );
29 if Z(posa) <> Z(posb):
30 (ypos,zref) = whatever[(Y(posa),Z(posa)),(Y(posb),Z(posb))];
31 (yref,zpos) = whatever[(Y(posa),Z(posa)),(Y(posb),Z(posb))];
32 if ( abs(zpos) > limz-bord ):
33 hd = abs(zpos)-limz+bord;
34 if ypos < yref:
35 if zpos > 0:
36 vech = -green;
37 else:
38 vech = green;
39 fi;
40 else:
41 if zpos > 0:
42 vech = green;
43 else:
44 vech = -green;
45 fi;
46 fi;
47 else:
48 hd = 0;
49 vech = black;
50 fi;
51 else:
52 zref = Z(posa);
53 vech = green;
54 if ( abs(zpos) > limz-bord ):
55 hd = abs(zpos)-limz+bord;
56 else:
57 hd = 0;
58 fi;
59 fi;
60 if ( abs(ypos) > limy-bord ):
61 vd = abs(ypos)-limy+bord;
62 if zpos > zref:
63 if ypos < 0:
64 vecv = blue;
65 else:
66 vecv = -blue;
67 fi;
68 else:
69 if ypos < 0:
70 vecv = -blue;
71 else:
72 vecv = blue;
73 fi;
74 fi;
75 else:
76 vd = 0;
77 vecv = black;
78 fi;
79 hf = (hd/bord)**2;
80 vf = (vd/bord)**2;
81 ( N(field*(0.975-hf)*(0.975-vf)+hf*vech+vf*vecv) )
82 endgroup
83 enddef;
84
85 def potfunc( expr posit )=
86 begingroup
87 color field;
88 field = vecfunc( posit );
89 ( ncrossprod( field, perpd ) )
90 endgroup
91 enddef;
92
93 beginfig(1);
94 numeric i, lena, lenb, numa, numb, sa, sb, diffstep, ray;
95 numeric j, gridstep, fac;
96 color stp, grdp, locv;
97 path oneline;
98 pen grossa, fina;
99 grossa = pencircle scaled 3pt;
100 fina = pencircle scaled 2pt;
101 lena = 60;
102 lenb = 40;
103 numa = 15;
104 numb = 30;
105 diffstep = 0.1;
106 ray = 0.5;
107 sa = 40;
108 sb = 5;
109 fac = 0.275;
110 gridstep = 0.3;
111 for i=-limy+0.5*gridstep step gridstep until limy-0.5*gridstep:
112 for j=-limz+0.5*gridstep step gridstep until limz-0.5*gridstep:
113 grdp := (0,i,j);
114 locv := 0.5*fac*vecfunc( grdp );
115 drawarrow rp(grdp-locv)--rp(grdp+locv);
116 endfor;
117 endfor;
118 for i=sa step numa until (360-sa):
119 stp := posa+( 0, ray*cosd(i), ray*sind(i) );
120 oneline := fieldlinepath( lena, stp, diffstep, vecfunc );
121 draw oneline withpen grossa;
122 draw oneline withpen fina withcolor background;
123 endfor;
124 for i=sb step numb until (360-sb):
125 stp := posb+( 0, ray*cosd(i), ray*sind(i) );
126 oneline := fieldlinepath( lenb, stp, -diffstep, vecfunc );
127 draw oneline withpen grossa;
128 draw oneline withpen fina withcolor background;
129 endfor;
130 drawoptions(withpen grossa);
131 draw rp(posa);
132 draw rp(posb);
133 draw rp((0,-limy,-limz))--rp((0, limy,-limz))--
134 rp((0, limy, limz))--rp((0,-limy, limz))--cycle;
135 drawoptions();
136 endfig;
137
138 beginfig(2);
139 numeric i, lena, numa, diffstep, fac, gridstep, margnum;
140 color stp, grdp, locv;
141 path oneline, twoline, bordeline;
142 pen grossa, fina;
143 grossa = pencircle scaled 3pt;
144 fina = pencircle scaled 2pt;
145 lena = 85;
146 numa = 13;
147 margnum = 2;
148 diffstep = 0.2;
149 fac = 0.275;
150 gridstep = 0.3;
151 for i=-limy+0.5*gridstep step gridstep until limy-0.5*gridstep:
152 for j=-limz+0.5*gridstep step gridstep until limz-0.5*gridstep:
153 grdp := (0,i,j);
154 locv := 0.5*fac*potfunc( grdp );
155 drawarrow rp(grdp-locv)--rp(grdp+locv);
156 endfor;
157 endfor;
158 for i=margnum upto numa-margnum:
159 stp := (i/numa)[posa,posb];
160 oneline := fieldlinepath( lena, stp, diffstep, potfunc );
161 twoline := fieldlinepath( lena, stp, -diffstep, potfunc );
162 draw oneline withpen grossa;
163 draw twoline withpen grossa;
164 endfor;
165 drawoptions(withpen grossa);
166 draw rp(posa);
167 draw rp(posb);
168 bordeline = rp((0,-limy,-limz))--rp((0, limy,-limz))--
169 rp((0, limy, limz))--rp((0,-limy, limz))--cycle;
170 clip currentpicture to bordeline;
171 draw bordeline;
172 drawoptions(withpen fina);
173 numeric side;
174 path aux, electri;
175 side = 10mm;
176 aux = origin--(side,0)--(side,side)--cycle;
177 z0 = 0.667[origin,(side,0.5*side)];
178 electri = aux shifted (-z0) rotated (45+angle(rp(posb)-rp(posa)));
179 fill electri shifted rp(posb);
180 fill electri rotated 180 shifted rp(posa);
181 endfig;
182
183 end.