Created
December 13, 2010 11:34
-
-
Save tyage/738919 to your computer and use it in GitHub Desktop.
copy from this site. http://lab.polygonal.de/articles/recursive-dimensional-clustering/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Entity = function (type, pos, obj) { | |
this.type = type; | |
this.pos = pos; | |
this.obj = obj; | |
} | |
var RDC = function (collide) { | |
this.collide = collide; | |
} | |
RDC.SUBDIVISION_THRESHOLD = 4; | |
RDC.CONTACT_THRESHOLD = 0.001; | |
RDC.prototype.bruteForce = function (group) { | |
for (var i=0,l=group.length; i<l; i++) { | |
for (var j=i+1; j<k; j++) { | |
this.collide(group[i], group[j]); | |
} | |
} | |
}; | |
RDC.prototype.recursiveClustering = function (group, axis1, axis2) { | |
if (axis1 === -1 || group.length < RDC.SUBDIVISION_THRESHOLD) { | |
this.bruteForce(group); | |
} else { | |
var boundaries = this.getOpenCloseBounds(group, axis1); | |
boundaries.sort(function (a, b) { | |
return a.pos - b.pos; | |
}); | |
var newAxis1 = axis2, | |
newAxis2 = -1, | |
groupSubdivided = false, | |
subgroup = [], | |
count = 0, | |
l = boundaries.length; | |
for (var i=0; i<l; i++) | |
{ | |
var b = boundaries[i]; | |
if (b.type == "open") { | |
count++; | |
subgroup.push(b.obj); | |
} else { | |
count--; | |
if (count == 0) { | |
if (i != (k - 1)) { | |
groupSubdivided = true; | |
} | |
if (groupSubdivided) { | |
if (axis1 == 0) { | |
newAxis1 = 1; | |
} else if (axis1 == 1) { | |
newAxis1 = 0; | |
} | |
} | |
this.recursiveClustering(subgroup, newAxis1, newAxis2); | |
subgroup = []; | |
} | |
} | |
} | |
} | |
}; | |
RDC.prototype.getOpenCloseBounds = function (group, axis) { | |
var l = group.length, | |
boundaries = []; | |
switch(axis) | |
{ | |
case 0: | |
for (i=0; i<l; i++) { | |
o = group[i]; | |
boundaries.push(new Entity("open", o.x - o.radius + RDC.CONTACT_THRESHOLD, o)); | |
boundaries.push(new Entity("close", o.x + o.radius - RDC.CONTACT_THRESHOLD, o)); | |
} | |
break; | |
case 1: | |
for (i=0; i<l; i++) { | |
o = group[i]; | |
boundaries.push(new Entity("open", o.y - o.radius + RDC.CONTACT_THRESHOLD, o)); | |
boundaries.push(new Entity("close", o.y + o.radius - RDC.CONTACT_THRESHOLD, o)); | |
} | |
} | |
return boundaries; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment