java.lang.Math.hypot()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(7.4k)|赞(0)|评价(0)|浏览(302)

本文整理了Java中java.lang.Math.hypot()方法的一些代码示例,展示了Math.hypot()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Math.hypot()方法的具体详情如下:
包路径:java.lang.Math
类名称:Math
方法名:hypot

Math.hypot介绍

[英]Returns sqrt(x 2 + y 2 ). The final result is without medium underflow or overflow. The returned result is within 1 ulp (unit in the last place) of the real result. If one parameter remains constant, the result should be semi-monotonic.

Special cases:

  • hypot(+infinity, (anything including NaN)) = +infinity
  • hypot(-infinity, (anything including NaN)) = +infinity
  • hypot((anything including NaN), +infinity) = +infinity
  • hypot((anything including NaN), -infinity) = +infinity
  • hypot(NaN, NaN) = NaN
    [中]

代码示例

代码示例来源:origin: PhilJay/MPAndroidChart

  1. /**
  2. * Calculates the distance between the two given points.
  3. *
  4. * @param x1
  5. * @param y1
  6. * @param x2
  7. * @param y2
  8. * @return
  9. */
  10. protected float getDistance(float x1, float y1, float x2, float y2) {
  11. //return Math.abs(y1 - y2);
  12. //return Math.abs(x1 - x2);
  13. return (float) Math.hypot(x1 - x2, y1 - y2);
  14. }

代码示例来源:origin: kevin-wayne/algs4

  1. /**
  2. * Returns the absolute value of this complex number.
  3. * This quantity is also known as the <em>modulus</em> or <em>magnitude</em>.
  4. *
  5. * @return the absolute value of this complex number
  6. */
  7. public double abs() {
  8. return Math.hypot(re, im);
  9. }

代码示例来源:origin: andkulikov/Transitions-Everywhere

  1. private static double distance(float x1, float y1, float x2, float y2) {
  2. double x = x2 - x1;
  3. double y = y2 - y1;
  4. return Math.hypot(x, y);
  5. }
  6. }

代码示例来源:origin: ZieIony/Carbon

  1. public static float dist(float x1, float y1, float x2, float y2) {
  2. final float x = (x2 - x1);
  3. final float y = (y2 - y1);
  4. return (float) Math.hypot(x, y);
  5. }

代码示例来源:origin: apache/nifi

  1. /**
  2. * @return distance from a (0, 0) point.
  3. */
  4. public int distance() {
  5. // a simplified distance formula because the other coord is (0, 0)
  6. return (int) Math.hypot(x, y);
  7. }

代码示例来源:origin: ZieIony/Carbon

  1. public static float mag(float a, float b) {
  2. return (float) Math.hypot(a, b);
  3. }

代码示例来源:origin: airbnb/lottie-android

  1. public static float getScale(Matrix matrix) {
  2. points[0] = 0;
  3. points[1] = 0;
  4. // Use sqrt(2) so that the hypotenuse is of length 1.
  5. points[2] = SQRT_2;
  6. points[3] = SQRT_2;
  7. matrix.mapPoints(points);
  8. float dx = points[2] - points[0];
  9. float dy = points[3] - points[1];
  10. // TODO: figure out why the result needs to be divided by 2.
  11. return (float) Math.hypot(dx, dy) / 2f;
  12. }

代码示例来源:origin: square/okio

  1. /**
  2. * Returns a bitmap that lights up red subpixels at the bottom, green subpixels on the right, and
  3. * blue subpixels in bottom-right.
  4. */
  5. Bitmap generateGradient() {
  6. int[][] pixels = new int[1080][1920];
  7. for (int y = 0; y < 1080; y++) {
  8. for (int x = 0; x < 1920; x++) {
  9. int r = (int) (y / 1080f * 255);
  10. int g = (int) (x / 1920f * 255);
  11. int b = (int) ((Math.hypot(x, y) / Math.hypot(1080, 1920)) * 255);
  12. pixels[y][x] = r << 16 | g << 8 | b;
  13. }
  14. }
  15. return new Bitmap(pixels);
  16. }

代码示例来源:origin: apache/incubator-druid

  1. @Override
  2. protected ExprEval eval(double x, double y)
  3. {
  4. return ExprEval.of(Math.hypot(x, y));
  5. }
  6. }

代码示例来源:origin: andkulikov/Transitions-Everywhere

  1. private static double calculateMaxDistance(View sceneRoot, int focalX, int focalY) {
  2. int maxX = Math.max(focalX, sceneRoot.getWidth() - focalX);
  3. int maxY = Math.max(focalY, sceneRoot.getHeight() - focalY);
  4. return Math.hypot(maxX, maxY);
  5. }

代码示例来源:origin: robolectric/robolectric

  1. @Implementation
  2. protected float mapRadius(float radius) {
  3. float[] src = new float[] {radius, 0.f, 0.f, radius};
  4. mapVectors(src, 0, src, 0, 2);
  5. float l1 = (float) Math.hypot(src[0], src[1]);
  6. float l2 = (float) Math.hypot(src[2], src[3]);
  7. return (float) Math.sqrt(l1 * l2);
  8. }

代码示例来源:origin: andkulikov/Transitions-Everywhere

  1. @Override
  2. @NonNull
  3. public Path getPath(float startX, float startY, float endX, float endY) {
  4. double dx = endX - startX;
  5. double dy = endY - startY;
  6. float length = (float) Math.hypot(dx, dy);
  7. double angle = Math.atan2(dy, dx);
  8. mTempMatrix.setScale(length, length);
  9. mTempMatrix.postRotate((float) Math.toDegrees(angle));
  10. mTempMatrix.postTranslate(startX, startY);
  11. Path path = new Path();
  12. mPatternPath.transform(mTempMatrix, path);
  13. return path;
  14. }

