Merge pull request #783 from Tonny2442/area-feature

add sort by area (largest to smallest)
This commit is contained in:
Alex Tsernoh 2022-04-07 19:17:17 +01:00 committed by GitHub
commit ece70714e6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 1 deletions

View file

@ -39,9 +39,15 @@ function getPositionOfEntry(entry){
return [parseInt(startX), parseInt(startY)];
}
const areaMap = new Map();
// Modified from https://stackoverflow.com/a/33670691
function calcPolygonArea(vertices) {
var hit = areaMap.get(vertices);
if (hit != null) {
return hit;
}
var total = 0;
for (var i = 0, l = vertices.length; i < l; i++) {
@ -54,5 +60,8 @@ function calcPolygonArea(vertices) {
total -= (subX * subY * 0.5);
}
return Math.floor(Math.abs(total));
var area = Math.floor(Math.abs(total));
areaMap.set(vertices, area);
return area;
}

View file

@ -449,6 +449,11 @@ function initView(){
return 0;
}
break;
case "area":
sortFunction = function(a, b){
return calcPolygonArea(b.path) - calcPolygonArea(a.path);
}
break;
case "relevant":
sortFunction = function(a, b){
if(a.name.toLowerCase().indexOf(filter) !== -1 && b.name.toLowerCase().indexOf(filter) !== -1){

View file

@ -126,6 +126,7 @@ <h1 id="title">The 2022 /r/place Atlas</h1>
<option value="alphaDesc">↑ Alphabetical</option>
<option value="newest">Newest</option>
<option value="oldest">Oldest</option>
<option value="area">Area</option>
</select>
</div>
</div>