Bookmark this: tool.io/geometry/distance/source-code
2D, 3D & Geographic Distance Calculator Source Code
Browse the implementation files for 2D, 3D & Geographic Distance Calculator.tool.ts typescript
import {
calculateDistance,
type DistanceInput,
type DistanceMode,
} from '@tool.io/tools/geometry/distance';
const isMissing = (input: unknown): boolean =>
input === undefined ||
input === null ||
(typeof input === 'string' && input.trim() === '');
const parseOptionalNumber = (input: unknown): number | undefined | null => {
if (isMissing(input)) return undefined;
if (typeof input !== 'string' && typeof input !== 'number') return null;
const value = Number(input);
return Number.isFinite(value) ? value : null;
};
export const distance = (
calculationInput: unknown,
point1AInput: unknown,
point1BInput: unknown,
point1ZInput: unknown,
point2AInput: unknown,
point2BInput: unknown,
point2ZInput: unknown,
): string => {
if (typeof calculationInput !== 'string') return '';
const values = [
point1AInput,
point1BInput,
point1ZInput,
point2AInput,
point2BInput,
point2ZInput,
].map(parseOptionalNumber);
if (values.includes(null)) return '';
const [point1A, point1B, point1Z, point2A, point2B, point2Z] =
values as Array<number | undefined>;
const input: DistanceInput = {
mode: calculationInput as DistanceMode,
point1A: point1A as number,
point1B: point1B as number,
...(point1Z !== undefined ? { point1Z } : {}),
point2A: point2A as number,
point2B: point2B as number,
...(point2Z !== undefined ? { point2Z } : {}),
};
const result = calculateDistance(input);
return result === null || !Number.isFinite(result) ? '' : String(result);
};
export const Tool = {
name: '2D, 3D & Geographic Distance Calculator',
slug: 'geometry/distance',
intro: 'Calculate distance between points in 2D, 3D, or geographic coordinates.',
tool: distance,
i: [
{
label: 'Distance Calculation',
placeholder: 'Select a calculation',
type: 'select',
options: [
{
value: '2d-euclidean',
text: '2D Euclidean distance',
selected: true,
},
{
value: '3d-euclidean',
text: '3D Euclidean distance',
},
{
value: 'geographic-haversine',
text: 'Geographic distance — Haversine',
},
{
value: 'geographic-lambert',
text: 'Geographic distance — Lambert',
},
],
},
{
example: '0',
help: 'Enter X₁ for Euclidean distance or latitude 1 for geographic distance.',
label: 'Point 1 — X or Latitude',
placeholder: 'X₁ or latitude 1',
type: 'number',
},
{
example: '0',
help: 'Enter Y₁ for Euclidean distance or longitude 1 for geographic distance.',
label: 'Point 1 — Y or Longitude',
placeholder: 'Y₁ or longitude 1',
type: 'number',
},
{
example: '0',
help: 'Required only for 3D Euclidean distance.',
label: 'Point 1 — Z',
placeholder: 'Z₁ for 3D only',
type: 'number',
},
{
example: '3',
help: 'Enter X₂ for Euclidean distance or latitude 2 for geographic distance.',
label: 'Point 2 — X or Latitude',
placeholder: 'X₂ or latitude 2',
type: 'number',
},
{
example: '4',
help: 'Enter Y₂ for Euclidean distance or longitude 2 for geographic distance.',
label: 'Point 2 — Y or Longitude',
placeholder: 'Y₂ or longitude 2',
type: 'number',
},
{
example: '0',
help: 'Required only for 3D Euclidean distance.',
label: 'Point 2 — Z',
placeholder: 'Z₂ for 3D only',
type: 'number',
},
],
o: [
{
example: '5',
help: 'Distance in coordinate units or kilometers for geographic calculations.',
label: 'Distance',
placeholder: 'Distance will appear here...',
type: 'number',
render: 'textarea',
},
],
equation: [
{
name: 'Euclidean distance',
eq: '$$d=\\sqrt{\\sum_i(q_i-p_i)^2}$$',
},
{
name: 'Haversine distance',
eq: '$$d=2R\\arctan2(\\sqrt{a},\\sqrt{1-a})$$',
},
{
name: 'Andoyer-Lambert distance',
eq: '$$d=a\\left(\\sigma-\\frac{f}{2}(X+Y)\\right)$$',
},
],
};
export default Tool;
Description
Browse the implementation files for 2D, 3D & Geographic Distance Calculator.