Skip to content

Commit

Permalink
feat: get the slice and slice the word
Browse files Browse the repository at this point in the history
  • Loading branch information
RoxaneBurri committed Mar 17, 2023
1 parent 3f3af71 commit 4b9b958
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 1 deletion.
103 changes: 103 additions & 0 deletions src/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
getDistance,
randomInterval,
cumulativeBins,
sliceWords,
getSliceOfWords,
} from "./utils";

const origin: Coordinate = {
Expand Down Expand Up @@ -197,3 +199,104 @@ describe("CumulativeBins", () => {
expect(cumulativeBins([4, -2, 1, 3])).toEqual([4, 2, 3, 6]);
});
});

describe("slideWords", () => {
describe("Slide one word", () => {
it("On the bottom", () => {
expect(sliceWords([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 })
).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 })
).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 })
).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 })
).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 })
).toEqual([{ x: 0, y: 2, width: 1, height: 1 }]);
});
});

describe("Slide multiple word", () => {
it("On the bottom and right", () => {
expect(
sliceWords(
[
{ x: 1, y: 4, width: 4, height: 4 },
{ x: 6, y: 9, width: 4, height: 4 },
],
{ x: 1, y: 2 }
)
).toEqual([
{ x: 2, y: 6, width: 4, height: 4 },
{ x: 7, y: 11, width: 4, height: 4 },
]);
});

it("On the top and left", () => {
expect(
sliceWords(
[
{ x: 1, y: 4, width: 4, height: 4 },
{ x: 6, y: 9, width: 4, height: 4 },
],
{ x: -1, y: -2 }
)
).toEqual([
{ x: 0, y: 2, width: 4, height: 4 },
{ x: 5, y: 7, width: 4, height: 4 },
]);
});
});
});

describe("getSliceOfWords", () => {
it("No move", () => {
expect(
getSliceOfWords(
{ x: 3, y: 3, width: 4, height: 4 },
{ x: 1, y: 1, width: 4, height: 4 }
)
).toEqual({ x: 0, y: 0 });
});

it("Move on the right", () => {
expect(
getSliceOfWords(
{ x: 2, y: 3, width: 4, height: 4 },
{ x: 1, y: 1, width: 4, height: 4 }
)
).toEqual({ x: 1, y: 0 });
});

it("Move on the left and top", () => {
expect(
getSliceOfWords(
{ x: 7, y: 8, width: 4, height: 4 },
{ x: 3, y: 5, width: 4, height: 4 }
)
).toEqual({ x: -2, y: -1 });
});
});
31 changes: 30 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { R } from "vitest/dist/types-7cd96283";
import { p, R } from "vitest/dist/types-7cd96283";
import {
CONTAINER_HEIGHT,
CONTAINER_WIDTH,
Expand Down Expand Up @@ -292,3 +292,32 @@ export const boundParent = (

return newParentBound;
};

export const getSliceOfWords = (
parent: Rectangle,
bound: Rectangle
): Coordinate => {
const boundCentered: Rectangle = {
x: bound.x + bound.width / 2,
y: bound.y + bound.height / 2,
width: bound.width,
height: bound.height,
};

const differenceX = boundCentered.x - parent.x;
const differenceY = boundCentered.y - parent.y;

return { x: differenceX, y: differenceY };
};

export const sliceWords = (
words: Rectangle[],
slice: Coordinate
): Rectangle[] => {
words.map((w) => {
w.x = w.x + slice.x;
w.y = w.y + slice.y;
});

return words;
};

0 comments on commit 4b9b958

Please sign in to comment.