Skip to content

Commit

Permalink
Move euclidean distance function to shared utils file
Browse files Browse the repository at this point in the history
  • Loading branch information
r59q committed Apr 29, 2024
1 parent 22ab3d6 commit 430f0ea
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
17 changes: 3 additions & 14 deletions src/script/mlmodels/KNNNonNormalizedMLModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import MLModel from '../domain/MLModel';
import { Point3D } from '../utils/graphUtils';
import { Point3D, distanceBetween } from '../utils/graphUtils';

export type LabelledPoint = {
classIndex: number;
Expand All @@ -28,8 +28,8 @@ class KNNNonNormalizedMLModel implements MLModel {
// Sort points by distance to live-data point
const orderedPoints = [...this.points];
orderedPoints.sort((a, b) => {
const aDist = this.distanceBetween(predictedPoint, a);
const bDist = this.distanceBetween(predictedPoint, b);
const aDist = distanceBetween(predictedPoint, a);
const bDist = distanceBetween(predictedPoint, b);
return aDist - bDist;
});

Expand All @@ -56,17 +56,6 @@ class KNNNonNormalizedMLModel implements MLModel {
z: filteredData.length > 2 ? filteredData[2] : 0,
};
}

private distanceBetween(point1: Point3D, point2: Point3D) {
const { x: x1, y: y1, z: z1 } = point1;
const { x: x2, y: y2, z: z2 } = point2;

const [dx, dy, dz] = [x2 - x1, y2 - y1, z2 - z1];

const squaredDistance = dx ** 2 + dy ** 2 + dz ** 2;

return Math.sqrt(squaredDistance);
}
}

export default KNNNonNormalizedMLModel;
11 changes: 11 additions & 0 deletions src/script/utils/graphUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,14 @@ export const extractAxisFromAccelerometerData = (
return data.map(val => val.z);
}
};

export const distanceBetween = (point1: Point3D, point2: Point3D): number => {
const { x: x1, y: y1, z: z1 } = point1;
const { x: x2, y: y2, z: z2 } = point2;

const [dx, dy, dz] = [x2 - x1, y2 - y1, z2 - z1];

const squaredDistance = dx ** 2 + dy ** 2 + dz ** 2;

return Math.sqrt(squaredDistance);
}

0 comments on commit 430f0ea

Please sign in to comment.