diff --git a/tests/tests.js b/tests/tests.js
index 0f348cc3..9b8e16bb 100644
--- a/tests/tests.js
+++ b/tests/tests.js
@@ -219,7 +219,7 @@ define(function(require) {
var point4 = new geometry.point(7,9);
var point5 = new geometry.point(4,6);
ok(!rect1.containsPoint(point1), "rect1 does not contain point1");
- ok(!rect1.containsPoint(point2), "rect1 does not contain point2");
+ ok(rect1.containsPoint(point2), "rect1 contains point2");
ok(rect1.containsPoint(point3), "rect1 contains point3");
ok(!rect1.containsPoint(point4), "rect1 does not contain point4");
ok(rect1.containsPoint(point5), "rect1 contains point5");
@@ -257,10 +257,10 @@ define(function(require) {
&& normalizedRect5.width===4
&& normalizedRect5.height===1, "rect5 successfully normalized by switching bottom right and top left corners");
});
- test("check rectangle constructor from top-left and bottom-right points", function() {
- var topLeft = new geometry.point(2,5);
- var bottomRight = new geometry.point(5,3);
- var rect = new geometry.rect(topLeft, bottomRight);
+ test("check rectangle constructor from bottom-left and top-right points", function() {
+ var bottomLeft = new geometry.point(2,3);
+ var topRight = new geometry.point(5,5);
+ var rect = new geometry.rect(bottomLeft, topRight);
equal(rect.x, 2 , "rect.x should be 2");
equal(rect.y, 3 , "rect.y should be 3");
equal(rect.width, 3 , "rect.width should be 3");
@@ -285,12 +285,12 @@ define(function(require) {
});
test("check bearing algorithm", function() {
var point1 = new geometry.point(0,0);
- var point2 = new geometry.point(2,0);
+ var point2 = new geometry.point(0,2);
var line1 = new geometry.line(point1, point2);
equal(line1.bearing(), "N", "Bearing of line1 should be N");
- var pointLondon = new geometry.point(51.50777816772461, -0.12805555760860443);
- var pointParis = new geometry.point(48.856614, 2.3522219000000177);
- var pointAmsterdam = new geometry.point(52.326947, 4.741287);
+ var pointLondon = new geometry.point(-0.12805555760860443, 51.50777816772461);
+ var pointParis = new geometry.point(2.3522219000000177, 48.856614);
+ var pointAmsterdam = new geometry.point(4.741287, 52.326947);
var lineLondonParis = new geometry.line(pointLondon, pointParis);
var lineParisLondon = new geometry.line(pointParis, pointLondon);
var lineLondonAmsterdam = new geometry.line(pointLondon, pointAmsterdam);
@@ -339,7 +339,7 @@ define(function(require) {
start();
};
- var rectFranceGermany = new geometry.rect(40,0,10,10);
+ var rectFranceGermany = new geometry.rect(0,40,10,10);
localArchive.getTitlesInCoords(rectFranceGermany, 10, callbackTitlesNearbyFound);
});
@@ -351,7 +351,7 @@ define(function(require) {
start();
};
- var rectFranceGermany = new geometry.rect(40,0,10,10);
+ var rectFranceGermany = new geometry.rect(0,40,10,10);
localArchive.getTitlesInCoords(rectFranceGermany, 2, callbackTitlesNearbyMaximumFound);
});
@@ -372,12 +372,12 @@ define(function(require) {
// Check coordinates of London
var x = titleLondon._geolocation.x;
var y = titleLondon._geolocation.y;
- equal(x, 51.50777816772461, "London should be at latitude 51.50777816772461");
- equal(y, -0.12805555760860443, "London should be at longitude -0.12805555760860443");
+ equal(y, 51.50777816772461, "London should be at latitude 51.50777816772461");
+ equal(x, -0.12805555760860443, "London should be at longitude -0.12805555760860443");
start();
};
- var pointLondon = new geometry.point(51, 0);
+ var pointLondon = new geometry.point(0, 51);
var maxDistance = 1;
var rectLondon = new geometry.rect(
pointLondon.x - maxDistance,
@@ -403,7 +403,7 @@ define(function(require) {
start();
};
- var pointAmsterdam = new geometry.point(55, 5);
+ var pointAmsterdam = new geometry.point(5, 55);
var maxDistance = 5;
var rectAmsterdam = new geometry.rect(
pointAmsterdam.x - maxDistance,
diff --git a/www/index.html b/www/index.html
index 97242b60..466374ef 100644
--- a/www/index.html
+++ b/www/index.html
@@ -177,7 +177,7 @@
QUnit 2, released under the MIT license
Icons, algorithms and inspiration from Evopedia (Qt version), released under the GPL v3 license
Mediawiki CSS from Wikidev, released under the GPL license
- Geometry library from JointJS, released under the Mozilla Public License v2 (small additions have been made to this javascript file)
+ Geometry library from JointJS, released under the Mozilla Public License v2 (some changes have been made to this javascript file : see its header)
Apache Cordova, released under the Apache license 2.0
Other platforms/versions
diff --git a/www/js/app.js b/www/js/app.js
index 73166b7b..dbcc7024 100644
--- a/www/js/app.js
+++ b/www/js/app.js
@@ -50,6 +50,8 @@ define(function(require) {
// This max distance has a default value, but the user can make it change
var maxDistanceArticlesNearbySearch = DEFAULT_MAX_DISTANCE_ARTICLES_NEARBY;
+ var currentCoordinates = null;
+
// Define behavior of HTML elements
$('#searchTitles').on('click', function(e) {
searchTitlesFromPrefix($('#prefix').val());
@@ -641,8 +643,6 @@ define(function(require) {
});
}
- var currentCoordinates = null;
-
/**
* Looks for titles located around where the device is geolocated
*/
@@ -670,7 +670,7 @@ define(function(require) {
$('#geolocationProgress').html("Found your location : lat:" + crd.latitude.toFixed(7) + ", long:" + crd.longitude.toFixed(7)
+ "
Now looking for articles around this location...");
- currentCoordinates = new geometry.point(crd.latitude, crd.longitude);
+ currentCoordinates = new geometry.point(crd.longitude, crd.latitude);
var rectangle = new geometry.rect(
currentCoordinates.x - maxDistanceArticlesNearbySearch,
diff --git a/www/js/lib/archive.js b/www/js/lib/archive.js
index a889d7c9..8cfa6b05 100644
--- a/www/js/lib/archive.js
+++ b/www/js/lib/archive.js
@@ -44,7 +44,7 @@ define(function(require) {
// Size of chunks read in the dump files : 128 KB
var CHUNK_SIZE = 131072;
// A rectangle representing all the earth globe
- var GLOBE_RECTANGLE = new geometry.rect(-91, -181, 182, 362);
+ var GLOBE_RECTANGLE = new geometry.rect(-181, -91, 362, 182);
/**
* LocalArchive class : defines a wikipedia dump on the filesystem
@@ -740,7 +740,7 @@ define(function(require) {
readCoordinates = function(byteArray, startIndex) {
var lat = util.readFloatFrom4Bytes(byteArray, startIndex, true);
var long = util.readFloatFrom4Bytes(byteArray, startIndex + 4, true);
- var point = new geometry.point(lat, long);
+ var point = new geometry.point(long, lat);
return point;
};
@@ -799,13 +799,13 @@ define(function(require) {
callbackCounterForTitlesInCoordsSearch++;
LocalArchive.getTitlesInCoordsInt(localArchive, coordinateFileIndex, pos0, targetRect, rectSW, maxTitles, titlePositionsFound, callbackFunction, callbackGetTitlesInCoordsInt);
}
- if (targetRect.intersect(rectNW)) {
- callbackCounterForTitlesInCoordsSearch++;
- LocalArchive.getTitlesInCoordsInt(localArchive, coordinateFileIndex, pos1, targetRect, rectNW, maxTitles, titlePositionsFound, callbackFunction, callbackGetTitlesInCoordsInt);
- }
if (targetRect.intersect(rectSE)) {
callbackCounterForTitlesInCoordsSearch++;
- LocalArchive.getTitlesInCoordsInt(localArchive, coordinateFileIndex, pos2, targetRect, rectSE, maxTitles, titlePositionsFound, callbackFunction, callbackGetTitlesInCoordsInt);
+ LocalArchive.getTitlesInCoordsInt(localArchive, coordinateFileIndex, pos1, targetRect, rectSE, maxTitles, titlePositionsFound, callbackFunction, callbackGetTitlesInCoordsInt);
+ }
+ if (targetRect.intersect(rectNW)) {
+ callbackCounterForTitlesInCoordsSearch++;
+ LocalArchive.getTitlesInCoordsInt(localArchive, coordinateFileIndex, pos2, targetRect, rectNW, maxTitles, titlePositionsFound, callbackFunction, callbackGetTitlesInCoordsInt);
}
if (targetRect.intersect(rectNE)) {
callbackCounterForTitlesInCoordsSearch++;
diff --git a/www/js/lib/geometry.js b/www/js/lib/geometry.js
index 69d5ca94..0f4dd2af 100644
--- a/www/js/lib/geometry.js
+++ b/www/js/lib/geometry.js
@@ -5,6 +5,11 @@
// Geometry library.
// (c) 2011-2013 client IO
// Copied from https://github.com/DavidDurman/joint/blob/master/src/geometry.js
+// and modified in order to :
+// - add a few functions necessary to Evopedia
+// - use the cartesian coordinate system (x goes right, y goes up)
+// It might have inverted the clockwise/anticlockwise directions for angles/rotations
+// but it doesn't matter because they are not used by Evopedia
define(function(require) {
@@ -255,15 +260,15 @@ define(function(require) {
* @returns {String} One of the following bearings : NE, E, SE, S, SW, W, NW, N
*/
bearing: function() {
- var lat1 = this.start.x * Math.PI / 180;
- var lat2 = this.end.x * Math.PI / 180;
- var lon1 = this.start.y;
- var lon2 = this.end.y;
- var dLon = (lon2 - lon1) * Math.PI / 180;
- var y = Math.sin(dLon) * Math.cos(lat2);
- var x = Math.cos(lat1) * Math.sin(lat2) -
- Math.sin(lat1) * Math.cos(lat2) * Math.cos(dLon);
- var brng = Math.atan2(y, x) / Math.PI * 180;
+ var lat1 = toRad(this.start.y);
+ var lat2 = toRad(this.end.y);
+ var lon1 = this.start.x;
+ var lon2 = this.end.x;
+ var dLon = toRad(lon2 - lon1);
+ var y = sin(dLon) * cos(lat2);
+ var x = cos(lat1) * sin(lat2) -
+ sin(lat1) * cos(lat2) * cos(dLon);
+ var brng = toDeg(atan2(y, x));
var bearings = ["NE", "E", "SE", "S", "SW", "W", "NW", "N"];
@@ -289,13 +294,13 @@ define(function(require) {
x = x.x;
}
if (w === undefined && h === undefined) {
- // The rectangle is built from topLeft and bottomRight points
- var topLeft = x;
- var bottomRight = y;
- this.x = topLeft.x;
- this.y = bottomRight.y;
- this.width = bottomRight.x - topLeft.x;
- this.height = topLeft.y - bottomRight.y;
+ // The rectangle is built from bottomLeft and topRight points
+ var bottomLeft = x;
+ var topRight = y;
+ this.x = bottomLeft.x;
+ this.y = bottomLeft.y;
+ this.width = topRight.x - bottomLeft.x;
+ this.height = topRight.y - bottomLeft.y;
}
else {
this.x = x;
@@ -321,18 +326,11 @@ define(function(require) {
ne : function() {
return point(this.x + this.width, this.y + this.height);
},
- // TODO : rename all this right/left/top/bottom terms because they are misleading (and wrong) in the Evopedia context : replace with N/S/E/W
origin: function() {
return point(this.x, this.y);
},
corner: function() {
- return point(this.x + this.width, this.y + this.height);
- },
- topRight: function() {
- return point(this.x + this.width, this.y);
- },
- bottomLeft: function() {
- return point(this.x, this.y + this.height);
+ return this.ne();
},
center: function() {
return point(this.x + this.width/2, this.y + this.height/2);
@@ -356,8 +354,8 @@ define(function(require) {
p = point(p);
var distToLeft = p.x - this.x;
var distToRight = (this.x + this.width) - p.x;
- var distToTop = p.y - this.y;
- var distToBottom = (this.y + this.height) - p.y;
+ var distToBottom = p.y - this.y;
+ var distToTop = (this.y + this.height) - p.y;
var closest = distToLeft;
var side = 'left';
@@ -365,21 +363,21 @@ define(function(require) {
closest = distToRight;
side = 'right';
}
- if (distToTop < closest) {
- closest = distToTop;
- side = 'top';
- }
if (distToBottom < closest) {
closest = distToBottom;
side = 'bottom';
}
+ if (distToTop < closest) {
+ closest = distToTop;
+ side = 'top';
+ }
return side;
},
// @return {bool} true if point p is insight me
containsPoint: function(p) {
p = point(p);
- if (p.x > this.x && p.x < this.x + this.width &&
- p.y > this.y && p.y < this.y + this.height) {
+ if (p.x >= this.x && p.x <= this.x + this.width &&
+ p.y >= this.y && p.y <= this.y + this.height) {
return true;
}
return false;
@@ -436,8 +434,8 @@ define(function(require) {
switch (side){
case "right": return point(this.x + this.width, p.y);
case "left": return point(this.x, p.y);
- case "bottom": return point(p.x, this.y + this.height);
- case "top": return point(p.x, this.y);
+ case "bottom": return point(p.x, this.y);
+ case "top": return point(p.x, this.y + this.height);
}
}
return p.adhereToRect(this);
@@ -453,10 +451,10 @@ define(function(require) {
// (clockwise, starting from the top side)
var sides = [
- line(this.origin(), this.topRight()),
- line(this.topRight(), this.corner()),
- line(this.corner(), this.bottomLeft()),
- line(this.bottomLeft(), this.origin())
+ line(this.sw(), this.nw()),
+ line(this.nw(), this.ne()),
+ line(this.ne(), this.se()),
+ line(this.se(), this.sw())
];
var connector = line(center, p);