Skip to content
This repository has been archived by the owner on Feb 19, 2022. It is now read-only.

Make path helpers work correctly with decimal values #300

Merged
merged 1 commit into from
Sep 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 51 additions & 45 deletions src/victory-primitives/path-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,74 +3,80 @@ import { range } from "lodash";

export default {
circle(x, y, size) {
const s = Math.round(size);
return `M ${Math.round(x)}, ${Math.round(y)} m ${-s}, 0
a ${s}, ${s} 0 1,0 ${s * 2},0
a ${s}, ${s} 0 1,0 ${-s * 2},0`;
const s = Math.floor(size);
return `
M ${Math.floor(x)}, ${Math.floor(y)}
m ${-s}, 0
a ${s}, ${s} 0 1,0 ${s * 2},0
a ${s}, ${s} 0 1,0 ${-s * 2},0
`;
},

square(x, y, size) {
const baseSize = 0.87 * size; // eslint-disable-line no-magic-numbers
const x0 = Math.round(x - baseSize);
const x1 = Math.round(x + baseSize);
const y0 = Math.round(y - baseSize);
const y1 = Math.round(y + baseSize);
const x0 = Math.floor(x - baseSize);
const y1 = Math.floor(y + baseSize);
const distance = Math.abs(x0 - y1);
return `M ${x0}, ${y1}
L ${x1}, ${y1}
L ${x1}, ${y0}
L ${x0}, ${y0}
z`;
h${distance}
v-${distance}
h-${distance}
z`;
},

diamond(x, y, size) {
const baseSize = 0.87 * size; // eslint-disable-line no-magic-numbers
const length = Math.sqrt(2 * (baseSize * baseSize));
return `M ${Math.round(x)}, ${Math.round(y + length)}
L ${Math.round(x + length)}, ${Math.round(y)}
L ${Math.round(x)}, ${Math.round(y - length)}
L ${Math.round(x - length)}, ${Math.round(y)}
const distance = Math.floor(length);
return `M ${Math.floor(x)}, ${Math.floor(y + length)}
l ${distance}, -${distance}
l -${distance}, -${distance}
l -${distance}, ${distance}
l ${distance}, ${distance}
z`;
},

triangleDown(x, y, size) {
const height = (size / 2 * Math.sqrt(3));
const x0 = Math.round(x - size);
const x1 = Math.round(x + size);
const y0 = Math.round(y - size);
const y1 = Math.round(y + height);
const height = size / 2 * Math.sqrt(3);
const x0 = Math.floor(x - size);
const x1 = Math.floor(x + size);
const y0 = Math.floor(y - size);
const y1 = Math.floor(y + height);
return `M ${x0}, ${y0}
L ${x1}, ${y0}
L ${Math.round(x)}, ${y1}
z`;
L ${x1}, ${y0}
L ${Math.floor(x)}, ${y1}
z`;
},

triangleUp(x, y, size) {
const height = (size / 2 * Math.sqrt(3));
const x0 = Math.round(x - size);
const x1 = Math.round(x + size);
const y0 = Math.round(y - height);
const y1 = Math.round(y + size);
const height = size / 2 * Math.sqrt(3);
const x0 = Math.floor(x - size);
const x1 = Math.floor(x + size);
const y0 = Math.floor(y - height);
const y1 = Math.floor(y + size);
return `M ${x0}, ${y1}
L ${x1}, ${y1}
L ${Math.round(x)}, ${y0}
z`;
L ${x1}, ${y1}
L ${Math.floor(x)}, ${y0}
z`;
},

plus(x, y, size) {
const baseSize = 1.1 * size; // eslint-disable-line no-magic-numbers
return `M ${Math.round(x - baseSize / 2.5)}, ${Math.round(y + baseSize)}
L ${Math.round(x + baseSize / 2.5)}, ${Math.round(y + baseSize)}
L ${Math.round(x + baseSize / 2.5)}, ${Math.round(y + baseSize / 2.5)}
L ${Math.round(x + baseSize)}, ${Math.round(y + baseSize / 2.5)}
L ${Math.round(x + baseSize)}, ${Math.round(y - baseSize / 2.5)}
L ${Math.round(x + baseSize / 2.5)}, ${Math.round(y - baseSize / 2.5)}
L ${Math.round(x + baseSize / 2.5)}, ${Math.round(y - baseSize)}
L ${Math.round(x - baseSize / 2.5)}, ${Math.round(y - baseSize)}
L ${Math.round(x - baseSize / 2.5)}, ${Math.round(y - baseSize / 2.5)}
L ${Math.round(x - baseSize)}, ${Math.round(y - baseSize / 2.5)}
L ${Math.round(x - baseSize)}, ${Math.round(y + baseSize / 2.5)}
L ${Math.round(x - baseSize / 2.5)}, ${Math.round(y + baseSize / 2.5)}
z`;
const distance = Math.floor(baseSize / 2.5) * 2 || 1;
return `
M ${Math.floor(x - baseSize / 2.5)}, ${Math.floor(y + baseSize)}
v-${distance}
h-${distance}
v-${distance}
h${distance}
v-${distance}
h${distance}
v${distance}
h${distance}
v${distance}
h-${distance}
v${distance}
z`;
},

star(x, y, size) {
Expand Down
4 changes: 2 additions & 2 deletions test/client/spec/victory-primitives/path-helper.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe("path-helpers", () => {
it("draws a path for a square at the correct location", () => {
const pathResult = PathHelpers.square(x, y, size);
const baseSize = 0.87 * size;
expect(pathResult).to.contain(`M ${Math.round(x - baseSize)}, ${Math.round(y + baseSize)}`);
expect(pathResult).to.contain(`M ${Math.floor(x - baseSize)}, ${Math.floor(y + baseSize)}`);
});
});

Expand Down Expand Up @@ -48,7 +48,7 @@ describe("path-helpers", () => {
const pathResult = PathHelpers.plus(0, 0, 1);
const baseSize = 1.1 * size;
expect(pathResult).to.contain(
`M ${Math.round(x - baseSize / 2.5)}, ${Math.round(y + baseSize)}`
`M ${Math.floor(x - baseSize / 2.5)}, ${Math.floor(y + baseSize)}`
);
});
});
Expand Down