代码示例来源:origin: hitherejoe/animate

  1. @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  2. private void prepareCircularReveal(View startView, View targetView) {
  3. int centerX = (startView.getLeft() + startView.getRight()) / 2;
  4. int centerY = (startView.getTop() + startView.getBottom()) / 2;
  5. float finalRadius = (float) Math.hypot((double) centerX, (double) centerY);
  6. mCircularReveal = ViewAnimationUtils.createCircularReveal(
  7. targetView, centerX, centerY, 0, finalRadius);
  8. mCircularReveal.addListener(new AnimatorListenerAdapter() {
  9. @Override
  10. public void onAnimationEnd(Animator animation) {
  11. mCircularReveal.removeListener(this);
  12. }
  13. });
  14. }
  15. }

代码示例来源:origin: nickbutcher/plaid

  1. /**
  2. * Calculate the duration for the transition depending upon how far the text has to move.
  3. */
  4. private long calculateDuration(@NonNull Rect startPosition, @NonNull Rect endPosition) {
  5. float distance = (float) Math.hypot(
  6. startPosition.exactCenterX() - endPosition.exactCenterX(),
  7. startPosition.exactCenterY() - endPosition.exactCenterY());
  8. long duration = (long) (1000 * (distance / velocity));
  9. return Math.max(minDuration, Math.min(maxDuration, duration));
  10. }

代码示例来源:origin: bluelinelabs/Conductor

  1. @Override @NonNull
  2. protected Animator getAnimator(@NonNull ViewGroup container, View from, View to, boolean isPush, boolean toAddedToContainer) {
  3. final float radius = (float) Math.hypot(cx, cy);
  4. Animator animator = null;
  5. if (isPush && to != null) {
  6. animator = ViewAnimationUtils.createCircularReveal(to, cx, cy, 0, radius);
  7. } else if (!isPush && from != null) {
  8. animator = ViewAnimationUtils.createCircularReveal(from, cx, cy, radius, 0);
  9. }
  10. return animator;
  11. }

代码示例来源:origin: seven332/EhViewer

  1. private boolean createCircularReveal() {
  2. if (mColorBg == null) {
  3. return false;
  4. }
  5. int w = mColorBg.getWidth();
  6. int h = mColorBg.getHeight();
  7. if (ViewCompat.isAttachedToWindow(mColorBg) && w != 0 && h != 0) {
  8. ViewAnimationUtils.createCircularReveal(mColorBg, w / 2, h / 2, 0,
  9. (float) Math.hypot(w / 2, h / 2)).setDuration(300).start();
  10. return true;
  11. } else {
  12. return false;
  13. }
  14. }

代码示例来源:origin: airbnb/lottie-android

  1. private RadialGradient getRadialGradient() {
  2. int gradientHash = getGradientHash();
  3. RadialGradient gradient = radialGradientCache.get(gradientHash);
  4. if (gradient != null) {
  5. return gradient;
  6. }
  7. PointF startPoint = startPointAnimation.getValue();
  8. PointF endPoint = endPointAnimation.getValue();
  9. GradientColor gradientColor = colorAnimation.getValue();
  10. int[] colors = gradientColor.getColors();
  11. float[] positions = gradientColor.getPositions();
  12. int x0 = (int) (boundsRect.left + boundsRect.width() / 2 + startPoint.x);
  13. int y0 = (int) (boundsRect.top + boundsRect.height() / 2 + startPoint.y);
  14. int x1 = (int) (boundsRect.left + boundsRect.width() / 2 + endPoint.x);
  15. int y1 = (int) (boundsRect.top + boundsRect.height() / 2 + endPoint.y);
  16. float r = (float) Math.hypot(x1 - x0, y1 - y0);
  17. gradient = new RadialGradient(x0, y0, r, colors, positions, Shader.TileMode.CLAMP);
  18. radialGradientCache.put(gradientHash, gradient);
  19. return gradient;
  20. }

代码示例来源:origin: runelite/runelite

  1. /**
  2. * Gets the distance between this point and another.
  3. *
  4. * @param other other point
  5. * @return the distance
  6. */
  7. public int distanceTo(LocalPoint other)
  8. {
  9. return (int) Math.hypot(getX() - other.getX(), getY() - other.getY());
  10. }

代码示例来源:origin: runelite/runelite

  1. /**
  2. * Gets the distance between this point and another.
  3. *
  4. * @param other other point
  5. * @return the distance
  6. */
  7. public int distanceTo(Point other)
  8. {
  9. return (int) Math.hypot(getX() - other.getX(), getY() - other.getY());
  10. }

代码示例来源:origin: airbnb/lottie-android

  1. private RadialGradient getRadialGradient() {
  2. int gradientHash = getGradientHash();
  3. RadialGradient gradient = radialGradientCache.get(gradientHash);
  4. if (gradient != null) {
  5. return gradient;
  6. }
  7. PointF startPoint = startPointAnimation.getValue();
  8. PointF endPoint = endPointAnimation.getValue();
  9. GradientColor gradientColor = colorAnimation.getValue();
  10. int[] colors = gradientColor.getColors();
  11. float[] positions = gradientColor.getPositions();
  12. float x0 = startPoint.x;
  13. float y0 = startPoint.y;
  14. float x1 = endPoint.x;
  15. float y1 = endPoint.y;
  16. float r = (float) Math.hypot(x1 - x0, y1 - y0);
  17. if (r <= 0) {
  18. r = 0.001f;
  19. }
  20. gradient = new RadialGradient(x0, y0, r, colors, positions, Shader.TileMode.CLAMP);
  21. radialGradientCache.put(gradientHash, gradient);
  22. return gradient;
  23. }

相关文章