从变换矩阵中找到角度

从变换矩阵中找到角度,第1张

变换矩阵中找到角度

如果仅关于旋转,则可以使用给定的矩阵对向量(1,0)进行变换,并计算所得向量与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;    }}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5507318.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-13
下一篇 2022-12-13

发表评论

登录后才能评论

评论列表(0条)

保存