简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM