diff --git a/src/utils.test.ts b/src/utils.test.ts index 5b5b70c..ff34386 100644 --- a/src/utils.test.ts +++ b/src/utils.test.ts @@ -9,8 +9,8 @@ import { getDistance, randomInterval, cumulativeBins, - sliceWords, - getSliceOfWords, + slideWords, + getWordSlide, } from "./utils"; const origin: Coordinate = { @@ -203,38 +203,38 @@ describe("CumulativeBins", () => { describe("slideWords", () => { describe("Slide one word", () => { it("On the bottom", () => { - expect(sliceWords([originRectangle], { x: 0, y: 2 })).toEqual([ + expect(slideWords([originRectangle], { x: 0, y: 2 })).toEqual([ { x: 0, y: 2, width: 1, height: 1 }, ]); }); it("On the top", () => { expect( - sliceWords([{ x: 1, y: 4, width: 4, height: 4 }], { x: 0, y: -2 }) + slideWords([{ x: 1, y: 4, width: 4, height: 4 }], { x: 0, y: -2 }) ).toEqual([{ x: 1, y: 2, width: 4, height: 4 }]); }); it("On the right", () => { expect( - sliceWords([{ x: 2, y: 0, width: 1, height: 1 }], { x: 2, y: 0 }) + slideWords([{ x: 2, y: 0, width: 1, height: 1 }], { x: 2, y: 0 }) ).toEqual([{ x: 4, y: 0, width: 1, height: 1 }]); }); it("On the left", () => { expect( - sliceWords([{ x: 2, y: 0, width: 1, height: 1 }], { x: -2, y: 0 }) + slideWords([{ x: 2, y: 0, width: 1, height: 1 }], { x: -2, y: 0 }) ).toEqual([{ x: 0, y: 0, width: 1, height: 1 }]); }); it("On the right and bottom", () => { expect( - sliceWords([{ x: 2, y: 0, width: 1, height: 1 }], { x: 2, y: 4 }) + slideWords([{ x: 2, y: 0, width: 1, height: 1 }], { x: 2, y: 4 }) ).toEqual([{ x: 4, y: 4, width: 1, height: 1 }]); }); it("On the left and top", () => { expect( - sliceWords([{ x: 2, y: 4, width: 1, height: 1 }], { x: -2, y: -2 }) + slideWords([{ x: 2, y: 4, width: 1, height: 1 }], { x: -2, y: -2 }) ).toEqual([{ x: 0, y: 2, width: 1, height: 1 }]); }); }); @@ -242,7 +242,7 @@ describe("slideWords", () => { describe("Slide multiple word", () => { it("On the bottom and right", () => { expect( - sliceWords( + slideWords( [ { x: 1, y: 4, width: 4, height: 4 }, { x: 6, y: 9, width: 4, height: 4 }, @@ -257,7 +257,7 @@ describe("slideWords", () => { it("On the top and left", () => { expect( - sliceWords( + slideWords( [ { x: 1, y: 4, width: 4, height: 4 }, { x: 6, y: 9, width: 4, height: 4 }, @@ -275,7 +275,7 @@ describe("slideWords", () => { describe("getSliceOfWords", () => { it("No move", () => { expect( - getSliceOfWords( + getWordSlide( { x: 3, y: 3, width: 4, height: 4 }, { x: 1, y: 1, width: 4, height: 4 } ) @@ -284,7 +284,7 @@ describe("getSliceOfWords", () => { it("Move on the right", () => { expect( - getSliceOfWords( + getWordSlide( { x: 2, y: 3, width: 4, height: 4 }, { x: 1, y: 1, width: 4, height: 4 } ) @@ -293,7 +293,7 @@ describe("getSliceOfWords", () => { it("Move on the left and top", () => { expect( - getSliceOfWords( + getWordSlide( { x: 7, y: 8, width: 4, height: 4 }, { x: 3, y: 5, width: 4, height: 4 } ) diff --git a/src/utils.ts b/src/utils.ts index 9570901..81430f6 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,4 +1,3 @@ -import { p, R } from "vitest/dist/types-7cd96283"; import { CONTAINER_HEIGHT, CONTAINER_WIDTH, @@ -105,9 +104,13 @@ export const getTheCircle = (passRect: Rectangle[]): Circle => { return { x: centerMass.x, y: centerMass.y, radius }; }; +// This function return a random float between min and max export const randomInterval = (min: number, max: number): number => { return Math.random() * (max - min) + min; }; + +// This function return the cumulative weight of an array : for example [1, 2, 3, 4] become [1, 3, 6, 10] +// source: https://quickref.me/create-an-array-of-cumulative-sum.html export const cumulativeBins = (bin: number[]): number[] => { return bin.map( ( @@ -134,8 +137,10 @@ export const placeWordOnOuterCircle = ( weight[inter] += 1; + const maxWeight = Math.max(...weight); + // substract the max to each element to promote other interval - weight = weight.map((a) => Math.max(...weight) - a); + weight = weight.map((a) => maxWeight - a); let angleInter = { x: 0, y: 360 }; @@ -293,7 +298,7 @@ export const boundParent = ( return newParentBound; }; -export const getSliceOfWords = ( +export const getWordSlide = ( parent: Rectangle, bound: Rectangle ): Coordinate => { @@ -310,7 +315,7 @@ export const getSliceOfWords = ( return { x: differenceX, y: differenceY }; }; -export const sliceWords = ( +export const slideWords = ( words: Rectangle[], slice: Coordinate ): Rectangle[] => {