Java Rectangle Rotate rotateRectangleOnEllipse(Rectangle2D rect, Point2D center, double angle, double a, double b)

Here you can find the source of rotateRectangleOnEllipse(Rectangle2D rect, Point2D center, double angle, double a, double b)

Description

rotate Rectangle On Ellipse

License

Open Source License

Parameter

Parameter Description
rect rectangle to rotate
center of rotation, relative to rect
angle in radians, 0 is right on X axis

Declaration

public static Rectangle2D rotateRectangleOnEllipse(Rectangle2D rect, Point2D center, double angle, double a,
        double b) 

Method Source Code


//package com.java2s;
/*/*  ww w. ja  v a 2s  . c  o  m*/
  MathUtils.java
    
  (c) 2012 Edward Swartz
    
  All rights reserved. This program and the accompanying materials
  are made available under the terms of the Eclipse Public License v1.0
  which accompanies this distribution, and is available at
  http://www.eclipse.org/legal/epl-v10.html
 */

import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

public class Main {
    /**
     * @param rect rectangle to rotate
     * @param center of rotation, relative to rect
     * @param angle in radians, 0 is right on X axis
     * @return
     */
    public static Rectangle2D rotateRectangleOnEllipse(Rectangle2D rect, Point2D center, double angle, double a,
            double b) {

        double ellipseScale = b > 0 ? a / b : 0;
        AffineTransform trans = AffineTransform.getRotateInstance(-angle, rect.getX() + center.getX(),
                (rect.getY() + center.getY()) * ellipseScale);
        trans.scale(1.0, ellipseScale > 0 ? 1.0 / ellipseScale : 0);

        Point2D upperLeft = trans.transform(new Point2D.Double(rect.getMinX(), rect.getMinY() * ellipseScale),
                null);
        Point2D upperRight = trans.transform(new Point2D.Double(rect.getMaxX(), rect.getMinY() * ellipseScale),
                null);
        Point2D lowerLeft = trans.transform(new Point2D.Double(rect.getMinX(), rect.getMaxY() * ellipseScale),
                null);
        Point2D lowerRight = trans.transform(new Point2D.Double(rect.getMaxX(), rect.getMaxY() * ellipseScale),
                null);

        double minX = Math.min(upperLeft.getX(),
                Math.min(upperRight.getX(), Math.min(lowerLeft.getX(), lowerRight.getX())));
        double minY = Math.min(upperLeft.getY(),
                Math.min(upperRight.getY(), Math.min(lowerLeft.getY(), lowerRight.getY())));
        double maxX = Math.max(upperLeft.getX(),
                Math.max(upperRight.getX(), Math.max(lowerLeft.getX(), lowerRight.getX())));
        double maxY = Math.max(upperLeft.getY(),
                Math.max(upperRight.getY(), Math.max(lowerLeft.getY(), lowerRight.getY())));

        return new Rectangle2D.Double(minX /*+ center.getX()*/, minY /*+ center.getY()*/, maxX - minX, maxY - minY);
    }
}

Related

  1. rotateRoundMiddlePoint(Rectangle2D rectangle)