Skip to content

Commit

Permalink
fix: xyObjectJoinX with y=0
Browse files Browse the repository at this point in the history
  • Loading branch information
lpatiny committed Mar 10, 2021
1 parent 1bc2fcf commit feaa0fe
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/xy/__tests__/xyJoinX.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe('xyJoinX', function () {
it('join with 0', () => {
let data = { x: [2, 3, 6], y: [0, 0, 0] };
expect(xyJoinX(data)).toStrictEqual({
x: [2.5, 6],
x: [2, 6],
y: [0, 0],
});
});
Expand Down
6 changes: 2 additions & 4 deletions src/xy/xyJoinX.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,11 @@ export function xyJoinX(data = {}, options = {}) {

if (difference <= currentDelta) {
// we join
if (y[position] === 0 && y[i] === 0) {
x[position] = (x[position] + x[i]) / 2;
} else {
if (y[position] !== 0 || y[i] !== 0) {
x[position] =
(x[position] * y[position] + x[i] * y[i]) / (y[position] + y[i]);
y[position] += y[i];
}
y[position] += y[i];
} else {
position++;
x[position] = x[i];
Expand Down
12 changes: 12 additions & 0 deletions src/xyObject/__tests__/xyObjectJoinX.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,16 @@ test('xyObjectJoinX', () => {
{ x: 0.5, y: 2 },
{ x: 3.5, y: 4 },
]);

let arrayXYzero = [
{ x: 0, y: 0 },
{ x: 1, y: 0 },
{ x: 3, y: 0 },
{ x: 4, y: 0 },
];

expect(xyObjectJoinX(arrayXYzero, { xError: 1.1 })).toStrictEqual([
{ x: 0, y: 0 },
{ x: 3, y: 0 },
]);
});
8 changes: 5 additions & 3 deletions src/xyObject/xyObjectJoinX.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ export function xyObjectJoinX(points, options = {}) {
for (let point of points) {
if (point.x - current.x <= xError) {
// weighted sum
current.x =
(point.y / (current.y + point.y)) * (point.x - current.x) + current.x;
current.y += point.y;
if (current.y !== 0 || point.y !== 0) {
current.x =
(point.y / (current.y + point.y)) * (point.x - current.x) + current.x;
current.y += point.y;
}
} else {
current = {
x: point.x,
Expand Down

0 comments on commit feaa0fe

Please sign in to comment.