1

**


谷歌用户记得使用 tx.setToRotation(Math.toRadians(angle)); 使用李的方法时。

**

我正在制作一个基于单元格的游戏,其中每个“有机体”都由多个 10x10 像素正方形(单元格)组成,这些单元格必须连接到“主单元格”,否则它们会被删除。主单元格包含一个表示其角度(0-360。)的int,当主单元格旋转时,它必须将所有连接的单元格与其对齐。下面的例子

单元格旋转 0 度 单元格旋转 120 度

我已经可以在这个角度上绘制它们,我只需要 Cell 的 getX() 和 getY() 函数根据主单元格的旋转返回修改后的 X/Y。

给定主单元格角度(int 角度),给定单元格的偏移量(int xMod,int yMod)和当前位置(int x,int y),您可以为 Cell 创建一个 getter 和 setter,它返回一个 X 和 Y 修改以适应主细胞(所有者)的轮换?

4

1 回答 1

1

Look into the AffineTransform class, which provides easy to use methods for various tranformations, including rotation. Then construct a Point2D using your (x, y) values and apply the AffineTransform to get a new Point2D that is rotated. This is nice because its usage is very similar to what you're currently using to rotate the graphics context.

AffineTransform tx = new AffineTransform();
tx.rotate(...);

Point2D point = new Point2D.Double(x, y);
Point2D rotated = new Point2D.Double();
tx.transform(point, rotated);

Bonus: you can use the transform for both the rendering and calculations! You can apply the AffineTransform on a Graphics2D object to cut down on similar/duplicate code.

Graphics2D g = ...
g.transform(tx);
于 2013-11-04T14:33:44.813 回答