Use Math.hypot instead of Math.sqrt(a^2 + b^2)

This commit is contained in:
Gold856
2025-03-30 01:30:43 -04:00
committed by Matt Morley
parent 38ee450117
commit e514071094
5 changed files with 7 additions and 11 deletions

View File

@@ -52,7 +52,7 @@ public class UICameraCalibrationCoefficients extends CameraCalibrationCoefficien
.map(
it2 ->
it2.reprojectionErrors.stream()
.mapToDouble(it -> Math.sqrt(it.x * it.x + it.y * it.y))
.mapToDouble(it -> Math.hypot(it.x, it.y))
.average()
.orElse(0))
.toList();

View File

@@ -85,9 +85,7 @@ public class CornerDetectionPipe
* @return The straight line distance between them.
*/
private static double distanceBetween(Point a, Point b) {
double xDelta = a.x - b.x;
double yDelta = a.y - b.y;
return Math.sqrt(xDelta * xDelta + yDelta * yDelta);
return Math.hypot(a.x - b.x, a.y - b.y);
}
/**

View File

@@ -170,9 +170,7 @@ public class FindBoardCornersPipe
// +1 idx Neighbor distance
double[] startPoint = inPoints.get(pointIdx, 0);
double[] endPoint = inPoints.get(pointIdx + 1, 0);
double deltaX = startPoint[0] - endPoint[0];
double deltaY = startPoint[1] - endPoint[1];
double distToNext = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
double distToNext = Math.hypot(startPoint[0] - endPoint[0], startPoint[1] - endPoint[1]);
minSpacing = Math.min(distToNext, minSpacing);
}

View File

@@ -51,9 +51,9 @@ public class SortContoursPipe
}
private double calcSquareCenterDistance(PotentialTarget tgt) {
return Math.sqrt(
Math.pow(params.frameStaticProperties().centerX - tgt.getMinAreaRect().center.x, 2)
+ Math.pow(params.frameStaticProperties().centerY - tgt.getMinAreaRect().center.y, 2));
return Math.hypot(
params.frameStaticProperties().centerX - tgt.getMinAreaRect().center.x,
params.frameStaticProperties().centerY - tgt.getMinAreaRect().center.y);
}
public static record SortContoursParams(

View File

@@ -171,7 +171,7 @@ public class SimCameraProperties {
DriverStation.reportError(
"Requested invalid FOV! Clamping between (1, 179) degrees...", false);
}
double resDiag = Math.sqrt(resWidth * resWidth + resHeight * resHeight);
double resDiag = Math.hypot(resWidth, resHeight);
double diagRatio = Math.tan(fovDiag.getRadians() / 2);
var fovWidth = new Rotation2d(Math.atan(diagRatio * (resWidth / resDiag)) * 2);
var fovHeight = new Rotation2d(Math.atan(diagRatio * (resHeight / resDiag)) * 2);