1

所以对于这个项目,我希望输入一个包含多个点的数组。(没有具体数字)。我对如何处理方法和构造函数感到困惑,因为某些方法需要更改,例如获取 X 或 Y 值的平均值。我是否以正确的方式处理这个项目?我应该使用点列表的克隆,还是数组列表或什么...(请注意,我仅以 PolygonImpl 类的一部分为例,它们的功能都相似)类点包含:

public class Point {

private double x;
private double y;

public Point(double x, double y) {
    this.x = x;
    this.y = y;
}

public double getX() {
    return x;
}

public double getY() {
    return y;
}

public Point translate(double dx, double dy) {
    return new Point(x+dx, y+dy);
}

public double distanceTo(Point p) {
    return Math.sqrt((p.x - x)*(p.x -x) + (p.y-y)*(p.y-y));
}

}

public class PolygonImpl implements Polygon {

private double xSum=0;
private double ySum=0;
private ArrayList<Point> points;
private Point[] pList;
private Point a;

PolygonImpl(Point[] pList) {

    this.pList = pList.clone();
    points = new ArrayList<Point>();

    for (int index = 0; index < pList.length; index++) {
           points.add(pList[index]);
       xSum += pList[index].getX();
       ySum += pList[index].getY();


    }
}



public Point getVertexAverage() {
     double xSum = 0;
     ArrayList<Point> vlist = new ArrayList<Point> ();
     double ySum = 0;
    for (int index = 0; index < vlist.size(); index++) {
         xSum = xSum + vlist.get(index).getX();
         ySum = ySum + vlist.get(index).getY();
        }

    return new Point(xSum/getNumSides(), ySum/getNumSides());
}

public int getNumSides() {
    return pList.length;
}

public void move(Point c) {
    Point newCentroid = new Point(a.getX()+ c.getX(), a.getY() +c.getY());


}

public void scale(double factor) {

 ArrayList<Point> points = new ArrayList<Point> ();

    for (int index = 0; index < pList.length; index++) {
        { double x = pList[index].getX() *factor;
          double y = pList[index].getY() * factor;
          Point a = new Point(x,y);
            points.add(index,a);

        }
    }
     }
4

2 回答 2

0

在这种情况下,我认为您不需要额外的 List 对象;它们是多余的。这是仅使用 pList 数组的代码。

public class PolygonImpl implements Polygon {

private double xSum=0;
private double ySum=0;
private Point[] pList;
private Point a;

    PolygonImpl(Point[] pList) {

        this.pList = pList.clone();

        for (int index = 0; index < pList.length; index++) {
           xSum += pList[index].getX();
           ySum += pList[index].getY();
        }
    }



    public Point getVertexAverage() {
         double xSum = 0;
         double ySum = 0;
        for (int index = 0; index < pList.length; index++) {
             xSum = xSum + pList[index].getX();
             ySum = ySum + pList[index].getY();
            }

        return new Point(xSum/getNumSides(), ySum/getNumSides());
    }

    public int getNumSides() {
        return pList.length;
    }

    public void move(Point c) {
        Point newCentroid = new Point(a.getX()+ c.getX(), a.getY() +c.getY());


    }

    public void scale(double factor) {
        for (int index = 0; index < pList.length; index++)
        {
            double x = pList[index].getX() *factor;
            double y = pList[index].getY() * factor;
            Point a = new Point(x,y);
            pList[index] = a;

        }
    }
于 2013-04-17T17:12:47.883 回答
0

这里至少有三点需要指出。

首先是“克隆”和“复制”的区别。.clone 是浅拷贝;这意味着如果你改变了克隆,你也改变了原来的。

第二个是关于你对收藏的使用。由于多边形是点的集合,因此数组或 ArrayList 都是合适的,但是使用麻烦没有意义,或者,如果有,请仔细考虑它是否是一个好点,然后在内联文档为什么它很重要,否则它会以在你指的是另一个时使用一个的形式回来咬你。

三是范围。您的多边形类具有实例变量(xSum 和 ySum),它们被 getVertexAverage 中同名的变量所遮挡。它们在 getVertexAverage 中的使用方式本身是合适的;实例变量仅在您打算缓存总和时才有用,这变得更加可疑,因为更改点的每个操作都会使实例值无效。

实例值用于存储有关对象的数据(在这种情况下,点是合理的);实例方法用于在给定状态(在本例中为平均值)对数据进行操作。

记住这一点,您现在可以理解 move 方法是如何未完成的:)

于 2013-04-17T17:29:34.323 回答