0

我之前问过一个关于克隆数组的问题,这样当您更改区域/缩放多边形等时......它不会更改数组的值(封装的基础知识)。在尝试将数组复制到另一个数组、使用克隆数组或使用数组列表后,我仍然遇到问题。任何人都对如何复制我的数组有任何建议,以便我可以对其进行更改,返回方法中的值,但仍然保持传递给构造函数的原始数组的完整性?Point 类定义了传入的值。我也会发布它。Polygon 创建了这些方法中的大部分。沮丧的...

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

}

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

 public class PolygonImpl implements Polygon {

   private double xSum=0;
    private double ySum=0;
    private Point[] points;
   private Point[] cloneList;
   private Point a;

  public PolygonImpl(Point[] points) {
 this.points = new Point[points.length];
 for (int i = 0; i < points.length; i++)
 this.points[i] = points[i];
}



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

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

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

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


}

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

    }
}




public Point[] getPoints() {
   //  Point[] newPoints = new Point[0];
    //for (int i = 0; i <newPoints.length; i++)
    //{
    // newPoints = points.get(i);
    //}
    return points;

}




public Rectangle getBoundingBox() {
    double minX= 99999;
    double maxX= -999999;
    double minY= 999999;
    double maxY= -999999;

    for (int i = 1; i<points.length; i ++){
        if (points[i].getX()< minX )
        {
            minX= points[i].getX();
        }
        if (points[i].getX() > maxX)
        {
            maxX= points[i].getX();
        }
        if (points[i].getY()< minY )
        {
            minY= points[i].getY();
        }
        if (points[i].getY() > maxY)
        {
            maxY= points[i].getY();
        }

        Point a = new Point(minX, minY);
        Point b = new Point(maxX, maxY);
        //need for next three cases
    }
    return null;
            //new Rectangle(Point a, Point b);
}




public Point getCentroid() {
    double x = 0;
    double y =0;
    double something = 1/(6*getArea());
    for (int i = 0; i< points.length-1; i++)
    {
        x+= (points[i].getX() + points[i+1].getX())*(points[i].getX()*points[i+1].getY()-points[i+1].getX()*points[i].getY());
        y+= (points[i].getY() + points[i+1].getY())*(points[i].getX()*points[i+1].getY()-points[i+1].getX()*points[i].getY());
    }
        x = x*something;
        y= y*something;

    Point a = new Point(x,y);
    return a;
}




public double getArea() {
    double area = 0;
for (int i = 0; i <points.length-1; i++){
    area += points[i].getX()*points[i+1].getY() - points[i+1].getX()*points[i].getY();
}
    area = area/2;
return area;
}




public void move(double dx, double dy) {
    //ArrayList<Point> points = new ArrayList<Point> ();

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

        }
}










}
}
4

1 回答 1

0

我不确定你想在哪里克隆你的阵列;但是我相信您尝试过类似的方法:

// This won't work because you keep the same reference to the old objects
public Point[] cloneArrayOfPoints(Point[] points) {
    Point[] result = new Point[points.length];
    for (int i = 0; i < points.length; i++){
        result.points[i] = points[i];
    }
    return result;
}

但是,你应该试试这个:

// This will work because you create new objects with same old values
public Point[] cloneArrayOfPoints(Point[] points) {
    Point[] result = new Point[points.length];
    for (int i = 0; i < points.length; i++){
        result.points[i] = new Point(points[i].getX(), points[i].getY());
    }
    return result;
}
于 2013-04-18T03:46:05.057 回答