Skip to content

Instantly share code, notes, and snippets.

@dynamite-bud
Created July 9, 2022 04:57
Show Gist options
  • Select an option

  • Save dynamite-bud/c315e122b6b226275bd3abc9862d27df to your computer and use it in GitHub Desktop.

Select an option

Save dynamite-bud/c315e122b6b226275bd3abc9862d27df to your computer and use it in GitHub Desktop.
export interface RadarData {
id: number;
rcs: number;
dist_lat: number;
dist_long: number;
dyn_prop: number;
vrel_lat: number;
vrel_long: number;
}
export interface NearestObject extends RadarData {
distanceToObject: number;
}
function absDistance(a1: number, a2: number, b1: number, b2: number) {
return Math.sqrt(Math.pow(a1 - b1, 2) + Math.pow(a2 - b2, 2));
}
export function getMinDistanceOfObject(
detectedObjects: Array<RadarData>
): NearestObject {
return detectedObjects.reduce(
(prev, curr) => {
const prevPointDistance = absDistance(
0,
0,
prev.dist_lat,
prev.dist_long
);
const currPointDistance = absDistance(
0,
0,
curr.dist_lat,
curr.dist_long
);
return prevPointDistance < currPointDistance
? {
...prev,
distanceToObject: prevPointDistance,
}
: {
...curr,
distanceToObject: currPointDistance,
};
},
{
id: -1,
rcs: -1,
dyn_prop: -1,
vrel_lat: -1,
vrel_long: -1,
dist_lat: Infinity,
dist_long: Infinity,
distanceToObject: Infinity,
}
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment