gpt4 book ai didi

java - 如何获得旋转图像上的点?

转载 作者:行者123 更新时间:2023-12-01 10:55:54 26 4
gpt4 key购买 nike

对于使用 Graphics2D 绘制的图像,我需要访问左下角的坐标(或图像上的任何其他 3 个点)。问题是图像旋转了。这是 paintComponent() 方法:

protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;

//Makes a white background
g2d.setColor(Color.WHITE);
g2d.fill(new Rectangle2D.Float(0, 0, (float)Toolkit.getDefaultToolkit().getScreenSize().getWidth(), (float)Toolkit.getDefaultToolkit().getScreenSize().getHeight()));

//Draws Images & rotates
g2d.rotate(Math.toRadians(rocketAngle), r.getxPos() + (r.getImage().getWidth()/2), r.getyPos());
g2d.drawImage(r.getImage(), r.getxPos(), r.getyPos(), this);
}

(其中“r”是包含照片的对象)

通常,我会将图像高度添加到图像绘制点,但这不起作用,因为图像会旋转。有谁知道如何实现这一点吗?

最佳答案

因此,您的原始坐标系 (CS1) 中有以下点:

CS1:
Left bottom: lb(0, h)
Right bottom: rb(w, h)
Right top: rt(w, 0)
Left top: lt(0, 0)

您想要围绕点 v(w/2, 0) 旋转图片。为此,我们引入以点 v (CS2) 为中心的新坐标系:

CS2:
x' = x-w/2
y' = y

现在我们介绍一下CS3,它是CS2旋转角度phi:

CS3:
x'' = x'*cos phi - y'*sin phi = (x-w/2)*cos phi - y*sin phi
y'' = x'*sin phi + y'*cos phi = (x-w/2)*sin phi + y*cos phi

现在你想获取CS3中CS1的点lb、rb、rt、lt的坐标:

lb'' = (-(w/2)*cos phi - h*sin phi, -(w/2)*sin phi + h*cos phi)

我希望你明白了

关于java - 如何获得旋转图像上的点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33614275/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com