Debian Codegraph @

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

raw
1 % lawofcosines.mp
2 % L. Nobre G.
3 % 2007
4
5 prologues := 1;
6
7 beginfig(1);
8 numeric u, limit, major, minor, abvcos, blwcos;
9 color gridc;
10 pen gridpmaj, gridpmin, dotp;
11 u=0.7mm;
12 limit=64;
13 major=20;
14 minor=2;
15 abvcos=0.5;
16 blwcos=1/3;
17 gridc=0.5white;
18 gridpmaj=pencircle scaled 2pt;
19 gridpmin=pencircle scaled 1.3pt;
20 dotp=pencircle scaled 2.7pt;
21 % labeloffset := 3u;
22 numeric i,j, abvsin, blwsin, stpmaj, stpmin;
23 abvsin=1 +-+ abvcos;
24 blwsin=1 +-+ blwcos;
25 stpmaj=major*u;
26 stpmin=minor*u;
27 pair abvdir, blwdir, thedir;
28 abvdir=(abvcos,abvsin);
29 blwdir=(blwcos,-blwsin);
30 boolean ismajor, isaxe;
31 drawoptions( withpen gridpmin withcolor gridc );
32 for i=-limit step minor until limit:
33 ismajor := not ((i mod major)<>0);
34 if ismajor:
35 isaxe := not (i <> 0);
36 if isaxe:
37 drawoptions( withpen gridpmaj );
38 else:
39 drawoptions( withpen gridpmaj withcolor gridc );
40 fi;
41 fi;
42 draw ((i*u,0)+limit*u*blwdir)--(i*u,0)--((i*u,0)+limit*u*abvdir);
43 if i<0:
44 thedir := -blwdir;
45 else:
46 thedir := abvdir;
47 fi;
48 draw ((-limit*u,0)+i*u*thedir)--((limit*u,0)+i*u*thedir);
49 if ismajor:
50 drawoptions( );
51 label.bot( decimal( abs( i ) ), (i*u,0)+limit*u*blwdir );
52 if i<0:
53 label.llft( decimal( abs( i ) ), (-limit*u,0)+i*u*thedir );
54 elseif not (i<>0):
55 label.lft( decimal( abs( i ) ), (-limit*u,0)+i*u*thedir );
56 else:
57 label.ulft( decimal( abs( i ) ), (-limit*u,0)+i*u*thedir );
58 fi;
59 label.top( decimal( abs( i ) ), (i*u,0)+limit*u*abvdir );
60 drawoptions( withpen gridpmin withcolor gridc );
61 fi;
62 endfor;
63 drawoptions( withpen dotp withcolor red );
64 numeric signal;
65 for i=-limit upto limit:
66 if i <> 0:
67 if i<0:
68 signal := -1;
69 else:
70 signal := 1;
71 fi;
72 for j=-limit upto -2:
73 if not ( ( sqrt((i**2)+(j**2)+signal*2*abs(i*j)/3) mod 1 ) <> 0 ):
74 draw (i*u,0)-j*u*blwdir;
75 fi;
76 endfor;
77 for j=limit downto 2:
78 if not ( ( sqrt((i**2)+(j**2)+signal*2*abs(i*j)*abvcos) mod 1 ) <> 0 ):
79 draw (i*u,0)+j*u*abvdir;
80 fi;
81 endfor;
82 fi;
83 endfor;
84
85 endfig;
86
87 beginfig(2);
88 numeric u, limit, major, minor, abvcos, blwcos;
89 color gridc;
90 pen gridpmaj, gridpmin, dotp;
91 u=0.7mm;
92 limit=24;
93 major=20;
94 minor=2;
95 abvcos=0.8;
96 blwcos=0.9;
97 gridc=0.5white;
98 gridpmaj=pencircle scaled 1.3pt;
99 gridpmin=pencircle scaled 0.7pt;
100 dotp=pencircle scaled 1.4pt;
101 % labeloffset := 3u;
102 numeric i,j, abvsin, blwsin, stpmaj, stpmin;
103 abvsin=1 +-+ abvcos;
104 blwsin=1 +-+ blwcos;
105 stpmaj=major*u;
106 stpmin=minor*u;
107 pair abvdir, blwdir, thedir;
108 abvdir=(abvcos,abvsin);
109 blwdir=(blwcos,-blwsin);
110 boolean ismajor, isaxe;
111 drawoptions( withpen gridpmin withcolor gridc );
112 for i=-limit step minor until limit:
113 ismajor := not ((i mod major)<>0);
114 if ismajor:
115 isaxe := not (i <> 0);
116 if isaxe:
117 drawoptions( withpen gridpmaj );
118 else:
119 drawoptions( withpen gridpmaj withcolor gridc );
120 fi;
121 fi;
122 draw ((i*u,0)+limit*u*blwdir)--(i*u,0)--((i*u,0)+limit*u*abvdir);
123 if i<0:
124 thedir := -blwdir;
125 else:
126 thedir := abvdir;
127 fi;
128 draw ((-limit*u,0)+i*u*thedir)--((limit*u,0)+i*u*thedir);
129 if ismajor:
130 drawoptions( );
131 label.bot( decimal( abs( i ) ), (i*u,0)+limit*u*blwdir );
132 if i<0:
133 label.llft( decimal( abs( i ) ), (-limit*u,0)+i*u*thedir );
134 elseif not (i<>0):
135 label.lft( decimal( abs( i ) ), (-limit*u,0)+i*u*thedir );
136 else:
137 label.ulft( decimal( abs( i ) ), (-limit*u,0)+i*u*thedir );
138 fi;
139 label.top( decimal( abs( i ) ), (i*u,0)+limit*u*abvdir );
140 drawoptions( withpen gridpmin withcolor gridc );
141 fi;
142 endfor;
143 drawoptions( withpen dotp withcolor red );
144 numeric signal;
145 for i=-limit upto limit:
146 if i <> 0:
147 if i<0:
148 signal := -1;
149 else:
150 signal := 1;
151 fi;
152 for j=-limit upto -2:
153 if not ( ( sqrt((i**2)+(j**2)+signal*18*abs(i*j)/10) mod 1 ) <> 0 ):
154 draw (i*u,0)-j*u*blwdir;
155 fi;
156 endfor;
157 for j=limit downto 2:
158 if not ( ( sqrt((i**2)+(j**2)+signal*8*abs(i*j)/5) mod 1 ) <> 0 ):
159 draw (i*u,0)+j*u*abvdir;
160 fi;
161 endfor;
162 fi;
163 endfor;
164 draw origin withcolor blue withpen dotp;
165 endfig;
166
167 end.