Untitled

mail@pastecode.io avatar
unknown
javascript
a year ago
1.3 kB
4
Indexable
Never
/**
 * sqrt(
 * 	abs(x2-x1)² +
 * 	abs(y2-y1)²
 * )
 */
const calcDist = (a, b) =>
	Math.sqrt(
		Math.pow(Math.abs(a.x - b.x), 2) + Math.pow(Math.abs(a.y - b.y), 2)
	);

console.log('PARTE 0:', calcDist({ x: 50, y: 30 }, { x: 20, y: 10 }));

const calcMultDist = (arr) => {
	let menor = Number.MAX_VALUE;

	for (let i = 0; i < arr.length; i++) {
		const item = arr[i];
		for (let j = i + 1; j < arr.length; j++) {
			const next = arr[j];
			const r = calcDist(item, next);

			if (r < menor) {
				menor = r;
			}
		}
	}

	return menor;
};

// parte 2
console.log(
	'PARTE 1:',
	calcMultDist([
		{ x: 20, y: 50 },
		{ x: 50, y: 80 },
		{ x: 70, y: 40 },
		{ x: 45, y: 10 },
		{ x: 30, y: 15 },
	])
);

// parte 3
/**
 * {40,175}, {70,175}, {70,215}, {130,215}, {130,125}, {80, 125},
{80,95}, {110, 95}, {110, 65}, {60,65}, {60,30}, {100,30},
{100,0}, {0,0}, {0,45}, {40, 45}
*/
console.log(
	'PARTE 2:',
	calcMultDist([
		{ x: 40, y: 175 },
		{ x: 70, y: 175 },
		{ x: 70, y: 215 },
		{ x: 130, y: 215 },
		{ x: 130, y: 125 },
		{ x: 80, y: 125 },
		{ x: 80, y: 95 },
		{ x: 110, y: 95 },
		{ x: 110, y: 65 },
		{ x: 60, y: 65 },
		{ x: 60, y: 30 },
		{ x: 100, y: 30 },
		{ x: 100, y: 0 },
		{ x: 0, y: 0 },
		{ x: 0, y: 45 },
		{ x: 40, y: 45 },
	])
);