Skip to content
This repository has been archived by the owner on Mar 13, 2018. It is now read-only.

Commit

Permalink
bug fixes in randomer
Browse files Browse the repository at this point in the history
  • Loading branch information
Endmaril committed May 25, 2011
1 parent 51acf30 commit 8a42144
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 7 deletions.
16 changes: 11 additions & 5 deletions src/Randomer.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,21 @@ var Randomer = new Class({
if (segmentCrossed.length != 0) {
var lowerAttach = PointsHelper.lowestPointFromIn(newPoint, this.tenvelope);
var upperAttach = PointsHelper.highestPointFromIn(newPoint, this.tenvelope);
var toRemove = PointsHelper.sameSideAs(new Vector(this.tenvelope[lowerAttach], this.tenvelope[upperAttach]), newPoint, this.tenvelope);

var toRemove = [];
var refVect = new Vector(this.tenvelope[lowerAttach], this.tenvelope[upperAttach]);
if(refVect.by(new Vector(this.tenvelope[lowerAttach], newPoint))!=0) {
toRemove = PointsHelper.sameSideAs(refVect, newPoint, this.tenvelope);
}
/**
* If there's nothing to remove, we just add the new point
* between the upper and the lower attach points int the
* envelope.
*/
var min = (lowerAttach<upperAttach)? lowerAttach : upperAttach;
var max = lowerAttach+upperAttach-min;



if (toRemove.length == 0) {
var newEnvelope = [];
if (min == 0 && max == this.tenvelope.length-1) {
Expand All @@ -86,7 +92,7 @@ var Randomer = new Class({
* remove from the envelope.
*/
else {
//*
/*
if (min < toRemove[0] && max > toRemove[0]) {
min++;
this.tenvelope[min] = newPoint;
Expand Down Expand Up @@ -121,10 +127,10 @@ var Randomer = new Class({
}
//*/
/*
//*
this.tenvelope[toRemove[0]] = newPoint;
toRemove.splice(0,1);
toRemove.each(
toRemove.sort().reverse().each(
function(pointIndex) {
this.tenvelope.splice(pointIndex,1);
},this);
Expand Down
109 changes: 107 additions & 2 deletions test/Randomer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
(function() {

var points, envelope;
var points, points2, hardcore;

describe('Randomer class', {
before: function() {
Expand All @@ -15,6 +15,27 @@ describe('Randomer class', {
{'x': 5, 'y': 5},
{'x': 4, 'y': 3}
];

points2 = [
{'x':302,'y':280},
{'x':226,'y':270},
{'x':256,'y':304},
{'x':294,'y':296},
{'x':277,'y':275},
{'x':257,'y':277},
{'x':268,'y':283},
{'x':250,'y':281},
{'x':268,'y':294},
{'x':242,'y':291}
];

hardcore = [
{'x':100,'y':100},
{'x':150,'y':150},
{'x':200,'y':200},
{'x':300,'y':300},
{'x':400,'y':300}
];
},

"One iteration in the computation" : function() {
Expand Down Expand Up @@ -117,6 +138,60 @@ describe('Randomer class', {
]);
},

"and again" : function() {
var randomer = new Randomer(points);
randomer.tenvelope = [
{'x': 5, 'y': 5},
{'x': 10, 'y': 11},
{'x': 4, 'y': 3}
];
randomer.innerPoint = { 'x' : 19/3, 'y' : 19/3};
randomer.iterate({'x': 0, 'y': 0});
value_of(randomer.tenvelope).should_be([
{'x': 0, 'y': 0},
{'x': 10, 'y': 11},
{'x': 4, 'y': 3}
]);
},

"another error" : function() {
var randomer = new Randomer(points2);
randomer.tenvelope = [
{'x':242, 'y':291},
{'x':226, 'y':270},
{'x':257, 'y':277},
{'x':268, 'y':283},
{'x':268, 'y':294},
{'x':256, 'y':304}
];

randomer.innerPoint = { 'x' : (242+268+256)/3, 'y' : (291+294+304)/3};
randomer.iterate({'x': 302, 'y': 280});
value_of(randomer.tenvelope).should_be([
{'x':242, 'y':291},
{'x':226, 'y':270},
{'x':302, 'y':280},
//{'x':268, 'y':294},
{'x':256, 'y':304}
]);
},

"points alignés" : function() {
var randomer = new Randomer(points);
randomer.tenvelope = [
{'x': 400, 'y': 300},
{'x': 200, 'y': 200},
{'x': 100, 'y': 100}
];
randomer.innerPoint = { 'x' : 750/3, 'y' : 600/3};
randomer.iterate({'x': 300, 'y': 300});
value_of(randomer.tenvelope).should_be([
{'x': 400, 'y': 300},
{'x': 300, 'y': 300},
{'x': 100, 'y': 100}
]);
},

"Envelope calculation" : function() {
var randomer = new Randomer(points);
randomer.envelope();
Expand All @@ -125,7 +200,37 @@ describe('Randomer class', {
value_of(randomer.tenvelope).should_include({'x': 1, 'y': 10});
value_of(randomer.tenvelope).should_include({'x': 10, 'y': 11});
value_of(randomer.tenvelope).should_include({'x': 11, 'y': 0});
}
},

"Envelope calculation points2" : function() {
var randomer = new Randomer(points2);
randomer.envelope();
//var newpage=open("",'newpopup','width=400,height=400,toolbar=no,scrollbars=no,resizable=no');
//newpage.document.write("<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'>");
//newpage.document.write("<html><head><title>page popup</title></head>");
//newpage.document.write("<body>"+randomer.texte+"</body></html>");
value_of(randomer.tenvelope).should_have(6, "items");
value_of(randomer.tenvelope).should_include({'x': 226, 'y': 270});
value_of(randomer.tenvelope).should_include({'x': 242, 'y': 291});
value_of(randomer.tenvelope).should_include({'x': 256, 'y': 304});
value_of(randomer.tenvelope).should_include({'x': 294, 'y': 296});
value_of(randomer.tenvelope).should_include({'x': 302, 'y': 280});
value_of(randomer.tenvelope).should_include({'x': 277, 'y': 275});
},

"Envelope calculation hardcore" : function() {
//*
var randomer = new Randomer(hardcore);
randomer.envelope();
//var newpage=open("",'newpopup','width=400,height=400,toolbar=no,scrollbars=no,resizable=no');
//newpage.document.write("<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'>");
//newpage.document.write("<html><head><title>page popup</title></head>");
//newpage.document.write("<body>"+randomer.texte+"</body></html>");
value_of(randomer.tenvelope).should_have(3, "items");
value_of(randomer.tenvelope).should_include({'x': 100, 'y': 100});
value_of(randomer.tenvelope).should_include({'x': 300, 'y': 300});
value_of(randomer.tenvelope).should_include({'x': 400, 'y': 300});
//*/
}
});
})();

0 comments on commit 8a42144

Please sign in to comment.