gpt4 book ai didi

java - 从变换矩阵中寻找角度

转载 作者:行者123 更新时间:2023-11-29 05:29:38 25 4
gpt4 key购买 nike

我有一个包含这样值的变换矩阵。

变换:分别为 xx、xy、yx、yy、tx 和 ty。

如何从上面的一组给定值中找到角度。

最佳答案

如果只是关于旋转,可以使用给定矩阵变换 vector (1,0),并计算结果 vector 与 x 轴之间的角度,如对原始问题的评论中所述

import java.awt.Point;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import java.util.Random;


public class ExtractRotation
{
public static void main(String[] args)
{
for (int i=0; i<=180; i++)
{
double angleRad = Math.toRadians(i);
AffineTransform at = createRandomTransform(angleRad);
double extractedAngleRad = extractAngle(at);
System.out.println(
"In: "+Math.toDegrees(angleRad)+ " " +
"Out "+Math.toDegrees(extractedAngleRad));
}
}

private static double extractAngle(double m[])
{
return extractAngle(new AffineTransform(m));
}
private static double extractAngle(AffineTransform at)
{
Point2D p0 = new Point();
Point2D p1 = new Point(1,0);
Point2D pp0 = at.transform(p0, null);
Point2D pp1 = at.transform(p1, null);
double dx = pp1.getX() - pp0.getX();
double dy = pp1.getY() - pp0.getY();
double angle = Math.atan2(dy, dx);
return angle;
}

private static Random random = new Random(0);
private static AffineTransform createRandomTransform(double angleRad)
{
AffineTransform at = new AffineTransform();
double scale = 1.0;
at.translate(randomDouble(), randomDouble());
scale = Math.abs(randomDouble());
at.scale(scale, scale);
at.rotate(angleRad);
at.translate(randomDouble(), randomDouble());
scale = Math.abs(randomDouble());
at.scale(scale, scale);
return at;
}

private static double randomDouble()
{
return -5.0 + random.nextDouble() * 10;
}


}

关于java - 从变换矩阵中寻找角度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21561909/

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