【问题标题】:Java - Get new points of rotation in Graphics2DJava - 在 Graphics2D 中获取新的旋转点
【发布时间】:2017-09-25 09:04:05
【问题描述】:

我需要获取新的旋转点才能知道物体的真实位置和尺寸,例如:

Example 01.

我的应用程序的上下文是渲染 2D 图形,它的主要内容如下:

    protected void paintComponent(Graphics g) {
        ...

        g2d.rotate(Test.angle * Math.PI / 180, Test.hero, Test.heroY); 
        g2d.drawImage(Main.hero, Test.hero - 15, Test.heroY - 15, this);
        ...

图像绘制正确,但我无法获取新点。

【问题讨论】:

  • 你好北美的家伙,我发现了一个很酷的解决方案,然后我会在这里发布示例。还是谢谢你。
  • 但我仍然继续为其他人的答案开放,记住。
  • 我正在开发解决方案,目前结果非常好,但肯定需要改进。
  • 请不要关闭这个帖子,正如我已经说过的,我取得了令人满意的结果,但是我会发布一个有组织的回复,比通用算法更好,我会在空闲时间发布。

标签: java rotation 2d rendering collision


【解决方案1】:
double angle = ang * Math.PI / 180;
double sin = Math.sin(angle);
double cos = Math.cos(angle);       

double a = (centerX + ray) - x0;
double b = centerY - y0;
int xx = (int) (a * cos - b * -sin + x0);
int yy = (int) (a * -sin + b * cos + y0);

ang++;

它将在 +ray 中从这些原点围绕 centerX 和 centerY 旋转一个点,代码已经过测试并且可以从各个角度工作。

【讨论】:

    猜你喜欢
    • 2018-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-22
    • 1970-01-01
    • 2011-01-15
    • 2014-06-18
    • 2013-08-18
    相关资源
    最近更新 更多