Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
340 views
in Technique[技术] by (71.8m points)

java - 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. However, I need to make getVertices return (x1,y1,0), (x2,y2,0), (x3,y3,0); that is, the coordinates given to the constructor, with coordinate z set to 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

question from:https://stackoverflow.com/questions/65863284/how-to-re-format-getvertices-to-return-x1-y1-0-x2-y2-0-x3-y3-0

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Point is a 2 dimensional coordinate of x and y. You would need to define your own class for a 3 dimensional x,y,z. Call the 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.

You could also subclass the Point class. But there are quite a few methods you would need to add if you wanted the same functionality for a 3D point.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...