/
NikolayIvkin
/
TheAlgorithms
Обзор
Документация
Войти
/
NikolayIvkin
/
TheAlgorithms
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/main/java/com/thealgorithms/maths/DistanceFormula.java
47 строк
1 KB
Piotr Idzik
style: enable `HideUtilityClassConstructor` in checkstyle (#5147)
08 май 2024, 09:58
Не верифицирован
08 май 2024, 09:58
d3bb691
Код
Авторство
О чём код?
package com.thealgorithms.maths; public final class DistanceFormula { private DistanceFormula() { } public static double euclideanDistance(double x1, double y1, double x2, double y2) { double dX = Math.pow(x2 - x1, 2); double dY = Math.pow(y2 - x1, 2); return Math.sqrt(dX + dY); } public static double manhattanDistance(double x1, double y1, double x2, double y2) { return Math.abs(x1 - x2) + Math.abs(y1 - y2); } public static int hammingDistance(int[] b1, int[] b2) { int d = 0; if (b1.length != b2.length) { return -1; // error, both arrays must have the same length } for (int i = 0; i < b1.length; i++) { d += Math.abs(b1[i] - b2[i]); } return d; } public static double minkowskiDistance(double[] p1, double[] p2, int p) { double d = 0; double distance = 0.0; if (p1.length != p2.length) { return -1; // error, both arrays must have the same length } for (int i = 0; i < p1.length; i++) { distance += Math.abs(Math.pow(p1[i] - p2[i], p)); } distance = Math.pow(distance, (double) 1 / p); d = distance; return d; } }