32

2 つの円が重なっているかどうかを判断できるようにするプログラムのほとんどを作成しました。

1 つの問題を除けば、私のプログラムには何の問題もありません。プログラムは、2 つの中心点間の距離について私が書いたコードを受け入れません。後で距離の値に応じて何が起こるかをユーザーに伝える if/else ロジックを理解することはできますが、何が問題なのか知りたいです。私がコーディングしているプログラムである Eclipse は、距離を配列に解決する必要があると言っていますが、それは int であることは既に説明しました。

これが私のコードです:

package circles;
import java.util.Scanner;

public class MathCircles {    
    // variable for the distance between the circles' centers
    public static int distance;

    // variable for the lengths of the radii combined
    public static int radii;

    public static void main(String[] args) {
        // Get the x-value of the center of circle one
        System.out.println("What is the x-coordinate for the center of circle one?");
        Scanner keyboard = new Scanner(System.in);
        int x1 = keyboard.nextInt();

        //Get the y-value of the center of circle one
        System.out.println("What is the y-coordinate for the center of circle one?");
        Scanner keyboard1 = new Scanner(System.in);
        int y1 = keyboard1.nextInt();

        //Get the radius length of circle one.
        System.out.println("How long is circle one's radius?");
        Scanner keyboard2 = new Scanner(System.in);
        int r1 = keyboard2.nextInt();

        // Get the x-value of the center of circle two.
        System.out.println("What is the x-coordinate for the center of circle two?");
        Scanner keyboard3 = new Scanner(System.in);
        int x2 = keyboard3.nextInt();

        //Get the y-value of the center of circle two.
        System.out.println("What is the y-coordinate for the center of circle two?");
        Scanner keyboard4 = new Scanner(System.in);
        int y2 = keyboard4.nextInt();

        //Get the radius length of circle two.
        System.out.println("How long is circle two's radius?");
        Scanner keyboard5 = new Scanner(System.in);
        int r2 = keyboard5.nextInt();

        /*
         * OK, so now I have the location of the two circles' centers,
         * as well as the lengths of their radii.
         * The circles are intersecting IF THE DISTANCE BETWEEN THE TWO CENTERS
         * IS EQUAL TO OR LESS THAN THE COMBINED LENGTHS OF THE RADII.
         * Now I need to get some math done.
         */

        //calculate the combined lengths of the radii

        radii = r1 + r2;

        //calculate the distance
        distance = Math.sqrt((x1-x2)(x1-x2) + (y1-y2)(y1-y2));

    }    
}
4

7 に答える 7

74

@trashgod のコメントに基づいて、これは距離を計算する最も簡単な方法です。

double distance = Math.hypot(x1-x2, y1-y2);

ドキュメントからMath.hypot:

戻り値:sqrt(x²+ y²) 中間オーバーフローまたはアンダーフローなし。

于 2015-08-17T09:55:16.357 に答える
36

表記法とは異なりmaths-on-paper、ほとんどのプログラミング言語 (Java を含む) では、*乗算を行うために符号が必要です。したがって、距離の計算は次のようになります。

distance = Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));

または、次のようにします。

distance = Math.sqrt(Math.pow((x1-x2), 2) + Math.pow((y1-y2), 2));
于 2013-01-21T00:45:12.323 に答える
9

これは古いかもしれませんが、これが最良の答えです:

    float dist = (float) Math.sqrt(
            Math.pow(x1 - x2, 2) +
            Math.pow(y1 - y2, 2) );
于 2014-01-15T03:44:28.913 に答える
4

Point2D Java API クラスを使用することもできます。

public static double distance(double x1, double y1, double x2, double y2)

例:

double distance = Point2D.distance(3.0, 4.0, 5.0, 6.0);
System.out.println("The distance between the points is " + distance);
于 2015-09-08T19:48:46.240 に答える
4

乗算したいことを Java に明示的に伝える必要があります。

(x1-x2) * (x1-x2) + (y1-y2) * (y1-y2)

書かれた方程式とは異なり、コンパイラはこれがあなたがしたいことだとは知りません。

于 2013-01-21T00:45:13.627 に答える
3

Math.sqrt は double を返すため、int にもキャストする必要があります。

distance = (int)Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));

于 2013-01-21T00:48:39.577 に答える