Java 旋转二维三角形积得到奇数结果

Java 旋转二维三角形积得到奇数结果,java,Java,我有一个点为[0,20],[15,-10],-15,-10]的三角形。现在我想把这个三角形旋转1度,2度等等 Im遵循中提供的公式: 我的java代码是: theta = 0; x1 = 0; y1 = 20; x2 = 15; y2 = -10; x3 = -15; y3 = -10; System.out.println("--------------------adjusted to 0, 0 --------------------------------------------");

我有一个点为[0,20],[15,-10],-15,-10]的三角形。现在我想把这个三角形旋转1度,2度等等

Im遵循中提供的公式:

我的java代码是:

theta = 0;
x1 = 0;
y1 = 20;
x2 = 15;
y2 = -10;
x3 = -15;
y3 = -10;

System.out.println("--------------------adjusted to 0, 0 --------------------------------------------");
System.out.println("[" + x1 + ", "+ y1 + "] " + "[" + x2 + ", " + y2 + "] [" + x3 + ", " + y3 + "]");

x1 = (cos(theta) * x1) - (sin(theta) * y1);
y1 = (sin(theta) * x1) + (cos(theta) * y1);
x2 = (cos(theta) * x2) - (sin(theta) * y2);
y2 = (sin(theta) * x2) + (cos(theta) * y2);
x3 = (cos(theta) * x3) - (sin(theta) * y3);
y3 = (sin(theta) * x3) + (cos(theta) * y3);

System.out.println("-------------------- rotated --------------------------------------------");
System.out.println("[" + x1 + ", "+ y1 + "] " + "[" + x2 + ", " + y2 + "] [" + x3 + ", " + y3 + "]");
对此的输出产生了无法解释的结果:

--------------------adjusted to 0, 0 --------------------------------------------
[0.0, 20.0] [15.0, -10.0] [-15.0, -10.0]
-------------------- rotated --------------------------------------------
[-16.829418, -3.355421] [16.519243, 8.49744] [0.31017494, -5.1420197]
当我绘制这个图时,三角形看起来完全倾斜


我是否完全误解了如何执行此操作?

您不能重用变量

在变换的第一行中更改了x1的值之后,在变换的第二行中使用x1

看看公式/函数!x'是另一个变量x


尝试使用第二组变量。最好创建一个包含x和y的类点。另一类包含三个点的三角形。然后创建一个三角形a和一个三角形B。

我使用了以下程序进行测试:

double theta = 1;
double x1 = 0;
double y1 = 20;
double x2 = 15;
double y2 = -10;
double x3 = -15;
double y3 = -10;

BufferedImage bim=new BufferedImage(500, 500, BufferedImage.TYPE_INT_RGB);
Graphics2D g=(Graphics2D)bim.getGraphics();
Polygon pl=new Polygon();               
pl.addPoint((int)x1, (int)y1); pl.addPoint((int)x2, (int)y2); pl.addPoint((int)x3, (int)y3);                
pl.translate(100, 100);             
g.setColor(Color.red);
g.fill(pl);

double newx1 = (Math.cos(theta) * x1) - (Math.sin(theta) * y1);
double  newy1 = (Math.sin(theta) * x1) + (Math.cos(theta) * y1);
double  newx2 = (Math.cos(theta) * x2) - (Math.sin(theta) * y2);
double  newy2 = (Math.sin(theta) * x2) + (Math.cos(theta) * y2);
double  newx3 = (Math.cos(theta) * x3) - (Math.sin(theta) * y3);
double  newy3 = (Math.sin(theta) * x3) + (Math.cos(theta) * y3);

Polygon pl2=new Polygon();              
pl2.addPoint((int)newx1, (int)newy1); pl2.addPoint((int)newx2, (int)newy2);   pl2.addPoint((int)newx3, (int)newy3);             
pl2.translate(200, 200);                
g.setColor(Color.yellow);
g.fill(pl2);

.... display image bim here
事实上,它产生了这种结果

当我将角度转换为弧度时,它会产生这个

double theta = Math.toRadians(1);
当我把角度调到10度

double theta = Math.toRadians(10);
-

您还需要确保在两个方程中使用前面的坐标,如上所述


最后,多边形需要居中,因为它似乎是当前多边形;否则,您需要找到中心,这是另一个问题。

对于我来说,θ=0很好,必须将每个变量声明为double,并使用import static java.lang.Math.*;但是还有另一个错误:在公式中使用y1的已更改值x1-与x2和x3相同。请输入正确的代码,给定的结果是θ=1,而不是=0!!!您可以考虑使用相应的类,如矩阵和向量类,以及旋转函数,用于可读性。就像三角形中的所有向量v,rotatev,degree和rotatev,degree:创建旋转矩阵rot,返回v*rot仍然错误:使用“旋转”x1来计算y1-尝试使用更大的角度,如60~1 rad,70甚至80度-对于更小的角度,如10度,错误并不明显,但可视化效果很好!
double theta = Math.toRadians(10);