开发者社区> 问答> 正文

如何仅使用两个变量找到两个点之间的距离,然后存储所有点并获得形状?

我试图声明两个变量x和y,然后为它们和带有setter的getter创建构造函数。因此,为此,我使用了距离类,而为了获得形状,我需要另一个类。

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);
}

}


问题来源:stackoverflow

展开
收起
七天一失眠 2020-04-11 15:48:28 1474 0
1 条回答
写回答
取消 提交回答
  • 做一个优秀的阿里云志愿者

    创建一个接受Point类型对象的函数。该函数返回原始点和通过点之间的距离

    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); }

    另外,您计算距离的函数是错误的。


    答案来源:stackoverflow

    2020-04-11 15:48:50
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
重新定义计算的边界 立即下载
用计算和数据去改变整个世界 立即下载
图计算及其应用 立即下载