如何在Java中对多边形对象应用变换 [英] How to Apply transformation to Polygon object in Java

查看:210
本文介绍了如何在Java中对多边形对象应用变换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经做了一个变换和渲染一个Polygon对象(mesh是类型Polygon):

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);
    at.rotate(Math.toRadians(rotation));
    at.scale(scale, scale);
    g2d.setTransform(at);
    g2d.fillPolygon(mesh);



现在我想返回我渲染的确切的网格,以便我可以做它的冲突检查。只有问题是,如果我返回mesh,它返回未转换的网格。所以我试图设置变换到Polygon对象(mesh)像这样:

now i want to return the exact mesh i rendered so that i can do collision checks on it. only problem is that if i return mesh it returns the un-transformed mesh. so i tried setting the transform to the Polygon object (mesh) like so:

    mesh = (Polygon)at.createTransformedShape(mesh);

但不幸的是at.createTransformedShape()返回一个只能被转换为Path2D.Double的Shape。所以如果有人知道如何将Path2D.Double转换为Polygon或者知道另一种方法来设置转换到网格,请帮助。

but unfortunately at.createTransformedShape() returns a Shape that can only be casted to Path2D.Double. so if anyone knows how to convert Path2D.Double to Polygon or knows another way to set the transformations to the mesh please please help.

推荐答案

p>如果 AffineTransform#createTransformedShape 没有为多边形提供所需的结果(因为它似乎是这样),您可以将多边形拆分为,将每个并组合成一个新的多边形。尝试:

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)

这篇关于如何在Java中对多边形对象应用变换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