簡體   English   中英

如何僅使用兩個變量找到兩點之間的距離,然后存儲所有點並獲得形狀?

[英]How can I find the distance between two points using only two variables and after that store all points and obtain the shape?

我嘗試聲明兩個變量 x 和 y,然后為它們創建構造函數並使用 setter 創建 getter。 因此,為此我使用了 class 距離,而為了獲得形狀,我需要另一個 class。

package com.company;
import java.lang.Math;

public class Point {
    //fields
    private int x;
    private int y;

    //constructor
    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    //method
        //getters
    public int getX() {
        return x;
    }
    public int getY() {
        return y;
    }
        //setters
    public void setX(int x) {
        this.x = x;
    }
    public void setY(int y) {
        this.y = y;
    }

    public int getPoint(){

    }

    //function distance
    public void distance() {
        //**here I need somehow use only two variables instead of four**
        double res = Math.sqrt((Math.pow(getX1(), 2) - Math.pow(getX2(), 2))
                + (Math.pow(getY1(), 2) - Math.pow(getY2(), 2)));
        System.out.println(res);
    }
}

創建一個接受 Point 類型的 object 的 function。 function 返回原點到經過點的距離

public void distance(Point po) {
    //**here I need somehow use only two variables instead of four**
    double res = Math.sqrt(
                     Math.pow(getX() - po.getX(), 2) +
                     Math.pow(getY() - po.getY(), 2)
    );
    System.out.println(res);
}

你的 function 計算距離也是錯誤的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM