Skip to content

Commit

Permalink
Code clean up 6
Browse files Browse the repository at this point in the history
  • Loading branch information
beizhang authored and philogb committed Feb 7, 2012
1 parent 9c0f4aa commit a1b93b9
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 41 deletions.
13 changes: 11 additions & 2 deletions Source/Geometry/Geometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,19 @@ $jit.geometry = {
* @param {Complex} p1
* @param {Complex} p2
*/
dist : function(p1, p2) {
dist2 : function(p1, p2) {
var dx = p1.x - p2.x,
dy = p1.y - p2.y;
return Math.sqrt(dx * dx + dy * dy);
return dx * dx + dy * dy;
},

/**
* The distance between two points
* @param {Complex} p1
* @param {Complex} p2
*/
dist : function(p1, p2) {
return Math.sqrt(Geometry.dist2(p1,p2));
},

/**
Expand Down
70 changes: 34 additions & 36 deletions Source/Graph/Graph.Plot.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,75 +174,73 @@ Graph.Plot = {
},

'polygon': function(elem, prop, delta, getter, setter) {
if (elem.selected || elem.id == 'root') {
if (elem.selected || elem.id == 'root') {
var from = elem[getter](prop, 'start'),
to = elem[getter](prop, 'end'),
cur = [];
var dist = function (c1, c2) {
var dx = c1.x - c2.x, dy = c1.y - c2.y;
return dx * dx + dy * dy;
};
if(typeof from.offset == 'undefined') {
if(from === 0) {
from = $.map(to,function(){return new $jit.Complex(0, 0);});
if (typeof from.offset == 'undefined') {
if (from === 0) {
from = $.map(to, function() {
return new $jit.Complex(0, 0);
});
from.offset = 0;
}
else {
if(from.length == 0) {
from.push(new $jit.Complex(0, 0));
if (from.length == 0) {
from.push(new $jit.Complex(0, 0));
}
while(from.length < to.length) {
while (from.length < to.length) {
from.push(from[0]);
}
while(from.length > to.length) {
while (from.length > to.length) {
to.push(to[0] || new $jit.Complex(0, 0));
}
if (from.length == 0) return;
var l = from.length;
var minDist = 1e300;
for(var offset = 0; offset < l; offset ++) {
for (var offset = 0; offset < l; offset ++) {
var d = 0;
for(var i = 0; i < l; i++){
d += dist(from[(offset + i) % l], to[i]);
for (var i = 0; i < l; i++) {
d +=Geometry.dist2(from[(offset + i) % l], to[i]);
}
if (d < minDist)
{
if (d < minDist) {
from.offset = offset;
minDist = d;
}
}
}
}
for(var i=0, l=from.length; i<l; i++) {

for (var i = 0, l = from.length; i < l; i++) {
var fromi = from[(i + from.offset) % l], toi = to[i];
cur.push(new $jit.Complex(
this.compute(fromi.x, toi.x, delta),
this.compute(fromi.y, toi.y, delta)
this.compute(fromi.x, toi.x, delta),
this.compute(fromi.y, toi.y, delta)
));
}
elem[setter](prop, cur);
}
var sub = elem.getSubnodes([1, 1]);
var comp = this.compute;
var subSites = sub.map(function(node, i){
var fromi = node.getPos('start'), toi = node.getPos('end');
var site = new $jit.Complex(
comp(fromi.x, toi.x, delta),
comp(fromi.y, toi.y, delta)
}
var sub = elem.getSubnodes([1, 1]);
var comp = this.compute;
var subSites = sub.map(function(node, i) {
var fromi = node.getPos('start'), toi = node.getPos('end');
var site = $C(
comp(fromi.x, toi.x, delta),
comp(fromi.y, toi.y, delta)
);
if (node.data.$area)
site.area = node.data.$area;
return site;
});
var boundary = elem[getter](prop);
var offset = elem.offset;
if (offset) {
});
var boundary = elem[getter](prop);
var offset = elem.offset;
if (offset) {
boundary = $jit.geometry.offsetConvex(boundary, - offset);
}
var polygons = $jit.geometry.voronoi(subSites, boundary);
polygons.forEach(function(poly, i) {
sub[i][setter](prop, poly); });
var polygons = $jit.geometry.voronoi(subSites, boundary);
polygons.forEach(function(poly, i) {
sub[i][setter](prop, poly);
});
}
},

Expand Down
2 changes: 1 addition & 1 deletion Source/Visualizations/Voronoi.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ Layouts.TM.Voronoi = new Class({

centroid : function(sites, bound) {
var tdist = 2, polygons;
while (tdist > 1e-3) {
while (tdist > 1) {
polygons = Geometry.voronoi(sites, bound);
tdist = 0;
sites = polygons.map(function(p, j) {
Expand Down
9 changes: 7 additions & 2 deletions Tests/Voronoi/test1.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ var log = function(text) {
element.innerHTML = text;
can.getElement().appendChild(element);
}
if (!window.console) {
console = {
log : log
};
}
var V = $jit.util;
var color = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'];
var ps;
Expand Down Expand Up @@ -194,10 +199,10 @@ function step(){
function force() {
if(window.fc) {
window.clearInterval(window.fc), window.fc = null;
$jit.id('force-button').innerText = "Animate!";
$jit.id('force-button').innerHTML = "Animate!";
} else {
window.fc = window.setInterval(step, 10);
$jit.id('force-button').innerText = "Stop!";
$jit.id('force-button').innerHTML = "Stop!";
}
}

Expand Down
1 change: 1 addition & 0 deletions Tests/Voronoi/test2.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a1b93b9

Please sign in to comment.