templeos-info/public/Wb/Demo/Games/MassSpring.HC.HTML

201 lines
15 KiB
Plaintext
Raw Normal View History

2024-03-24 21:24:44 +00:00
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="generator" content="TempleOS V5.03">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="/style/templeos.css">
<script src="/script/templeos.js"></script>
<style type="text/css">
.cF0{color:#000000;background-color:#ffffff;}
.cF1{color:#0000aa;background-color:#ffffff;}
.cF2{color:#00aa00;background-color:#ffffff;}
.cF3{color:#00aaaa;background-color:#ffffff;}
.cF4{color:#aa0000;background-color:#ffffff;}
.cF5{color:#aa00aa;background-color:#ffffff;}
.cF6{color:#aa5500;background-color:#ffffff;}
.cF7{color:#aaaaaa;background-color:#ffffff;}
.cF8{color:#555555;background-color:#ffffff;}
.cF9{color:#5555ff;background-color:#ffffff;}
.cFA{color:#55ff55;background-color:#ffffff;}
.cFB{color:#55ffff;background-color:#ffffff;}
.cFC{color:#ff5555;background-color:#ffffff;}
.cFD{color:#ff55ff;background-color:#ffffff;}
.cFE{color:#ffff55;background-color:#ffffff;}
.cFF{color:#ffffff;background-color:#ffffff;}
</style>
</head>
<body>
<pre id="content">
<a name="l1"></a><span class=cF1>class</span><span class=cF0> MyMass:</span><span class=cF9>CMass</span><span class=cF0>
<a name="l2"></a>{
<a name="l3"></a> </span><span class=cF1>F64</span><span class=cF0> radius;
<a name="l4"></a>};
<a name="l5"></a>
<a name="l6"></a></span><span class=cF1>class</span><span class=cF0> MySpring:</span><span class=cF9>CSpring</span><span class=cF0>
<a name="l7"></a>{
<a name="l8"></a>};
<a name="l9"></a>
<a name="l10"></a></span><span class=cF9>CMathODE</span><span class=cF0> *ode=</span><span class=cF3>NULL</span><span class=cF0>;
<a name="l11"></a>
<a name="l12"></a></span><span class=cF1>U0</span><span class=cF0> </span><span class=cF5>DrawIt</span><span class=cF0>(</span><span class=cF9>CTask</span><span class=cF0> *,</span><span class=cF9>CDC</span><span class=cF0> *dc)
<a name="l13"></a>{
<a name="l14"></a> MyMass *tmpm;
<a name="l15"></a> MySpring *tmps;
<a name="l16"></a>
<a name="l17"></a> dc-&gt;color=</span><span class=cF3>RED</span><span class=cF0>;
<a name="l18"></a> tmps=ode-&gt;next_spring;
<a name="l19"></a> </span><span class=cF1>while</span><span class=cF0> (tmps!=&amp;ode-&gt;next_spring) </span><span class=cF7>{</span><span class=cF0>
<a name="l20"></a> </span><span class=cF5>GrLine</span><span class=cF0>(dc,tmps-&gt;end1-&gt;x,tmps-&gt;end1-&gt;y,tmps-&gt;end2-&gt;x,tmps-&gt;end2-&gt;y);
<a name="l21"></a> tmps=tmps-&gt;next;
<a name="l22"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l23"></a>
<a name="l24"></a> dc-&gt;color=</span><span class=cF3>BLACK</span><span class=cF0>;
<a name="l25"></a> tmpm=ode-&gt;next_mass;
<a name="l26"></a> </span><span class=cF1>while</span><span class=cF0> (tmpm!=&amp;ode-&gt;next_mass) </span><span class=cF7>{</span><span class=cF0>
<a name="l27"></a> </span><span class=cF5>GrCircle</span><span class=cF0>(dc,tmpm-&gt;x,tmpm-&gt;y,tmpm-&gt;radius);
<a name="l28"></a> tmpm=tmpm-&gt;next;
<a name="l29"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l30"></a>}
<a name="l31"></a>
<a name="l32"></a></span><span class=cF1>U0</span><span class=cF0> MyDerivative(</span><span class=cF9>CMathODE</span><span class=cF0> *ode,</span><span class=cF1>F64</span><span class=cF0>,</span><span class=cF9>COrder2D3</span><span class=cF0> *,</span><span class=cF9>COrder2D3</span><span class=cF0> *)
<a name="l33"></a>{</span><span class=cF2>//The forces due to springs and drag are</span><span class=cF0>
<a name="l34"></a></span><span class=cF2>//automatically handled by the ode code.</span><span class=cF0>
<a name="l35"></a> </span><span class=cF2>//We can add new forces here.</span><span class=cF0>
<a name="l36"></a> </span><span class=cF1>F64</span><span class=cF0> d,dd;
<a name="l37"></a> </span><span class=cF9>CD3</span><span class=cF0> p;
<a name="l38"></a> MyMass *tmpm1,*tmpm2;
<a name="l39"></a>
<a name="l40"></a> tmpm1=ode-&gt;next_mass;
<a name="l41"></a> </span><span class=cF1>while</span><span class=cF0> (tmpm1!=&amp;ode-&gt;next_mass) </span><span class=cF7>{</span><span class=cF0>
<a name="l42"></a> tmpm2=tmpm1-&gt;next;
<a name="l43"></a> </span><span class=cF1>while</span><span class=cF0> (tmpm2!=&amp;ode-&gt;next_mass) {
<a name="l44"></a> </span><span class=cF5>D3Sub</span><span class=cF0>(&amp;p,&amp;tmpm2-&gt;state-&gt;x,&amp;tmpm1-&gt;state-&gt;x);
<a name="l45"></a> dd=</span><span class=cF5>D3NormSqr</span><span class=cF0>(&amp;p);
<a name="l46"></a> </span><span class=cF1>if</span><span class=cF0> (dd&lt;=</span><span class=cF5>Sqr</span><span class=cF7>(</span><span class=cF0>tmpm1-&gt;radius+tmpm2-&gt;radius</span><span class=cF7>)</span><span class=cF0>) </span><span class=cF7>{</span><span class=cF0>
<a name="l47"></a> d=</span><span class=cF5>Sqrt</span><span class=cF0>(dd)+0.0001;
<a name="l48"></a> dd=10.0*</span><span class=cF5>Sqr</span><span class=cF0>(</span><span class=cF5>Sqr</span><span class=cF7>(</span><span class=cF5>Sqr</span><span class=cF0>(tmpm1-&gt;radius+tmpm2-&gt;radius)-dd</span><span class=cF7>)</span><span class=cF0>);
<a name="l49"></a> </span><span class=cF5>D3MulEqu</span><span class=cF0>(&amp;p,dd/d);
<a name="l50"></a> </span><span class=cF5>D3AddEqu</span><span class=cF0>(&amp;tmpm2-&gt;DstateDt-&gt;DxDt,&amp;p);
<a name="l51"></a> </span><span class=cF5>D3SubEqu</span><span class=cF0>(&amp;tmpm1-&gt;DstateDt-&gt;DxDt,&amp;p);
<a name="l52"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l53"></a> tmpm2=tmpm2-&gt;next;
<a name="l54"></a> }
<a name="l55"></a> tmpm1=tmpm1-&gt;next;
<a name="l56"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l57"></a>}
<a name="l58"></a>
<a name="l59"></a></span><span class=cF1>U0</span><span class=cF0> PlaceMass(</span><span class=cF9>I64</span><span class=cF0> x, </span><span class=cF9>I64</span><span class=cF0> y)
<a name="l60"></a>{
<a name="l61"></a> MyMass *tmpm=</span><span class=cF5>CAlloc</span><span class=cF0>(</span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF0>MyMass</span><span class=cF7>)</span><span class=cF0>);
<a name="l62"></a> tmpm-&gt;mass=1.0;
<a name="l63"></a> tmpm-&gt;drag_profile_factor=100.0;
<a name="l64"></a> tmpm-&gt;x=x;
<a name="l65"></a> tmpm-&gt;y=y;
<a name="l66"></a> tmpm-&gt;radius=10*(</span><span class=cF5>Rand</span><span class=cF0>+0.25);
<a name="l67"></a> </span><span class=cF5>QueIns</span><span class=cF0>(tmpm,ode-&gt;last_mass);
<a name="l68"></a>}
<a name="l69"></a>
<a name="l70"></a></span><span class=cF1>U0</span><span class=cF0> PlaceSpring(MyMass *tmpm1,MyMass *tmpm2)
<a name="l71"></a>{
<a name="l72"></a> MySpring *tmps=</span><span class=cF5>CAlloc</span><span class=cF0>(</span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF0>MySpring</span><span class=cF7>)</span><span class=cF0>);
<a name="l73"></a> tmps-&gt;end1=tmpm1;
<a name="l74"></a> tmps-&gt;end2=tmpm2;
<a name="l75"></a> tmps-&gt;const=10000;
<a name="l76"></a> tmps-&gt;rest_len=100;
<a name="l77"></a> </span><span class=cF5>QueIns</span><span class=cF0>(tmps,ode-&gt;last_spring);
<a name="l78"></a>}
<a name="l79"></a>
<a name="l80"></a></span><span class=cF1>U0</span><span class=cF0> Init()
<a name="l81"></a>{
<a name="l82"></a> ode=</span><span class=cF5>ODENew</span><span class=cF0>(0,1e-4,</span><span class=cF3>ODEF_HAS_MASSES</span><span class=cF0>);
<a name="l83"></a> ode-&gt;derive=&amp;MyDerivative;
<a name="l84"></a> ode-&gt;drag_v2=0.002;
<a name="l85"></a> ode-&gt;drag_v3=0.00001;
<a name="l86"></a> ode-&gt;acceleration_limit=5e3;
<a name="l87"></a>
<a name="l88"></a> </span><span class=cF5>QueIns</span><span class=cF0>(ode,</span><span class=cF5>Fs</span><span class=cF0>-&gt;last_ode);
<a name="l89"></a>}
<a name="l90"></a>
<a name="l91"></a></span><span class=cF1>U0</span><span class=cF0> CleanUp()
<a name="l92"></a>{
<a name="l93"></a> </span><span class=cF5>QueRem</span><span class=cF0>(ode);
<a name="l94"></a> </span><span class=cF5>QueDel</span><span class=cF0>(&amp;ode-&gt;next_mass,</span><span class=cF3>TRUE</span><span class=cF0>);
<a name="l95"></a> </span><span class=cF5>QueDel</span><span class=cF0>(&amp;ode-&gt;next_spring,</span><span class=cF3>TRUE</span><span class=cF0>);
<a name="l96"></a> </span><span class=cF5>ODEDel</span><span class=cF0>(ode);
<a name="l97"></a>}
<a name="l98"></a>
<a name="l99"></a></span><span class=cF1>U0</span><span class=cF0> MassSpringDemo()
<a name="l100"></a>{
<a name="l101"></a> </span><span class=cF9>I64</span><span class=cF0> msg_code,arg1,arg2;
<a name="l102"></a> MyMass *tmpm1=</span><span class=cF3>NULL</span><span class=cF0>,*tmpm2=</span><span class=cF3>NULL</span><span class=cF0>;
<a name="l103"></a>
<a name="l104"></a> </span><span class=cF5>PopUpOk</span><span class=cF0>(</span><span class=cF6>&quot;Left-Click to place mas\n&quot;</span><span class=cF0>
<a name="l105"></a> </span><span class=cF6>&quot;Right-Click and drag to\n&quot;</span><span class=cF0>
<a name="l106"></a> </span><span class=cF6>&quot;connect with spring.\n\n&quot;</span><span class=cF0>
<a name="l107"></a> </span><span class=cF6>&quot;Springs are 100 pixs long.\n&quot;</span><span class=cF0>);
<a name="l108"></a> </span><span class=cF5>SettingsPush</span><span class=cF0>; </span><span class=cF2>//See </span><a href="/Wb/Adam/TaskSettings.HC.HTML#l3"><span class=cF4>SettingsPush</span></a><span class=cF0>
<a name="l109"></a> </span><span class=cF5>AutoComplete</span><span class=cF0>;
<a name="l110"></a> </span><span class=cF5>WinBorder</span><span class=cF0>;
<a name="l111"></a> </span><span class=cF5>WinMax</span><span class=cF0>;
<a name="l112"></a> </span><span class=cF5>DocCursor</span><span class=cF0>;
<a name="l113"></a> </span><span class=cF5>DocClear</span><span class=cF0>;
<a name="l114"></a>
<a name="l115"></a> </span><span class=cF5>Fs</span><span class=cF0>-&gt;win_inhibit|=</span><span class=cF3>WIG_DBL_CLICK</span><span class=cF0>;
<a name="l116"></a>
<a name="l117"></a> </span><span class=cF5>MenuPush</span><span class=cF0>(
<a name="l118"></a> </span><span class=cF6>&quot;File {&quot;</span><span class=cF0>
<a name="l119"></a> </span><span class=cF6>&quot; Abort(,CH_SHIFT_ESC);&quot;</span><span class=cF0>
<a name="l120"></a> </span><span class=cF6>&quot; Exit(,CH_ESC);&quot;</span><span class=cF0>
<a name="l121"></a> </span><span class=cF6>&quot;}&quot;</span><span class=cF0>
<a name="l122"></a> </span><span class=cF6>&quot;Play {&quot;</span><span class=cF0>
<a name="l123"></a> </span><span class=cF6>&quot; Restart(,'\n');&quot;</span><span class=cF0>
<a name="l124"></a> </span><span class=cF6>&quot;}&quot;</span><span class=cF0>
<a name="l125"></a> );
<a name="l126"></a>
<a name="l127"></a> Init;
<a name="l128"></a> </span><span class=cF5>Fs</span><span class=cF0>-&gt;draw_it=&amp;</span><span class=cF5>DrawIt</span><span class=cF0>;
<a name="l129"></a>
<a name="l130"></a> </span><span class=cF1>try</span><span class=cF0> </span><span class=cF7>{</span><span class=cF0>
<a name="l131"></a> </span><span class=cF1>while</span><span class=cF0> (</span><span class=cF3>TRUE</span><span class=cF0>) {
<a name="l132"></a> msg_code=</span><span class=cF5>GetMsg</span><span class=cF0>(&amp;arg1,&amp;arg2,
<a name="l133"></a> 1&lt;&lt;</span><span class=cF3>MSG_MS_L_DOWN</span><span class=cF0>|1&lt;&lt;</span><span class=cF3>MSG_MS_R_DOWN</span><span class=cF0>|1&lt;&lt;</span><span class=cF3>MSG_MS_R_UP</span><span class=cF0>|1&lt;&lt;</span><span class=cF3>MSG_KEY_DOWN</span><span class=cF0>);
<a name="l134"></a> </span><span class=cF1>switch</span><span class=cF0> (msg_code) </span><span class=cF7>{</span><span class=cF0>
<a name="l135"></a> </span><span class=cF1>case</span><span class=cF0> </span><span class=cF3>MSG_MS_L_DOWN</span><span class=cF0>:
<a name="l136"></a> PlaceMass(arg1,arg2);
<a name="l137"></a> </span><span class=cF1>break</span><span class=cF0>;
<a name="l138"></a> </span><span class=cF1>case</span><span class=cF0> </span><span class=cF3>MSG_MS_R_DOWN</span><span class=cF0>:
<a name="l139"></a> tmpm1=</span><span class=cF5>MassFind</span><span class=cF0>(ode,arg1,arg2);
<a name="l140"></a> tmpm2=</span><span class=cF3>NULL</span><span class=cF0>;
<a name="l141"></a> </span><span class=cF1>break</span><span class=cF0>;
<a name="l142"></a> </span><span class=cF1>case</span><span class=cF0> </span><span class=cF3>MSG_MS_R_UP</span><span class=cF0>:
<a name="l143"></a> </span><span class=cF1>if</span><span class=cF0> (tmpm1 &amp;&amp; </span><span class=cF7>(</span><span class=cF0>tmpm2=</span><span class=cF5>MassFind</span><span class=cF0>(ode,arg1,arg2)</span><span class=cF7>)</span><span class=cF0> &amp;&amp; tmpm1!=tmpm2)
<a name="l144"></a> PlaceSpring(tmpm1,tmpm2);
<a name="l145"></a> tmpm1=tmpm2=</span><span class=cF3>NULL</span><span class=cF0>;
<a name="l146"></a> </span><span class=cF1>break</span><span class=cF0>;
<a name="l147"></a> </span><span class=cF1>case</span><span class=cF0> </span><span class=cF3>MSG_KEY_DOWN</span><span class=cF0>:
<a name="l148"></a> </span><span class=cF1>switch</span><span class=cF0> (arg1) {
<a name="l149"></a> </span><span class=cF1>case</span><span class=cF0> </span><span class=cF6>'\n'</span><span class=cF0>:
<a name="l150"></a> CleanUp;
<a name="l151"></a> Init;
<a name="l152"></a> </span><span class=cF1>break</span><span class=cF0>;
<a name="l153"></a> </span><span class=cF1>case</span><span class=cF0> </span><span class=cF3>CH_SHIFT_ESC</span><span class=cF0>:
<a name="l154"></a> </span><span class=cF1>case</span><span class=cF0> </span><span class=cF3>CH_ESC</span><span class=cF0>:
<a name="l155"></a> </span><span class=cF1>goto</span><span class=cF0> ms_done;
<a name="l156"></a> }
<a name="l157"></a> </span><span class=cF1>break</span><span class=cF0>;
<a name="l158"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l159"></a> }
<a name="l160"></a>ms_done: </span><span class=cF2>//Don't goto out of try</span><span class=cF0>
<a name="l161"></a> </span><span class=cF5>GetMsg</span><span class=cF0>(,,1&lt;&lt;</span><span class=cF3>MSG_KEY_UP</span><span class=cF0>);
<a name="l162"></a> </span><span class=cF7>}</span><span class=cF0> </span><span class=cF1>catch</span><span class=cF0>
<a name="l163"></a> </span><span class=cF5>PutExcept</span><span class=cF0>;
<a name="l164"></a> </span><span class=cF5>SettingsPop</span><span class=cF0>;
<a name="l165"></a> CleanUp;
<a name="l166"></a> </span><span class=cF5>MenuPop</span><span class=cF0>;
<a name="l167"></a>}
<a name="l168"></a>
<a name="l169"></a>MassSpringDemo;
</span></pre></body>
</html>