From bf834a6565cddbdde9a4068e47cc81ce9a5b60d0 Mon Sep 17 00:00:00 2001 From: Fabian Wunsch Date: Sat, 9 Apr 2022 21:04:08 +0200 Subject: [PATCH 1/3] Cleaned up calculateCenter function While I still don't understand what it's doing exactly, I noticed that the 2 iterations of the loop are unnecessary, and afterwards found out that the change to area is equal to f. Furthermore I replaced the `~~` flooring with Math.floor for better readability, since this function get's executed once per submission and the speed benefits of `~~` are neglectable. --- web/_js/draw.js | 42 +++++++++++++++++------------------------- 1 file changed, 17 insertions(+), 25 deletions(-) diff --git a/web/_js/draw.js b/web/_js/draw.js index a028fc2c..99bf6f9a 100644 --- a/web/_js/draw.js +++ b/web/_js/draw.js @@ -231,33 +231,25 @@ function initDraw(){ function calculateCenter(path){ var area = 0, - i, - j, - point1, - point2; + i, + j, + point1, + point2, + x = 0, + y = 0, + f; - for (i = 0, j = path.length - 1; i < path.length; j=i,i++) { - point1 = path[i]; - point2 = path[j]; - area += point1[0] * point2[1]; - area -= point1[1] * point2[0]; - } - area *= 3; - - var x = 0, - y = 0, - f; + for (i = 0, j = path.length - 1; i < path.length; j=i,i++) { + point1 = path[i]; + point2 = path[j]; + f = point1[0] * point2[1] - point2[0] * point1[1]; + area += f; + x += (point1[0] + point2[0]) * f; + y += (point1[1] + point2[1]) * f; + } + area *= 3; - for (i = 0, j = path.length - 1; i < path.length; j=i,i++) { - point1 = path[i]; - point2 = path[j]; - f = point1[0] * point2[1] - point2[0] * point1[1]; - x += (point1[0] + point2[0]) * f; - y += (point1[1] + point2[1]) * f; - } - - return [~~(x / area)+0.5, ~~(y / area)+0.5]; - + return [Math.min(x / area)+0.5, Math.max(y / area)+0.5]; } function undo(){ From 706f2b32b0a730eb85da78943b2c1d1344f390d0 Mon Sep 17 00:00:00 2001 From: Fabian Wunsch Date: Sat, 9 Apr 2022 21:08:36 +0200 Subject: [PATCH 2/3] Added a cleanup step to re calulate the center This change calculates the center of each entry and compares it to the current center. If they are different, the center get's updated. --- tools/formatter.py | 48 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/tools/formatter.py b/tools/formatter.py index 60681f3c..f9af7a1f 100644 --- a/tools/formatter.py +++ b/tools/formatter.py @@ -188,7 +188,49 @@ def convert_subreddit_to_website(entry: dict): entry["subreddit"] = "" return entry - + +def calculate_center(path: list): + """ + Caluclates the center of a polygon + + adapted from /web/_js/draw.js:calucalteCenter() + """ + area = 0 + x = 0 + y = 0 + + for i in range(len(path)): + point1 = path[i] + point2 = path[i-1 if i != 0 else len(path)-1] + f = point1[0] * point2[1] - point2[0] * point1[1] + area += f + x += (point1[0] + point2[0]) * f + y += (point1[1] + point2[1]) * f + + area *= 3 + + if area != 0: + return [x // area + 0.5, y // area + 0.5] + else: + # get the center of a straight line + max_x = max(i[0] for i in path) + min_x = min(i[0] for i in path) + max_y = max(i[1] for i in path) + min_y = min(i[1] for i in path) + return [(max_x + min_x) // 2 + 0.5, (max_y + min_y) // 2 + 0.5] + +def update_center(entry: dict): + """ + checks if the center of a entry is up to date, and updates it if it's either missing or outdated + """ + if 'path' not in entry: + return entry + path = entry['path'] + if len(path) > 1: + calculated_center = calculate_center(path) + if 'center' not in entry or entry['center'] != calculated_center: + entry['center'] = calculated_center + return entry def validate(entry: dict): """ @@ -256,6 +298,8 @@ def print_(*args, **kwargs): entry = fix_no_protocol_urls(entry) print_("Removing extras...") entry = remove_extras(entry) + print_("Updating center") + entry = update_center(entry) print_("Validating...") status_code = validate(entry) print_("Completed!") @@ -287,4 +331,4 @@ def go(path): print("Writing completed. All done.") - go("../web/atlas.json") \ No newline at end of file + go("../web/atlas.json") From 93dc57d61862c376362005f4462e65fc0bbc99fe Mon Sep 17 00:00:00 2001 From: Fabian Wunsch Date: Sat, 9 Apr 2022 21:22:56 +0200 Subject: [PATCH 3/3] Small bugfix in js --- web/_js/draw.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web/_js/draw.js b/web/_js/draw.js index 99bf6f9a..7026b86e 100644 --- a/web/_js/draw.js +++ b/web/_js/draw.js @@ -249,7 +249,7 @@ function initDraw(){ } area *= 3; - return [Math.min(x / area)+0.5, Math.max(y / area)+0.5]; + return [Math.floor(x / area)+0.5, Math.floor(y / area)+0.5]; } function undo(){