[英]How to re format GetVertices to return (x1,y1,0), (x2,y2,0), (x3,y3,0);?
So, I have added a constructor that makes it possible to create triangles with the expression new Triangle(x1, y1, x2, y2, x3, y3) where (x1,y1), (x2,y2), (x3,y3) are the three vertices of the triangle.因此,我添加了一个构造函数,它可以使用表达式 new Triangle(x1, y1, x2, y2, x3, y3) where (x1,y1), (x2,y2), (x3,y3) 创建三角形是三角形的三个顶点。 However, I need to make getVertices return (x1,y1,0), (x2,y2,0), (x3,y3,0);
但是,我需要让 getVertices 返回 (x1,y1,0), (x2,y2,0), (x3,y3,0); that is, the coordinates given to the constructor, with coordinate z set to 0.
也就是说,给构造函数的坐标,坐标 z 设置为 0。
below is code for a triangular prism, im trying to shorten/reformat so it works for a triangle, with a different formula.下面是三角棱镜的代码,我试图缩短/重新格式化,以便它适用于三角形,具有不同的公式。
private List<Point> getVertices() {
List<Point> result = new ArrayList<>();
result.add(new Point(x1 + x, y1 + y, z - height / 2.0));
result.add(new Point(x2 + x, y2 + y, z - height / 2.0));
result.add(new Point(x3 + x, y3 + y, z - height / 2.0));
result.add(new Point(x1 + x, y1 + y, z + height / 2.0));
result.add(new Point(x2 + x, y2 + y, z + height / 2.0));
result.add(new Point(x3 + x, y3 + y, z + height / 2.0));
return result;
any help would be greatly appreciated任何帮助将不胜感激
Point is a 2 dimensional coordinate of x and y
. Point 是
x and y
的二维坐标。 You would need to define your own class for a 3 dimensional x,y,z
.您需要为 3 维
x,y,z
定义自己的 class 。 Call the class Point3D
调用 class
Point3D
class Point3D {
int x, y, z;
public Point3D (int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getZ() {
return z;
}
}
That is not difficult but your List<Point3D>
may not be compatible with something that expects List<Point>
so you need to consider that.这并不难,但您的
List<Point3D>
可能与期望List<Point>
的东西不兼容,因此您需要考虑这一点。
You could also subclass the Point
class.您还可以对
Point
class 进行子类化。 But there are quite a few methods you would need to add if you wanted the same functionality for a 3D point.但是,如果您希望 3D 点具有相同的功能,则需要添加很多方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.