forked from tyt2y3/F.core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.js
More file actions
146 lines (137 loc) · 3.18 KB
/
Copy pathgraph.js
File metadata and controls
146 lines (137 loc) · 3.18 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
define(['F.core/util'],function(F)
{
/*\
* graph
* to maintain a graph
[ class ]
* a graph is a mapping/hashing of objects in finite 2d world into a 2d array
* for example, can be used in collision detection system to boost performance.
* speaking performance, however, there is considerable overhead in maintaining a graph,
* and factors like cell size and number of objects in scene impact performance the most and
* must be chosen carefully. and today's javascript is really too fast to care about algorithm
* while most probably the bottleneck is from rendering.
* you never know until I experiment on this, see [example](../sample/graph.html)
- config (object)
| var gh_config=
| {
| width: w, //width
| height: h,// and height of the 2d world
| gridx: gx,//make a
| gridy: gy,// gx*gy sized 2d array
| //specify only (gridx,gridy) OR (cellx,celly)
| cellx: cx,//the size of
| celly: cy // each cell is cx*cy
| }
\*/
function graph (config)
{
if( !config.cellx)
{
config.cellx=config.width/config.gridx;
config.celly=config.height/config.gridy;
}
if( !config.gridx)
{
config.gridx=config.width/config.cellx;
config.gridy=config.height/config.celly;
}
this.config=config;
this.create_graph(config.gridx, config.gridy);
}
/*\
* graph.create_graph
* create a 3D array
[ method ]
\*/
graph.prototype.create_graph=function(w,h)
{
var A = new Array(w);
for ( var i=0; i<w; i++)
{
A[i] = new Array(h);
for ( var j=0; j<h; j++)
A[i][j] = new Array();
}
this.G = A; //grid storing indices of objects
}
/*\
* graph.at
* convert P from world space to grid space
[ method ]
- P (object) `{x,y}` point in world space
= (object) `{x,y}` index in grid space
\*/
graph.prototype.at=function(P)
{
var A= {
x: Math.floor(P.x/this.config.cellx)%this.config.gridx,
y: Math.floor(P.y/this.config.celly)%this.config.gridy
}
//include the boundaries
if( A.x==this.config.gridx)
A.x=this.config.gridx-1;
if( A.y==this.config.gridy)
A.y=this.config.gridy-1;
if( A.x < 0)
A.x+=this.config.gridx;
if( A.y < 0)
A.y+=this.config.gridy;
return A;
}
/*\
* graph.get
* return array of object('s index) present at a particular point
[ method ]
- P (object) `{x,y}` index in grid space
= (array) array of objects in this cell
\*/
graph.prototype.get=function(P)
{
return this.G[P.x][P.y];
}
/*\
* graph.add
* add an object `i` at point `P`
[ method ]
- i (object)
- P (object) `{x,y}` point in world space
\*/
graph.prototype.add=function(i,P)
{
this.get(this.at(P)).push(i);
}
/*\
* graph.remove
* remove object i at P
[ method ]
- i (object)
- P (object) `{x,y}` point in world space
\*/
graph.prototype.remove=function(i,P)
{
var g=this.get(this.at(P));
var res = F.arr_search( g, function(e){return e==i} ); //search for i
if (res) g.splice(res,1); //remove
}
/*\
* graph.move
* an object i moves from A to B
[ method ]
- i (object)
- A,B (object) `{x,y}` point in world space
\*/
graph.prototype.move=function(i,A,B)
{
var gA=this.get(this.at(A));
var gB=this.get(this.at(B));
if(!gB)
var x=1;
if( gA !== gB)
{
var res = F.arr_search( gA, function(e){return e===i} ); //search for i
if (res) gA.splice(res,1); //remove
gB.push(i);
}
}
return graph;
});