Point2Dクラス、JavaSE

1.はじめに

JavaAPには、javafx.geometryパッケージに使いやすいPoint2Dクラスがあります。これは、2次元平面上のポイントを表すために使用されます。
x座標とy座標を使用して特定のポイントのPoint2Dオブジェクトを作成し、distanceメソッドを使用してポイントと別のポイントの間の距離を計算し、toStringOメソッドを使用してポイントの文字列表現を返すことができます。

2.コード

package com.zhuo.demo;


import javafx.geometry.Point2D;

import java.util.Scanner;

public class TestPoint2D {
    
    
    public static void main(String[] args) {
    
    
        Scanner input = new Scanner(System.in);
        System.out.print("请输入点1的坐标: ");
        double x1 = input.nextDouble();
        double y1 = input.nextDouble();
        System.out.print("请输入点2的坐标: ");
        double x2 = input.nextDouble();
        double y2 = input.nextDouble();
        Point2D p1 = new Point2D(x1, y1);
        Point2D p2 = new Point2D(x2, y2);
        System.out.println("点1的坐标为: " + p1.toString());
        System.out.println("点2的坐标为: " + p2.toString());
        System.out.println("点1到点2的距离为: " + p1.distance(p2));
    }
}

3.実行結果

请输入点1的坐标: 1.5 5.5
请输入点2的坐标: -5.3 -4.41的坐标为: Point2D [x = 1.5, y = 5.5]2的坐标为: Point2D [x = -5.3, y = -4.4]1到点2的距离为: 12.010412149464313

Process finished with exit code 0

おすすめ

転載: blog.csdn.net/weixin_42768634/article/details/113813699