How to Apply transformation to Polygon object in Java

后端 未结 1 732
太阳男子
太阳男子 2021-01-26 02:08

i have made a transform and rendered a Polygon object with it(mesh is of type Polygon):

    at.setToTranslation(gameObject.position.x, gameObject.position.y);
           


        
相关标签:
1条回答
  • 2021-01-26 02:43

    If AffineTransform#createTransformedShape doesn't provide the desired result for Polygons (as it seems to be the case), you can split the Polygon into Points, transform each Point and combine into a new Polygon. Try:

    //Polygon mesh
    //AffineTransform at
    
    int[] x = mesh.xpoints;
    int[] y = mesh.ypoints;
    int[] rx = new int[x.length];
    int[] ry = new int[y.length];
    
    for(int i=0; i<mesh.npoints; i++){
      Point2d p = new Point2d.Double(x[i], y[i]);
      at.transform(p,p);
      rx[i]=p.x;
      ry[i]=p.y;
    }
    
    mesh = new Polygon(rx, ry, mesh.npoints)
    
    0 讨论(0)
提交回复
热议问题