summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/doc/metapost/featpost/example/lawofcosines.mp
blob: 5d49e0adcdd10f312553cafc54dcc5848e7c2837 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
% lawofcosines.mp
% L. Nobre G.
% 2007

prologues := 1;

beginfig(1);
  numeric u, limit, major, minor, abvcos, blwcos;
  color gridc;
  pen gridpmaj, gridpmin, dotp;
  u=0.7mm;
  limit=64;
  major=20;
  minor=2;
  abvcos=0.5;
  blwcos=1/3;
  gridc=0.5white;
  gridpmaj=pencircle scaled 2pt;
  gridpmin=pencircle scaled 1.3pt;
  dotp=pencircle scaled 2.7pt;
%  labeloffset := 3u;
  numeric i,j, abvsin, blwsin, stpmaj, stpmin;
  abvsin=1 +-+ abvcos;
  blwsin=1 +-+ blwcos;
  stpmaj=major*u;
  stpmin=minor*u;
  pair abvdir, blwdir, thedir;
  abvdir=(abvcos,abvsin);
  blwdir=(blwcos,-blwsin);
  boolean ismajor, isaxe;
  drawoptions( withpen gridpmin withcolor gridc );
  for i=-limit step minor until limit:
    ismajor := not ((i mod major)<>0);
    if ismajor:
      isaxe := not (i <> 0);
      if isaxe:
	drawoptions( withpen gridpmaj );
      else:
	drawoptions( withpen gridpmaj withcolor gridc );
      fi;
    fi;
    draw ((i*u,0)+limit*u*blwdir)--(i*u,0)--((i*u,0)+limit*u*abvdir);    
    if i<0:
      thedir := -blwdir;
    else:
      thedir := abvdir;
    fi;
    draw ((-limit*u,0)+i*u*thedir)--((limit*u,0)+i*u*thedir);
    if ismajor:
      drawoptions( );
      label.bot( decimal( abs( i ) ), (i*u,0)+limit*u*blwdir );
      if i<0:
	label.llft( decimal( abs( i ) ), (-limit*u,0)+i*u*thedir );
      elseif not (i<>0):
	label.lft( decimal( abs( i ) ), (-limit*u,0)+i*u*thedir );
      else:
	label.ulft( decimal( abs( i ) ), (-limit*u,0)+i*u*thedir );
      fi;
      label.top( decimal( abs( i ) ), (i*u,0)+limit*u*abvdir );
      drawoptions( withpen gridpmin withcolor gridc );
    fi;    
  endfor;
  drawoptions( withpen dotp withcolor red );
  numeric signal;
  for i=-limit upto limit:
    if i <> 0:
      if i<0:
	signal := -1;
      else:
	signal := 1;
      fi;
      for j=-limit upto -2:
	if not ( ( sqrt((i**2)+(j**2)+signal*2*abs(i*j)/3) mod 1 ) <> 0 ):
	  draw (i*u,0)-j*u*blwdir;
	fi;
      endfor;
      for j=limit downto 2:
	if not ( ( sqrt((i**2)+(j**2)+signal*2*abs(i*j)*abvcos) mod 1 ) <> 0 ):
	  draw (i*u,0)+j*u*abvdir;
	fi;
      endfor;
    fi;
  endfor;
  
endfig;

beginfig(2);
  numeric u, limit, major, minor, abvcos, blwcos;
  color gridc;
  pen gridpmaj, gridpmin, dotp;
  u=0.7mm;
  limit=24;
  major=20;
  minor=2;
  abvcos=0.8;
  blwcos=0.9;
  gridc=0.5white;
  gridpmaj=pencircle scaled 1.3pt;
  gridpmin=pencircle scaled 0.7pt;
  dotp=pencircle scaled 1.4pt;
%  labeloffset := 3u;
  numeric i,j, abvsin, blwsin, stpmaj, stpmin;
  abvsin=1 +-+ abvcos;
  blwsin=1 +-+ blwcos;
  stpmaj=major*u;
  stpmin=minor*u;
  pair abvdir, blwdir, thedir;
  abvdir=(abvcos,abvsin);
  blwdir=(blwcos,-blwsin);
  boolean ismajor, isaxe;
  drawoptions( withpen gridpmin withcolor gridc );
  for i=-limit step minor until limit:
    ismajor := not ((i mod major)<>0);
    if ismajor:
      isaxe := not (i <> 0);
      if isaxe:
	drawoptions( withpen gridpmaj );
      else:
	drawoptions( withpen gridpmaj withcolor gridc );
      fi;
    fi;
    draw ((i*u,0)+limit*u*blwdir)--(i*u,0)--((i*u,0)+limit*u*abvdir);    
    if i<0:
      thedir := -blwdir;
    else:
      thedir := abvdir;
    fi;
    draw ((-limit*u,0)+i*u*thedir)--((limit*u,0)+i*u*thedir);
    if ismajor:
      drawoptions( );
      label.bot( decimal( abs( i ) ), (i*u,0)+limit*u*blwdir );
      if i<0:
	label.llft( decimal( abs( i ) ), (-limit*u,0)+i*u*thedir );
      elseif not (i<>0):
	label.lft( decimal( abs( i ) ), (-limit*u,0)+i*u*thedir );
      else:
	label.ulft( decimal( abs( i ) ), (-limit*u,0)+i*u*thedir );
      fi;
      label.top( decimal( abs( i ) ), (i*u,0)+limit*u*abvdir );
      drawoptions( withpen gridpmin withcolor gridc );
    fi;    
  endfor;
  drawoptions( withpen dotp withcolor red );
  numeric signal;
  for i=-limit upto limit:
    if i <> 0:
      if i<0:
	signal := -1;
      else:
	signal := 1;
      fi;
      for j=-limit upto -2:
	if not ( ( sqrt((i**2)+(j**2)+signal*18*abs(i*j)/10) mod 1 ) <> 0 ):
	  draw (i*u,0)-j*u*blwdir;
	fi;
      endfor;
      for j=limit downto 2:
	if not ( ( sqrt((i**2)+(j**2)+signal*8*abs(i*j)/5) mod 1 ) <> 0 ):
	  draw (i*u,0)+j*u*abvdir;
	fi;
      endfor;
    fi;
  endfor;
  draw origin withcolor blue withpen dotp;
endfig;

end.