第三章第二十九題(幾何:兩個圓)(Geometry: two circles)

jxxxh發表於2020-10-09

第三章第二十九題(幾何:兩個圓)(Geometry: two circles)

  • **3.29(幾何:兩個圓)編寫程式,提示使用者輸入兩個圓的中心座標和各自的半徑值,然後判斷第二個圓是在第一個圓內,還是和第一個圓重疊。
    下面是執行示例:
    Enter circle1’s center x-,y-coordinates,and radius: 0.5 5.1 13
    Enter circle2’s center x-,y-coordinates,and radius: 1 1.7 4.5
    circle2 is inside circle1
    Enter circle1’s center x-,y-coordinates,and radius: 3.4 5.7 5.5
    Enter circle2’s center x-,y-coordinates,and radius: 6.7 3.5 3
    circle2 overlaps circle1
    Enter circle1’s center x-,y-coordinates,and radius: 3.4 5.5 1
    Enter circle2’s center x-,y-coordinates,and radius: 5.5 7.2 1
    circle2 does not overlap circle1

    **3.29(Geometry: two circles) Write a program that prompts the user to enter the center coordinates and radii of two circles and determines whether the second circle is inside the first or overlaps with the first.
    Here are the sample runs:
    Enter circle1’s center x-,y-coordinates,and radius: 0.5 5.1 13
    Enter circle2’s center x-,y-coordinates,and radius: 1 1.7 4.5
    circle2 is inside circle1
    Enter circle1’s center x-,y-coordinates,and radius: 3.4 5.7 5.5
    Enter circle2’s center x-,y-coordinates,and radius: 6.7 3.5 3
    circle2 overlaps circle1
    Enter circle1’s center x-,y-coordinates,and radius: 3.4 5.5 1
    Enter circle2’s center x-,y-coordinates,and radius: 5.5 7.2 1
    circle2 does not overlap circle1

  • 參考程式碼:

package chapter03;

import java.util.Scanner;

public class Code_29 {
    public static void main(String[] args) {
        double xCoordinateCircle1,yCoordinateCircle1,radiusCircle1;
        double xCoordinateCircle2,yCoordinateCircle2,radiusCircle2;
        double distanceFromP1ToP2;

        System.out.print("Enter circle1's center x-,y-coordinates,and radius: ");
        Scanner input = new Scanner(System.in);
        xCoordinateCircle1 = input.nextDouble();
        yCoordinateCircle1 = input.nextDouble();
        radiusCircle1 = input.nextDouble();

        System.out.print("Enter circle2's center x-,y-coordinates,and radius: ");
        xCoordinateCircle2 = input.nextDouble();
        yCoordinateCircle2 = input.nextDouble();
        radiusCircle2 = input.nextDouble();

        distanceFromP1ToP2 = Math.pow((Math.pow(xCoordinateCircle1-xCoordinateCircle2, 2)
                +Math.pow(yCoordinateCircle1-yCoordinateCircle2, 2)), 0.5);

        if(radiusCircle1 + radiusCircle2 >= distanceFromP1ToP2)
        {
            if(radiusCircle1 - radiusCircle2 >= distanceFromP1ToP2)
                System.out.println("circle2 is inside circle1");
            else
                System.out.println("circle2 overlaps circle1");
        }
        else
            System.out.println("circle2 does not overlap circle1");

        input.close();
    }
}

  • 結果顯示:
Enter circle1's center x-,y-coordinates,and radius: 0.5 5.1 13
Enter circle2's center x-,y-coordinates,and radius: 1 1.7 4.5
circle2 is inside circle1

Process finished with exit code 0

相關文章