Modify the object by applying the given transform. - Java java.lang

Java examples for java.lang:Math Geometry

Description

Modify the object by applying the given transform.

Demo Code

/**//from w  ww .j  a v a  2  s .c  o m
 * Copyright 1998-2007, CHISEL Group, University of Victoria, Victoria, BC, Canada.
 * All rights reserved.
 */
//package com.java2s;
import java.awt.geom.AffineTransform;

import java.awt.geom.Rectangle2D;

public class Main {
    /**
     * Modify the object by applying the given transform.
     * @param tf the AffineTransform to apply.
     */
    public static Rectangle2D.Double transform(Rectangle2D.Double rect,
            AffineTransform tf) {
        double x = rect.x;
        double y = rect.y;
        double width = rect.width;
        double height = rect.height;

        // First, transform all 4 corners of the rectangle
        double[] pts = new double[8];
        pts[0] = x; // top left corner
        pts[1] = y;
        pts[2] = x + width; // top right corner
        pts[3] = y;
        pts[4] = x + width; // bottom right corner
        pts[5] = y + height;
        pts[6] = x; // bottom left corner
        pts[7] = y + height;
        tf.transform(pts, 0, pts, 0, 4);

        // Then, find the bounds of those 4 transformed points.
        double minX = pts[0];
        double minY = pts[1];
        double maxX = pts[0];
        double maxY = pts[1];
        int i;
        for (i = 1; i < 4; i++) {
            if (pts[2 * i] < minX) {
                minX = pts[2 * i];
            }
            if (pts[2 * i + 1] < minY) {
                minY = pts[2 * i + 1];
            }
            if (pts[2 * i] > maxX) {
                maxX = pts[2 * i];
            }
            if (pts[2 * i + 1] > maxY) {
                maxY = pts[2 * i + 1];
            }
        }

        Rectangle2D.Double transformedRect = new Rectangle2D.Double(minX,
                minY, maxX - minX, maxY - minY);
        return transformedRect;
    }
}

Related Tutorials