diff --git a/src/constants.ts b/src/constants.ts index 8a10977..5ac6f44 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -11,11 +11,4 @@ export const DEFAULT_RECT = { height: 10, }; -export const INTERVAL = { - a: { x: 0, y: 89 }, - b: { x: 90, y: 179 }, - c: { x: 180, y: 269 }, - d: { x: 270, y: 360 }, -}; - export const NUMBER_OF_INTERVALS = 6; diff --git a/src/utils.ts b/src/utils.ts index 0c6ad35..ba51a27 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -2,7 +2,7 @@ import { CONTAINER_HEIGHT, CONTAINER_WIDTH, DEFAULT_RECT, - INTERVAL, + NUMBER_OF_INTERVALS, } from "./constants"; export type Word = { @@ -116,6 +116,11 @@ export const cumulativeBins = (bin: number[]): number[] => { ); }; +// https://stackoverflow.com/questions/36947847/how-to-generate-range-of-numbers-from-0-to-n-in-es2015-only +// range(0, 9, 2) => [0, 2, 4, 6, 8] +export const range = (from: number, to: number, step: number) => + [...Array(Math.floor((to - from) / step) + 1)].map((_, i) => from + i * step); + // This function puts the word in a random place on a circle export const placeWordOnOuterCircle = ( w: Rectangle, @@ -141,16 +146,21 @@ export const placeWordOnOuterCircle = ( // Add to weights the position that has just been drawn weight[inter] += 1; - let angleInter = { x: 0, y: 360 }; + // Calculate the size of each intervals + const ratio = 360 / NUMBER_OF_INTERVALS; + + // create the intervals + const rangeInterval = range(0, 360, ratio); - if (inter === 0) { - angleInter = INTERVAL.a; - } else if (inter === 1) { - angleInter = INTERVAL.b; - } else if (inter === 2) { - angleInter = INTERVAL.c; - } else if (inter === 3) { - angleInter = INTERVAL.d; + let angleInter; + + if (Number.isInteger(inter)) { + angleInter = { + x: rangeInterval[inter], + y: rangeInterval[inter + 1] - 1, + }; + } else { + angleInter = { x: 0, y: 360 }; } const angle = randomInterval(angleInter.x, angleInter.y);