Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <head>
- <script>
- var Template = {};
- var requestAnimationFrame =
- window.requestAnimationFrame||
- window.webkitRequestAnimationFrame||
- window.mozRequestAnimationFrame||
- window.oRequestAnimationFrame||
- window.msRequestAnimationFrame;
- var cancelAnimationFrame =
- window.cancelAnimationFrame||
- window.webkitCancelRequestAnimationFrame||
- window.webkitCancelAnimationFrame||
- window.mozCancelRequestAnimationFrame||
- window.mozCancelAnimationFrame||
- window.oCancelRequestAnimationFrame||
- window.oCancelAnimationFrame||
- window.msCancelRequestAnimationFrame||
- window.msCancelAnimationFrame;
- Template.newTime = Date.now();
- Template.time = 0;
- Template.animation = function()
- {
- if(!Template.stopped)
- {
- Template.oldTime = Template.newTime;
- if(!Template.paused)
- {
- Template.calculate();
- }
- Template.draw();
- Template.newTime = Date.now();
- Template.time=Template.newTime-Template.oldTime;
- Template.animationFrame = requestAnimationFrame(Template.animation);
- }
- }
- Template.start = function(d,c)
- {
- Template.draw = d;
- Template.calculate = c;
- Template.paused = false;
- Template.animationFrame = requestAnimationFrame(Template.animation);
- }
- Template.stop = function()
- {
- cancelAnimationFrame(Template.animationFrame);
- }
- function Transition(start,end,period,loops,func)
- {
- this.start = start;
- this.end = end;
- this.period = period;
- if(!func)this.func = function(a){return a;};
- else this.func = func;
- this.pos = 0;
- this.reversed;
- this.loop = loops;
- this.res = 0;
- this.calc=function()
- {
- if(this.reversed) this.pos -= Template.time/this.period;
- else this.pos += Template.time/this.period;
- if(this.loop===0)
- {
- this.pos = (this.pos+1)%1;
- }
- else
- {
- this.pos = Math.min(this.loop,Math.max(this.pos,0));
- }
- this.res = this.func(this.pos)*(end-start)+start;
- }
- this.get = function()
- {
- return this.res;
- }
- this.reverse = function()
- {
- this.reversed = !this.reversed;
- }
- }
- </script>
- <script>
- function Point(x,y)
- {
- this.x = x;
- this.y = y;
- this.draw = function(G)
- {
- G.beginPath();
- G.moveTo(this.x+10,this.y);
- G.arc(this.x,this.y,10,0,2*Math.PI);
- G.stroke();
- }
- }
- function Line(xs,ys,xe,ye)
- {
- this.start = new Point(xs,ys);
- this.end = new Point(xe,ye);
- this.Intersects = function(line,x,y,x1,y1)
- {
- x = +x|0;
- y = +y|0;
- x1 = +x1|0;
- y1 = +y1|0;
- return get_line_intersection(this.start.x+x,this.start.y+y,
- this.end.x+x,this.end.y+y,
- line.start.x+x1,line.start.y+y1,
- line.end.x+x1,line.end.y+y1);
- }
- }
- function Mesh(x,y,strokeStyle,fillStyle)
- {
- this.prevPoint = null;
- this.lines = [];
- this.x = x;
- this.y = y;
- this.strokeStyle = strokeStyle;
- this.fillStyle = fillStyle;
- this.add = function(point,index)
- {
- if(this.prevPoint)
- {
- index = +index|this.lines.length;
- this.lines.splice(index,0,new Line(this.prevPoint.x,this.prevPoint.y,point.x,point.y));
- }
- this.prevPoint = new Point(point.x,point.y);
- }
- this.collides = function(thing)
- {
- var collisions = [];
- if(thing.constructor == Mesh)
- {
- var mesh = thing;
- for(var a=0,aa=this.lines.length; a<aa; a++)
- {
- for(var b=0,bb=mesh.lines.length; b<bb; b++)
- {
- var collision = this.lines[a].Intersects(mesh.lines[b],this.x,this.y,mesh.x,mesh.y);
- if(collision)collisions[collisions.length] = collision;
- }
- }
- }
- else if(thing.constructor == Line)
- {
- var line = thing;
- for(var a=0,aa=this.lines.length; a<aa; a++)
- {
- var collision = this.lines[a].Intersects(line,this.x,this.y);
- if(collision)collisions[collisions.length] = collision;
- }
- }
- return collisions;
- }
- this.FinishMesh = function()
- {
- if(this.lines.length==0)return false;
- this.add(new Point(this.lines[0].start.x,this.lines[0].start.y));
- this.finished = true;
- return true;
- }
- this.draw = function(G)
- {
- G.strokeStyle = this.strokeStyle;
- G.fillStyle = this.fillStyle;
- if(this.lines.length==0)return false;
- G.beginPath();
- G.moveTo(this.lines[0].start.x+this.x,this.lines[0].start.y+this.y);
- for(var a=0; a<this.lines.length; a++)
- {
- G.lineTo(this.lines[a].end.x+this.x,this.lines[a].end.y+this.y);
- }
- if(this.finished)G.fill();
- G.stroke();
- return true;
- }
- }
- function get_line_intersection(p0_x,p0_y,p1_x,p1_y,p2_x,p2_y,p3_x,p3_y)
- {
- var s1_x, s1_y, s2_x, s2_y;
- s1_x = p1_x - p0_x; s1_y = p1_y - p0_y;
- s2_x = p3_x - p2_x; s2_y = p3_y - p2_y;
- var cache2=(-s2_x * s1_y + s1_x * s2_y);
- if(cache2===0) return false; // Parallel
- var s, t;
- var cache0=(p0_y - p2_y);
- var cache1=(p0_x - p2_x);
- s = (-s1_y * cache1 + s1_x * cache0) / cache2;
- t = ( s2_x * cache0 - s2_y * cache1) / cache2;
- if (s >= 0 && s <= 1 && t >= 0 && t <= 1)
- {
- // Collision detected
- return new Point(p0_x + (t * s1_x),p0_y + (t * s1_y));
- }
- return false; // No collision
- }
- </script>
- <script>
- /******************************************************************************
- * Created 2008-08-19.
- *
- * Dijkstra path-finding functions. Adapted from the Dijkstar Python project.
- *
- * Copyright (C) 2008
- * Wyatt Baldwin <self@wyattbaldwin.com>
- * All rights reserved
- *
- * Licensed under the MIT license.
- *
- * http://www.opensource.org/licenses/mit-license.php
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- *****************************************************************************/
- var dijkstra = {
- single_source_shortest_paths: function(graph, s, d) {
- // Predecessor map for each node that has been encountered.
- // node ID => predecessor node ID
- var predecessors = {};
- // Costs of shortest paths from s to all nodes encountered.
- // node ID => cost
- var costs = {};
- costs[s] = 0;
- // Costs of shortest paths from s to all nodes encountered; differs from
- // `costs` in that it provides easy access to the node that currently has
- // the known shortest path from s.
- // XXX: Do we actually need both `costs` and `open`?
- var open = dijkstra.PriorityQueue.make();
- open.push(s, 0);
- var closest,
- u, v,
- cost_of_s_to_u,
- adjacent_nodes,
- cost_of_e,
- cost_of_s_to_u_plus_cost_of_e,
- cost_of_s_to_v,
- first_visit;
- while (!open.empty()) {
- // In the nodes remaining in graph that have a known cost from s,
- // find the node, u, that currently has the shortest path from s.
- closest = open.pop();
- u = closest.value;
- cost_of_s_to_u = closest.cost;
- // Get nodes adjacent to u...
- adjacent_nodes = graph[u] || {};
- // ...and explore the edges that connect u to those nodes, updating
- // the cost of the shortest paths to any or all of those nodes as
- // necessary. v is the node across the current edge from u.
- for (v in adjacent_nodes) {
- // Get the cost of the edge running from u to v.
- cost_of_e = adjacent_nodes[v];
- // Cost of s to u plus the cost of u to v across e--this is *a*
- // cost from s to v that may or may not be less than the current
- // known cost to v.
- cost_of_s_to_u_plus_cost_of_e = cost_of_s_to_u + cost_of_e;
- // If we haven't visited v yet OR if the current known cost from s to
- // v is greater than the new cost we just found (cost of s to u plus
- // cost of u to v across e), update v's cost in the cost list and
- // update v's predecessor in the predecessor list (it's now u).
- cost_of_s_to_v = costs[v];
- first_visit = (typeof costs[v] === 'undefined');
- if (first_visit || cost_of_s_to_v > cost_of_s_to_u_plus_cost_of_e) {
- costs[v] = cost_of_s_to_u_plus_cost_of_e;
- open.push(v, cost_of_s_to_u_plus_cost_of_e);
- predecessors[v] = u;
- }
- }
- }
- if (typeof d !== 'undefined' && typeof costs[d] === 'undefined') {
- var msg = ['Could not find a path from ', s, ' to ', d, '.'].join('');
- throw new Error(msg);
- }
- return predecessors;
- },
- extract_shortest_path_from_predecessor_list: function(predecessors, d) {
- var nodes = [];
- var u = d;
- var predecessor;
- while (u) {
- nodes.push(u);
- predecessor = predecessors[u];
- u = predecessors[u];
- }
- nodes.reverse();
- return nodes;
- },
- find_path: function(graph, s, d) {
- var predecessors = dijkstra.single_source_shortest_paths(graph, s, d);
- return dijkstra.extract_shortest_path_from_predecessor_list(
- predecessors, d);
- },
- /**
- * A very naive priority queue implementation.
- */
- PriorityQueue: {
- make: function (opts) {
- var T = dijkstra.PriorityQueue,
- t = {},
- opts = opts || {},
- key;
- for (key in T) {
- t[key] = T[key];
- }
- t.queue = [];
- t.sorter = opts.sorter || T.default_sorter;
- return t;
- },
- default_sorter: function (a, b) {
- return a.cost - b.cost;
- },
- /**
- * Add a new item to the queue and ensure the highest priority element
- * is at the front of the queue.
- */
- push: function (value, cost) {
- var item = {value: value, cost: cost};
- this.queue.push(item);
- this.queue.sort(this.sorter);
- },
- /**
- * Return the highest priority element in the queue.
- */
- pop: function () {
- return this.queue.shift();
- },
- empty: function () {
- return this.queue.length === 0;
- }
- }
- };
- </script>
- <script>
- var
- find_path = dijkstra.find_path,
- graph,
- path,
- paths;
- var Map = {}
- var width = 50;
- var height = 50;
- var Map = [];
- for(var a=0; a<width; a++)
- {
- Map[a] = [];
- for(var b=0; b<height; b++)
- {
- var h = Map[a][b-1]|parseInt(Math.random()*256);
- if(a>0)h = h/2+Map[a-1][b]/2;
- Map[a][b] = h+(Math.random()*20)-10;
- }
- }
- var graph = {};
- for(var a=0; a<width; a++)
- {
- for(var b=0; b<height; b++)
- {
- var index = a+","+b;
- graph[index] = {};
- var dx = [];
- var dy = [];
- if(a>0) {dx[dx.length] = a-1;dy[dy.length] = b; }
- if(a<width-1) {dx[dx.length] = a+1;dy[dy.length] = b; }
- if(b>0) {dx[dx.length] = a; dy[dy.length] = b-1;}
- if(b<height-1){dx[dx.length] = a; dy[dy.length] = b+1;}
- for(var z=0; z<dx.length; z++)
- {
- var di = dx[z]+","+dy[z];
- graph[index][di] = Map[dx[z]][dy[z]]-Map[a][b]+1;
- }
- }
- }
- </script>
- </head>
- <body>
- <canvas id="cvs" width="800" height="600" onmousemove="mouseMove(event)"></canvas>
- <script>
- var scale = 5,mouse={x:-1,y:-10};
- function init()
- {
- Template.start(draw,calc);
- }
- function mouseMove(e)
- {
- mouse = {x:e.offsetX-0.5,y:e.offsetY-0.5};
- }
- function draw()
- {
- var G = cvs.getContext("2d");
- G.clearRect(0,0,cvs.width,cvs.height);
- for(var a=0; a<width; a++)
- {
- for(var b=0; b<height; b++)
- {
- G.fillStyle = "hsl("+Map[a][b]/5+",50%,50%)";
- G.fillRect(a*scale,b*scale,10,10);
- }
- }
- G.beginPath();
- for(var a=0; a<path.length; a++)
- {
- var coord = path[a].split(",");
- G.lineTo(coord[0]*scale+scale/2,coord[1]*scale+scale/2);
- }
- G.stroke();
- }
- function calc()
- {
- if(Map[parseInt(mouse.x/scale).toString()])
- Map[parseInt(mouse.x/scale).toString()][parseInt(mouse.y/scale).toString()]+=15;
- var graph = {};
- for(var a=0; a<width; a++)
- {
- for(var b=0; b<height; b++)
- {
- var index = a+","+b;
- graph[index] = {};
- var dx = [];
- var dy = [];
- if(a>0) {dx[dx.length] = a-1;dy[dy.length] = b; }
- if(a<width-1) {dx[dx.length] = a+1;dy[dy.length] = b; }
- if(b>0) {dx[dx.length] = a; dy[dy.length] = b-1;}
- if(b<height-1){dx[dx.length] = a; dy[dy.length] = b+1;}
- for(var z=0; z<dx.length; z++)
- {
- var di = dx[z]+","+dy[z];
- graph[index][di] = Map[dx[z]][dy[z]]-Map[a][b]+1;
- }
- }
- }
- path = find_path(graph, '0,0', '49,49');
- }
- init();
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement