05_LadPohyb/ufo.js
1 // FUNKCIE
2 function Pozicia ()
3 { // Vypise poziciu mysi do stavoveho riadku
4 window.status =
5 "Myš:" +
6 " x = "+ event.clientX +
7 " y = "+ event.clientY;
8 }
9
10 function Info (am, Obj)
11 { var Text = "~ " + DajT(am) + " " + DajXY(Obj) + " ~";
12 return Text;
13 }
14
15 function DajT (am)
16 { // Vrati text s aktualnym casom objektu animujuceho prvku
17 sText ="t=" + DesatinneCislo (am.currTimeState.activeTime, 2);
18 return sText;
19 }
20
21 function DajXY (Obj)
22 { // Vrati text s aktualnou polohou zadaneho objektu
23 sText = "x=" + parseInt(Obj.currentStyle.left);
24 sText+=" y=" + parseInt(Obj.currentStyle.top);
25 return sText;
26 }
27
28 function DesatinneCislo (x, pdm)
29 { // Zo zadaneho cilsa x vytvori desatinne cislo
30 // so zadanym poctom desatinnych miest pdm
31 var sd = new String(".0000000000000");
32 var sx = new String("")+x;
33 if (sx.indexOf(".")>=0)sx+=sd.substring(1, pdm+1);
34 else sx+=sd.substring(0, pdm+1);
35 var nc = sx.indexOf(".")+pdm+1; // pocet ciselnych pozicii
36 return sx.substring(0, nc);
37 }
38
39 // TRIEDY
40 function CBod (x,y,t,dl)
41 {
42 this.x=x;
43 this.y=y;
44 this.t=t;
45 this.dl=dl;
46
47 this.Zobraz = Zobraz;
48 function Zobraz (Pred)
49 {
50 // var B=document.createElement ("div");
51 var B=Pred.ownerDocument.createElement ("div");
52
53 Text="position:absolute; width:3; border-width:3px; border-top-style:solid;"
54 Text+=" left:"+this.x+"px;";
55 Text+=" top:"+this.y+"px;";
56 B.style.cssText = Text;
57 B.title = "t="+DesatinneCislo (this.t, 3);
58 // document.body.insertBefore(B,null);
59 Pred.parentElement.insertBefore(B,Pred);
60 }
61 }
62
63 function CDraha ()
64 {
65 this.Body = new Array();
66 this.x = 0;
67 this.y = 0;
68 this.t = 0;
69 this.l = 0;
70 this.bStop = false;
71 this.Nadpis = '';
72 this.Win=null;
73 this.Interval=0;
74 this.nn=0;
75
76 this.PridajBod = PridajBod;
77 function PridajBod (am, Obj, bPosledny)
78 {
79 if (this.bStop == true) return;
80 // Zistenie zmeny súradníc a casu
81 var x=parseInt(Obj.currentStyle.left);
82 var y=parseInt(Obj.currentStyle.top);
83 var t=am.currTimeState.activeTime;
84 var a = Math.abs(this.x-x);
85 var b = Math.abs(this.y-y);
86 var c = Math.abs(this.t-t);
87 var dl= 0; // prirastok drahy
88 if( a>4 || b>4 || c>1 || this.Body.length == 0 || bPosledny )
89 { // Je dostatocna zmena
90 if (this.Body.length>0)
91 { // Vypocitat prirastok drahy
92 dl=Math.sqrt (a*a +b*b);
93 this.l+=dl;
94 }
95 // Pridat nový bod a zaznamenat x,y,t
96 this.Body.push(new CBod(x,y,t,dl));
97 this.x=x;
98 this.y=y;
99 this.t=t;
100 this.bStop = bPosledny;
101 }
102 }
103
104 this.Zobraz = Zobraz;
105 function Zobraz (Pred)
106 {
107 for (i=0; i<this.Body.length; i++) this.Body[i].Zobraz(Pred);
108 }
109
110 this.Tabulka = Tabulka;
111 function Tabulka(Text)
112 {
113 this.Win = window.open("Draha.htm",null,
114 "height=470,width=280,status=yes,toolbar=no,menubar=no, location=no,scrollbars=yes,resizable=yes");
115 this.Nadpis = Text;
116 this.Interval=setInterval(FNaplnTab,500);
117 }
118
119 this.NaplnTabulku = NaplnTabulku;
120 function NaplnTabulku()
121 {
122 this.nn++;
123 var Doc = this.Win.document;
124 if ( Doc.readyState!='complete') return;
125 clearInterval(this.Interval);
126 // Pocitadlo
127 var xx = Doc.getElementById("xx");
128 Text="nn="+this.nn;
129 xx.insertBefore(Doc.createTextNode(Text),null);
130 // Co je vykreslene
131 var Co = Doc.getElementById("Co");
132 var Text=this.Nadpis+": l="+this.l;
133 Co.insertBefore(Doc.createTextNode(Text),null);
134 // Body casoveho diagramu
135 var BCD = Doc.getElementById("BCD");
136 var Tab = Doc.getElementById("Tab");
137 // Telo tabulky
138 var tBody = Doc.createElement("tbody");
139 // Premenne pre vytvorenie riadku
140 var tr, tdi, tdt, tdx, tdy, tdl, text;
141 // Premenne pre vytvorenie casoveho diagramu
142 var l=0,t=0;
143 // Do tabulky dam udaje vsetkych bodov drahy
144 for (i=0; i<this.Body.length; i++)
145 {
146 tr = Doc.createElement("tr");
147
148 tdi = Doc.createElement("td");
149 text = Doc.createTextNode(i);
150 tdi.insertBefore(text, null);
151 tr.insertBefore(tdi, null);
152
153 tdt = Doc.createElement("td");
154 text = Doc.createTextNode(DesatinneCislo(this.Body[i].t, 2));
155 tdt.insertBefore(text, null);
156 tr.insertBefore(tdt, null);
157
158 tdx = Doc.createElement("td");
159 text = Doc.createTextNode(this.Body[i].x);
160 tdx.insertBefore(text, null);
161 tr.insertBefore(tdx, null);
162
163 tdy = Doc.createElement("td");
164 text = Doc.createTextNode(this.Body[i].y);
165 tdy.insertBefore(text, null);
166 tr.insertBefore(tdy, null);
167
168 tdl = Doc.createElement("td");
169 text = Doc.createTextNode(DesatinneCislo(this.Body[i].dl, 2));
170 tdl.insertBefore(text, null);
171 tr.insertBefore(tdl, null);
172
173 // Vlozenie riadku do tBody
174 tBody.insertBefore(tr, null);
175
176 if (i==0)
177 {
178 var B = new CBod(10,300,0,0);
179 B.Zobraz(BCD);
180 }
181 else
182 {
183 l+=this.Body[i].dl;
184 if (this.l>0 && this.t>0) // budu v menovateli
185 {
186 var c =(this.Body[i].t - t)*200/this.t;
187 if (c>2)
188 {
189 var x = 10 + this.Body[i].t *200/this.t;
190 var y = 300 - l*200/this.l;
191 var B = new CBod(x,y,this.Body[i].t,0);
192 B.Zobraz(BCD);
193 t=this.Body[i].t;
194 }
195 }
196 }
197 }
198 // Vlozenie tBody do tabulky
199 Tab.insertBefore(tBody, null);
200 }
201 }
202 // OBJEKTY
203 var Draha = new CDraha();
204 var bStop = false;
205 function FNaplnTab () {Draha.NaplnTabulku();}