该方法的目的是获取一个点 (c),然后将当前质心 (x,y) 移动到提供的新坐标。这是通过找到两者的差异然后将点转换到新坐标来完成的。但是,我不知道如何设置新的质心值,因为我将方法设置为 voids.... 有什么想法吗?对不起所有的代码,尽量少放。Translate 获取点 (x,y) 并添加给定的值 (dx,dy)。
private Point centroid;
Triangle(Point a, Point b, Point c) {
this.a = a;
this.b = b;
this.c = c;
triPoints = new Point[] { a, b, c };
}
public Point getCentroid() {
Point centroid;
double x = 0.0;
double y = 0.0;
x = (a.getX() + b.getX() + c.getX()) / 3;
y = (a.getY() + b.getY() + c.getY()) / 3;
centroid = new Point(x, y);
return centroid;
}
public void move(double dx, double dy) {
centroid = getCentroid();
System.out.println(centroid.getX() + " "+ centroid.getY());
centroid = centroid.translate(dx, dy);
Point newCentroid = new Point(centroid.getX(),centroid.getY());
System.out.println(newCentroid.getX() + " "+ newCentroid.getY());
if(getCentroid().equals(newCentroid)){
}else{
}
for (int index = 0; index < triPoints.length - 1; index++) {
triPoints[index].translate(dx, dy);
}
}
public void move(Point c) {
double firstx = 0.0;
double firsty = 0.0;
Point f = getCentroid();
System.out.println(f.getX()+ " "+ f.getY() + " "+ c.getX()+ " "+ c.getY());
if (f.getX() >= c.getX()) {
firstx = c.getX() - f.getX();}
else{
firstx = Math.abs(f.getX() - c.getX());
}
if (f.getY()>= c.getY()){
firsty = c.getY() - f.getY();
}
else {
firsty = Math.abs(f.getY() -c.getY());
}
move(firstx, firsty);
//System.out.println(centroid.getX() + " " + centroid.getY());
}