From 3b272dacb36642fb61f93fa42a4142382fb882c5 Mon Sep 17 00:00:00 2001 From: Eddie Whiteside Date: Thu, 1 Jun 2023 11:01:42 +0100 Subject: [PATCH] fix: deterministic behaviour for radar by storing the random fractions when generating rd.json (#372) * Store the random blip positions when generating rd.json Fixes: https://github.com/AOEpeople/aoe_technology_radar/issues/148 Simple solution to creating stable blips is to store the randomly generated angle and radius in rg.json. Attempted a few hash functions but found it hard to spread blips 'nicely' (they tended to clump into groups or sections of the radar). * distribute points within .1 and .9 of the arc --------- Co-authored-by: Bastian Ike --- dist_scripts/scripts/generateJson/radar.js | 2 ++ scripts/generateJson/radar.ts | 2 ++ src/components/Chart/BlipPoints.tsx | 14 ++++++++------ src/model.ts | 2 ++ 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/dist_scripts/scripts/generateJson/radar.js b/dist_scripts/scripts/generateJson/radar.js index 79e667d..3da6503 100644 --- a/dist_scripts/scripts/generateJson/radar.js +++ b/dist_scripts/scripts/generateJson/radar.js @@ -199,6 +199,8 @@ var addRevisionToItem = function (item, revision) { quadrant: "", body: "", info: "", + angleFraction: Math.random(), + radiusFraction: Math.random() }; } var newItem = __assign(__assign(__assign({}, item), revision), { body: ignoreEmptyRevisionBody(revision, item) }); if (revisionCreatesNewHistoryEntry(revision, item)) { diff --git a/scripts/generateJson/radar.ts b/scripts/generateJson/radar.ts index 4b3b70c..c0135be 100644 --- a/scripts/generateJson/radar.ts +++ b/scripts/generateJson/radar.ts @@ -146,6 +146,8 @@ const addRevisionToItem = ( quadrant: "", body: "", info: "", + angleFraction: Math.random(), + radiusFraction: Math.random() }, revision: Revision ): Item => { diff --git a/src/components/Chart/BlipPoints.tsx b/src/components/Chart/BlipPoints.tsx index 0797908..c191147 100644 --- a/src/components/Chart/BlipPoints.tsx +++ b/src/components/Chart/BlipPoints.tsx @@ -26,13 +26,15 @@ function generateCoordinates( ringPadding = 0.7; // radian between 0 and 90 degrees - const randomDegree = (Math.random() * 90 * pi) / 180; + const randomDegree = ((0.1 + (blip.angleFraction || Math.random()) * .8) * 90 * pi) / 180; // random distance from the centre of the radar, but within given ring. Also, with some "padding" so the points don't touch ring borders. - const radius = randomBetween( + const radius = pointBetween( previousRingRadius + ringPadding, - ringRadius - ringPadding + ringRadius - ringPadding, + blip.radiusFraction || Math.random() ); - /* + + /* Multiples of PI/2. To apply the calculated position to the specific quadrant. Order here is counter-clockwise, so we need to "invert" quadrant positions (i.e. swap 2 with 4) */ @@ -44,8 +46,8 @@ function generateCoordinates( }; } -function randomBetween(min: number, max: number): number { - return Math.random() * (max - min) + min; +function pointBetween(min: number, max: number, amount: number): number { + return amount * (max - min) + min; } function distanceBetween(point1: Point, point2: Point): number { diff --git a/src/model.ts b/src/model.ts index 8eaa3cf..f49d42d 100644 --- a/src/model.ts +++ b/src/model.ts @@ -25,6 +25,8 @@ export type Item = ItemAttributes & { info: string; flag: FlagType; revisions: Revision[]; + angleFraction?: number; + radiusFraction?: number; }; export type Blip = Item & {