From 871502044b1a6e074b6e8731bf6a6f67c2359e3e Mon Sep 17 00:00:00 2001 From: RoxaneBurri Date: Wed, 15 Mar 2023 14:41:50 +0100 Subject: [PATCH] fix: weight global reference --- src/App.tsx | 15 +++------------ src/utils.ts | 34 ++++++++++++++++++++++------------ 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 45fb0f3..fb131a6 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -36,20 +36,11 @@ const Wordcloud = () => { y: CENTER_Y, }; - // let cumulWeight = [0, 0, 0, 0]; - let weight = [0, 0, 0, 0]; + const weight = [1, 1, 1, 1]; const newPositions = rectsToPlace.slice(1).reduce( (placedElements, rect) => { - console.log(weight); - // move the word - const futurePositionWord = futurPosition( - rect, - placedElements, - 3, - weight - ); - const futureWord = futurePositionWord.rect; - weight = futurePositionWord.weight; + const futureWord = futurPosition(rect, placedElements, 3, weight); + // console.log("weight reduce", weight); return [...placedElements, futureWord]; }, [centeredRect] diff --git a/src/utils.ts b/src/utils.ts index ef301e1..0159b68 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -101,7 +101,7 @@ export const getTheCircle = (passRect: Rectangle[]): Circle => { }; export const randomInterval = (min: number, max: number): number => { - return Math.floor(Math.random() * (max - min + 1) + min); + return Math.floor(Math.random() * (max - min) + min); }; export const cumulativeBins = (bin: number[]): number[] => { @@ -113,19 +113,28 @@ export const cumulativeBins = (bin: number[]): number[] => { ); }; +// export const getWeight = (passRect: Rectangle[]) => { +// const weight = +// }; + // This function put the word in a random place export const placeWordOnOuterCircle = ( w: Rectangle, passRect: Rectangle[], weight: number[] -): FuturPosition => { +): Rectangle => { // Chose the parent face + console.log("weight place", weight); const cumulativeWeight = cumulativeBins(weight); - const randomInter = randomInterval(0, Math.max(...cumulativeWeight)); + console.log("cumul place", cumulativeWeight); + + const randomInter = randomInterval(0, cumulativeWeight.slice(-1)[0]); + console.log("random", randomInter); const inter = cumulativeWeight.findIndex((el) => el > randomInter); - weight[inter] = weight[inter] + 1; - console.log(weight); + console.log("inter", inter); + + weight[inter] += 1; let angleInter = { x: 0, y: 360 }; @@ -139,16 +148,19 @@ export const placeWordOnOuterCircle = ( angleInter = interval.d; } + console.log("angle interval", angleInter); + const angle = randomInterval(angleInter.x, angleInter.y); - // const angle = Math.random() * 360; + console.log("angle", angle); + const circle = getTheCircle(passRect); const newPosition = { ...w, x: circle.radius * Math.cos(angle) + circle.x, y: circle.radius * Math.sin(angle) + circle.y, }; - return { rect: newPosition, weight }; + return newPosition; }; export const getMoveDirection = ( @@ -172,14 +184,12 @@ export const futurPosition = ( passRect: Rectangle[], step: number, weight: number[] -): FuturPosition => { +): Rectangle => { let isCollision = false; // console.log(weight); // put the word in random place around the parent - const move = placeWordOnOuterCircle(word, passRect, weight); - let movedWord = move.rect; - weight = move.weight; + let movedWord = placeWordOnOuterCircle(word, passRect, weight); let iter = 0; let displacement = 0; do { @@ -215,7 +225,7 @@ export const futurPosition = ( displacement = Math.abs(stepX) + Math.abs(stepY); iter++; } while (!isCollision && displacement > 2 && iter < 300); - return { rect: movedWord, weight }; + return movedWord; }; export const areCentersTooClose = (