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

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

本文整理了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

/**
 * Calculates the distance between the two given points.
 *
 * @param x1
 * @param y1
 * @param x2
 * @param y2
 * @return
 */
protected float getDistance(float x1, float y1, float x2, float y2) {
  //return Math.abs(y1 - y2);
  //return Math.abs(x1 - x2);
  return (float) Math.hypot(x1 - x2, y1 - y2);
}

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

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

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

private static double distance(float x1, float y1, float x2, float y2) {
    double x = x2 - x1;
    double y = y2 - y1;
    return Math.hypot(x, y);
  }
}

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

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

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

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

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

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

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

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

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

/**
 * Returns a bitmap that lights up red subpixels at the bottom, green subpixels on the right, and
 * blue subpixels in bottom-right.
 */
Bitmap generateGradient() {
 int[][] pixels = new int[1080][1920];
 for (int y = 0; y < 1080; y++) {
  for (int x = 0; x < 1920; x++) {
   int r = (int) (y / 1080f * 255);
   int g = (int) (x / 1920f * 255);
   int b = (int) ((Math.hypot(x, y) / Math.hypot(1080, 1920)) * 255);
   pixels[y][x] = r << 16 | g << 8 | b;
  }
 }
 return new Bitmap(pixels);
}

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

@Override
 protected ExprEval eval(double x, double y)
 {
  return ExprEval.of(Math.hypot(x, y));
 }
}

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

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

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

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

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

@Override
@NonNull
public Path getPath(float startX, float startY, float endX, float endY) {
  double dx = endX - startX;
  double dy = endY - startY;
  float length = (float) Math.hypot(dx, dy);
  double angle = Math.atan2(dy, dx);
  mTempMatrix.setScale(length, length);
  mTempMatrix.postRotate((float) Math.toDegrees(angle));
  mTempMatrix.postTranslate(startX, startY);
  Path path = new Path();
  mPatternPath.transform(mTempMatrix, path);
  return path;
}

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

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
  private void prepareCircularReveal(View startView, View targetView) {
    int centerX = (startView.getLeft() + startView.getRight()) / 2;
    int centerY = (startView.getTop() + startView.getBottom()) / 2;
    float finalRadius = (float) Math.hypot((double) centerX, (double) centerY);
    mCircularReveal = ViewAnimationUtils.createCircularReveal(
        targetView, centerX, centerY, 0, finalRadius);
    mCircularReveal.addListener(new AnimatorListenerAdapter() {
      @Override
      public void onAnimationEnd(Animator animation) {
        mCircularReveal.removeListener(this);
      }
    });
  }
}

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

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

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

@Override @NonNull
protected Animator getAnimator(@NonNull ViewGroup container, View from, View to, boolean isPush, boolean toAddedToContainer) {
  final float radius = (float) Math.hypot(cx, cy);
  Animator animator = null;
  if (isPush && to != null) {
    animator = ViewAnimationUtils.createCircularReveal(to, cx, cy, 0, radius);
  } else if (!isPush && from != null) {
    animator = ViewAnimationUtils.createCircularReveal(from, cx, cy, radius, 0);
  }
  return animator;
}

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

private boolean createCircularReveal() {
  if (mColorBg == null) {
    return false;
  }
  int w = mColorBg.getWidth();
  int h = mColorBg.getHeight();
  if (ViewCompat.isAttachedToWindow(mColorBg) && w != 0 && h != 0) {
    ViewAnimationUtils.createCircularReveal(mColorBg, w / 2, h / 2, 0,
        (float) Math.hypot(w / 2, h / 2)).setDuration(300).start();
    return true;
  } else {
    return false;
  }
}

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

private RadialGradient getRadialGradient() {
 int gradientHash = getGradientHash();
 RadialGradient gradient = radialGradientCache.get(gradientHash);
 if (gradient != null) {
  return gradient;
 }
 PointF startPoint = startPointAnimation.getValue();
 PointF endPoint = endPointAnimation.getValue();
 GradientColor gradientColor = colorAnimation.getValue();
 int[] colors = gradientColor.getColors();
 float[] positions = gradientColor.getPositions();
 int x0 = (int) (boundsRect.left + boundsRect.width() / 2 + startPoint.x);
 int y0 = (int) (boundsRect.top + boundsRect.height() / 2 + startPoint.y);
 int x1 = (int) (boundsRect.left + boundsRect.width() / 2 + endPoint.x);
 int y1 = (int) (boundsRect.top + boundsRect.height() / 2 + endPoint.y);
 float r = (float) Math.hypot(x1 - x0, y1 - y0);
 gradient = new RadialGradient(x0, y0, r, colors, positions, Shader.TileMode.CLAMP);
 radialGradientCache.put(gradientHash, gradient);
 return gradient;
}

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

/**
 * Gets the distance between this point and another.
 *
 * @param other other point
 * @return the distance
 */
public int distanceTo(LocalPoint other)
{
  return (int) Math.hypot(getX() - other.getX(), getY() - other.getY());
}

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

/**
 * Gets the distance between this point and another.
 *
 * @param other other point
 * @return the distance
 */
public int distanceTo(Point other)
{
  return (int) Math.hypot(getX() - other.getX(), getY() - other.getY());
}

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

private RadialGradient getRadialGradient() {
 int gradientHash = getGradientHash();
 RadialGradient gradient = radialGradientCache.get(gradientHash);
 if (gradient != null) {
  return gradient;
 }
 PointF startPoint = startPointAnimation.getValue();
 PointF endPoint = endPointAnimation.getValue();
 GradientColor gradientColor = colorAnimation.getValue();
 int[] colors = gradientColor.getColors();
 float[] positions = gradientColor.getPositions();
 float x0 = startPoint.x;
 float y0 = startPoint.y;
 float x1 = endPoint.x;
 float y1 = endPoint.y;
 float r = (float) Math.hypot(x1 - x0, y1 - y0);
 if (r <= 0) {
  r = 0.001f;
 }
 gradient = new RadialGradient(x0, y0, r, colors, positions, Shader.TileMode.CLAMP);
 radialGradientCache.put(gradientHash, gradient);
 return gradient;
}

相关文章