定义Triangle类,要求实现IShape接口,定义三个顶点;定义无参和带参构造方法;定义set和get方法;moveTo方法实现将三角形的中心点位置移动到指定的位置(假设中心点位置的x、y分别为三个顶点坐标的均值);实现zoom方法,主要是对任意两个顶点的坐标值进行缩放;
时间: 2024-04-30 20:24:12 浏览: 153
```
public class Triangle implements IShape {
private Point vertex1;
private Point vertex2;
private Point vertex3;
public Triangle() {
this.vertex1 = new Point();
this.vertex2 = new Point();
this.vertex3 = new Point();
}
public Triangle(Point vertex1, Point vertex2, Point vertex3) {
this.vertex1 = vertex1;
this.vertex2 = vertex2;
this.vertex3 = vertex3;
}
public void setVertex1(Point vertex1) {
this.vertex1 = vertex1;
}
public void setVertex2(Point vertex2) {
this.vertex2 = vertex2;
}
public void setVertex3(Point vertex3) {
this.vertex3 = vertex3;
}
public Point getVertex1() {
return vertex1;
}
public Point getVertex2() {
return vertex2;
}
public Point getVertex3() {
return vertex3;
}
@Override
public void moveTo(int x, int y) {
int centerX = (vertex1.getX() + vertex2.getX() + vertex3.getX()) / 3;
int centerY = (vertex1.getY() + vertex2.getY() + vertex3.getY()) / 3;
int deltaX = x - centerX;
int deltaY = y - centerY;
vertex1.moveTo(vertex1.getX() + deltaX, vertex1.getY() + deltaY);
vertex2.moveTo(vertex2.getX() + deltaX, vertex2.getY() + deltaY);
vertex3.moveTo(vertex3.getX() + deltaX, vertex3.getY() + deltaY);
}
@Override
public void zoom(double ratio) {
vertex1.setX((int) (vertex1.getX() * ratio));
vertex1.setY((int) (vertex1.getY() * ratio));
vertex2.setX((int) (vertex2.getX() * ratio));
vertex2.setY((int) (vertex2.getY() * ratio));
vertex3.setX((int) (vertex3.getX() * ratio));
vertex3.setY((int) (vertex3.getY() * ratio));
}
}
```
阅读全文