fix: points not being normalized on single-elem resize (#5581)

This commit is contained in:
David Luzar 2022-08-16 21:51:43 +02:00 committed by GitHub
parent 27cf5ed17e
commit ad0c4c4c78
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 45 additions and 4 deletions

View file

@ -14,6 +14,7 @@ export const rescalePoints = (
dimension: 0 | 1,
newSize: number,
points: readonly Point[],
normalize: boolean,
): Point[] => {
const coordinates = points.map((point) => point[dimension]);
const maxCoordinate = Math.max(...coordinates);
@ -21,10 +22,35 @@ export const rescalePoints = (
const size = maxCoordinate - minCoordinate;
const scale = size === 0 ? 1 : newSize / size;
return points.map((point): Point => {
let nextMinCoordinate = Infinity;
const scaledPoints = points.map((point): Point => {
const newCoordinate = point[dimension] * scale;
const newPoint = [...point];
newPoint[dimension] = newCoordinate;
if (newCoordinate < nextMinCoordinate) {
nextMinCoordinate = newCoordinate;
}
return newPoint as unknown as Point;
});
if (!normalize) {
return scaledPoints;
}
if (scaledPoints.length === 2) {
// we don't translate two-point lines
return scaledPoints;
}
const translation = minCoordinate - nextMinCoordinate;
const nextPoints = scaledPoints.map(
(scaledPoint) =>
scaledPoint.map((value, currentDimension) => {
return currentDimension === dimension ? value + translation : value;
}) as [number, number],
);
return nextPoints;
};