Extract rotation Angle from a given AffineTransform Matrix. This function handle cases of mirror image flip about some axis. - Java 2D Graphics

Java examples for 2D Graphics:BufferedImage Rotate

Description

Extract rotation Angle from a given AffineTransform Matrix. This function handle cases of mirror image flip about some axis.

Demo Code


//package com.java2s;

import java.awt.geom.AffineTransform;

public class Main {
    /**//  w  w w.j a va 2 s  .  c om
     * Extract rotation Angle from a given AffineTransform Matrix.<br>
     * This function handle cases of mirror image flip about some axis. This changes right handed coordinate system into
     * a left handed system. Hence, returned angle has an opposite value.
     * 
     * @param transform
     * @return angle in the range of [ -PI ; PI ]
     */
    public static double extractAngleRad(AffineTransform transform) {
        double angleRad = 0.0;

        if (transform != null) {
            double sinTheta = transform.getShearY();
            double cosTheta = transform.getScaleX();

            angleRad = Math.atan2(sinTheta, cosTheta);

            if ((transform.getType() & AffineTransform.TYPE_FLIP) != 0) {
                angleRad *= -1.0;
            }
        }

        return angleRad;
    }
}

Related Tutorials