fieldscatterex1.html 19.8 KB
<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">&lt;?php</span><span class="hl-code"> </span><span class="hl-comment">//</span><span class="hl-comment"> content=&quot;text/plain; charset=utf-8&quot;</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">&quot;</span><span class="hl-quotes">&quot;</span><span class="hl-code">;
    </span><span class="hl-var">$arrowsize</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-quotes">&quot;</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"> &gt; </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 &quot;x=$x, y=$y, blue=$blue, red=$red&lt;br&gt;&quot;;</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"> &lt; </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">&lt;</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"> &amp;&amp; </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"> &gt; </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">  &gt; </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"> &lt; </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"> &lt; </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">-&gt;</span><span class="hl-identifier">SetScale</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">intlin</span><span class="hl-quotes">&quot;</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">-&gt;</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">-&gt;</span><span class="hl-identifier">title</span><span class="hl-code">-&gt;</span><span class="hl-identifier">Set</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">Field plot</span><span class="hl-quotes">&quot;</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">-&gt;</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">-&gt;</span><span class="hl-identifier">arrow</span><span class="hl-code">-&gt;</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">-&gt;</span><span class="hl-identifier">arrow</span><span class="hl-code">-&gt;</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">-&gt;</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">-&gt;</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">?&gt;</span></pre></td></tr></table></div>