fieldscatterex1.html
19.8 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
<div style="font-weight: bold;margin-left:15px;">fieldscatterex1.php</div><link rel="stylesheet" href="../phphl.css" type="text/css"><div class="hl-main"><table class="hl-table" width="100%"><tr><td class="hl-gutter" align="right" valign="top"><pre>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
</pre></td><td class="hl-main" valign="top"><pre><span class="hl-inlinetags"><?php</span><span class="hl-code"> </span><span class="hl-comment">//</span><span class="hl-comment"> content="text/plain; charset=utf-8"</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-reserved">require_once</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">jpgraph/jpgraph.php</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-reserved">require_once</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">jpgraph/jpgraph_scatter.php</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-var">$polex</span><span class="hl-code"> = </span><span class="hl-number">6</span><span class="hl-code">;
</span><span class="hl-var">$poley</span><span class="hl-code"> = </span><span class="hl-number">40</span><span class="hl-code">;
</span><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-identifier">FldCallback</span><span class="hl-brackets">(</span><span class="hl-var">$x</span><span class="hl-code">,</span><span class="hl-var">$y</span><span class="hl-code">,</span><span class="hl-var">$a</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-reserved">GLOBAL</span><span class="hl-code"> </span><span class="hl-var">$polex</span><span class="hl-code">, </span><span class="hl-var">$poley</span><span class="hl-code">;
</span><span class="hl-var">$maxr</span><span class="hl-code"> = </span><span class="hl-number">3000</span><span class="hl-code">;
</span><span class="hl-comment">//</span><span class="hl-comment"> Size and arrow size is constant</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-var">$size</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-quotes">"</span><span class="hl-code">;
</span><span class="hl-var">$arrowsize</span><span class="hl-code">=</span><span class="hl-quotes">"</span><span class="hl-quotes">"</span><span class="hl-code">;
</span><span class="hl-comment">//</span><span class="hl-comment"> Since we have different scales we need the data points</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-comment">//</span><span class="hl-comment"> to be of the same magnitude to give it a distance</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-comment">//</span><span class="hl-comment"> interpretation.</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-var">$x</span><span class="hl-code"> *= </span><span class="hl-number">10</span><span class="hl-code">;
</span><span class="hl-comment">//</span><span class="hl-comment"> Colors gets colder the further out we go from the center</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-var">$r</span><span class="hl-code"> = </span><span class="hl-brackets">(</span><span class="hl-var">$x</span><span class="hl-code">-</span><span class="hl-var">$polex</span><span class="hl-code">*</span><span class="hl-number">10</span><span class="hl-brackets">)</span><span class="hl-code">*</span><span class="hl-brackets">(</span><span class="hl-var">$x</span><span class="hl-code">-</span><span class="hl-var">$polex</span><span class="hl-code">*</span><span class="hl-number">10</span><span class="hl-brackets">)</span><span class="hl-code">+</span><span class="hl-brackets">(</span><span class="hl-var">$y</span><span class="hl-code">-</span><span class="hl-var">$poley</span><span class="hl-brackets">)</span><span class="hl-code">*</span><span class="hl-brackets">(</span><span class="hl-var">$y</span><span class="hl-code">-</span><span class="hl-var">$poley</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-var">$f</span><span class="hl-code"> = </span><span class="hl-var">$r</span><span class="hl-code">/</span><span class="hl-var">$maxr</span><span class="hl-code">;
</span><span class="hl-reserved">if</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-var">$f</span><span class="hl-code"> > </span><span class="hl-number">1</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-var">$f</span><span class="hl-code">=</span><span class="hl-number">1</span><span class="hl-code">;
</span><span class="hl-var">$red</span><span class="hl-code"> = </span><span class="hl-identifier">floor</span><span class="hl-brackets">(</span><span class="hl-brackets">(</span><span class="hl-number">1</span><span class="hl-code">-</span><span class="hl-var">$f</span><span class="hl-brackets">)</span><span class="hl-code">*</span><span class="hl-number">255</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-var">$blue</span><span class="hl-code"> = </span><span class="hl-identifier">floor</span><span class="hl-brackets">(</span><span class="hl-var">$f</span><span class="hl-code">*</span><span class="hl-number">255</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-var">$color</span><span class="hl-code"> = </span><span class="hl-reserved">array</span><span class="hl-brackets">(</span><span class="hl-var">$red</span><span class="hl-code">,</span><span class="hl-number">0</span><span class="hl-code">,</span><span class="hl-var">$blue</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-comment">//</span><span class="hl-comment">echo "x=$x, y=$y, blue=$blue, red=$red<br>";</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-reserved">array</span><span class="hl-brackets">(</span><span class="hl-var">$color</span><span class="hl-code">,</span><span class="hl-var">$size</span><span class="hl-code">,</span><span class="hl-var">$arrowsize</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-comment">//</span><span class="hl-comment"> Create data for a simulated pseudo-magnetic radient field</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-var">$datax</span><span class="hl-code"> = </span><span class="hl-reserved">array</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-var">$datay</span><span class="hl-code"> = </span><span class="hl-reserved">array</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-var">$angle</span><span class="hl-code"> = </span><span class="hl-reserved">array</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-reserved">for</span><span class="hl-brackets">(</span><span class="hl-var">$x</span><span class="hl-code">=</span><span class="hl-number">1</span><span class="hl-code">; </span><span class="hl-var">$x</span><span class="hl-code"> < </span><span class="hl-number">10</span><span class="hl-code">; ++</span><span class="hl-var">$x</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-reserved">for</span><span class="hl-brackets">(</span><span class="hl-var">$y</span><span class="hl-code">=</span><span class="hl-number">10</span><span class="hl-code">; </span><span class="hl-var">$y</span><span class="hl-code"><</span><span class="hl-number">100</span><span class="hl-code">; </span><span class="hl-var">$y</span><span class="hl-code"> += </span><span class="hl-number">10</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-var">$a</span><span class="hl-code"> = -</span><span class="hl-number">1</span><span class="hl-code">;
</span><span class="hl-reserved">if</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-var">$x</span><span class="hl-code">==</span><span class="hl-var">$polex</span><span class="hl-code"> && </span><span class="hl-var">$y</span><span class="hl-code">==</span><span class="hl-var">$poley</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-reserved">continue</span><span class="hl-code">;
</span><span class="hl-reserved">if</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-var">$x</span><span class="hl-code">==</span><span class="hl-var">$polex</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-reserved">if</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-var">$y</span><span class="hl-code"> > </span><span class="hl-var">$poley</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-var">$a</span><span class="hl-code">=</span><span class="hl-number">90</span><span class="hl-code">;
</span><span class="hl-reserved">else</span><span class="hl-code"> </span><span class="hl-var">$a</span><span class="hl-code"> = </span><span class="hl-number">270</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-reserved">if</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-var">$y</span><span class="hl-code">==</span><span class="hl-var">$poley</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-reserved">if</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-var">$x</span><span class="hl-code"> > </span><span class="hl-var">$polex</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-var">$a</span><span class="hl-code">=</span><span class="hl-number">0</span><span class="hl-code">;
</span><span class="hl-reserved">else</span><span class="hl-code"> </span><span class="hl-var">$a</span><span class="hl-code">=</span><span class="hl-number">180</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-reserved">if</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-var">$a</span><span class="hl-code"> == -</span><span class="hl-number">1</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-var">$d1</span><span class="hl-code"> = </span><span class="hl-var">$y</span><span class="hl-code">-</span><span class="hl-var">$poley</span><span class="hl-code">;
</span><span class="hl-var">$d2</span><span class="hl-code"> = </span><span class="hl-brackets">(</span><span class="hl-var">$polex</span><span class="hl-code">-</span><span class="hl-var">$x</span><span class="hl-brackets">)</span><span class="hl-code">*</span><span class="hl-number">20</span><span class="hl-code">;
</span><span class="hl-reserved">if</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-var">$y</span><span class="hl-code"> < </span><span class="hl-var">$poley</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-var">$d2</span><span class="hl-code"> *= -</span><span class="hl-number">1</span><span class="hl-code">;
</span><span class="hl-var">$h</span><span class="hl-code"> = </span><span class="hl-identifier">sqrt</span><span class="hl-brackets">(</span><span class="hl-var">$d1</span><span class="hl-code">*</span><span class="hl-var">$d1</span><span class="hl-code">+</span><span class="hl-var">$d2</span><span class="hl-code">*</span><span class="hl-var">$d2</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-var">$t</span><span class="hl-code"> = -</span><span class="hl-var">$d2</span><span class="hl-code">/</span><span class="hl-var">$h</span><span class="hl-code">;
</span><span class="hl-var">$ac</span><span class="hl-code"> = </span><span class="hl-identifier">acos</span><span class="hl-brackets">(</span><span class="hl-var">$t</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-reserved">if</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-var">$y</span><span class="hl-code"> < </span><span class="hl-var">$poley</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-var">$ac</span><span class="hl-code"> += </span><span class="hl-identifier">M_PI</span><span class="hl-code">;
</span><span class="hl-var">$a</span><span class="hl-code"> = </span><span class="hl-var">$ac</span><span class="hl-code"> * </span><span class="hl-number">180</span><span class="hl-code">/</span><span class="hl-identifier">M_PI</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-var">$datax</span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-code"> = </span><span class="hl-var">$x</span><span class="hl-code">;
</span><span class="hl-var">$datay</span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-code"> = </span><span class="hl-var">$y</span><span class="hl-code">;
</span><span class="hl-var">$angle</span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-code"> = </span><span class="hl-var">$a</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-comment">//</span><span class="hl-comment"> Setup the graph</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-var">$graph</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Graph</span><span class="hl-brackets">(</span><span class="hl-number">300</span><span class="hl-code">,</span><span class="hl-number">200</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-var">$graph</span><span class="hl-code">-></span><span class="hl-identifier">SetScale</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">intlin</span><span class="hl-quotes">"</span><span class="hl-code">,</span><span class="hl-number">0</span><span class="hl-code">,</span><span class="hl-number">100</span><span class="hl-code">,</span><span class="hl-number">0</span><span class="hl-code">,</span><span class="hl-number">10</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-var">$graph</span><span class="hl-code">-></span><span class="hl-identifier">SetMarginColor</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">lightblue</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-comment">//</span><span class="hl-comment"> ..and titles</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-var">$graph</span><span class="hl-code">-></span><span class="hl-identifier">title</span><span class="hl-code">-></span><span class="hl-identifier">Set</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">Field plot</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-comment">//</span><span class="hl-comment"> Setup the field plot</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-var">$fp</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">FieldPlot</span><span class="hl-brackets">(</span><span class="hl-var">$datay</span><span class="hl-code">,</span><span class="hl-var">$datax</span><span class="hl-code">,</span><span class="hl-var">$angle</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-comment">//</span><span class="hl-comment"> Setup formatting callback</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-var">$fp</span><span class="hl-code">-></span><span class="hl-identifier">SetCallback</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">FldCallback</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-comment">//</span><span class="hl-comment"> First size argument is length (in pixels of arrow) </span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-comment">//</span><span class="hl-comment"> Second size argument is roughly size of arrow. Arrow size is specified as</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-comment">//</span><span class="hl-comment"> an integer in the range [0,9]</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-var">$fp</span><span class="hl-code">-></span><span class="hl-identifier">arrow</span><span class="hl-code">-></span><span class="hl-identifier">SetSize</span><span class="hl-brackets">(</span><span class="hl-number">20</span><span class="hl-code">,</span><span class="hl-number">2</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-var">$fp</span><span class="hl-code">-></span><span class="hl-identifier">arrow</span><span class="hl-code">-></span><span class="hl-identifier">SetColor</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">navy</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-var">$graph</span><span class="hl-code">-></span><span class="hl-identifier">Add</span><span class="hl-brackets">(</span><span class="hl-var">$fp</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-comment">//</span><span class="hl-comment"> .. and output</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-var">$graph</span><span class="hl-code">-></span><span class="hl-identifier">Stroke</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-inlinetags">?></span></pre></td></tr></table></div>