creates a square beginning at x,y and tilted in the direction of the angle. - Java 2D Graphics

Java examples for 2D Graphics:Rectangle

Description

creates a square beginning at x,y and tilted in the direction of the angle.

Demo Code

/* Mesquite source code.  Copyright 1997 and onward, W. Maddison and D. Maddison. 


Disclaimer:  The Mesquite source code is lengthy and we are few.  There are no doubt inefficiencies and goofs in this code. 
The commenting leaves much to be desired. Please approach this source code with the spirit of helping out.
Perhaps with your help we can be more than a few, and make Mesquite better.

Mesquite is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY.
Mesquite's web site is http://mesquiteproject.org

This source code and its compiled class files are free and modifiable under the terms of 
GNU Lesser General Public License.  (http://www.gnu.org/copyleft/lesser.html)
 *//*  w w  w.  j a  v a2 s.c  om*/
//package com.java2s;
import java.awt.*;

public class Main {
    /** creates a square beginning at x,y and tilted in the direction of the angle. */
    public static Polygon createAngledSquare(int x, int y, double angle,
            int length) {
        int adj = -(int) (Math.cos(angle) * length);
        int opp = -(int) (Math.sin(angle) * length);
        x -= opp / 2;
        y -= adj / 2;

        Polygon poly = new Polygon();
        poly.npoints = 0;
        poly.addPoint(x, y);
        poly.addPoint(x + opp, y + adj);
        poly.addPoint(x + opp - adj, y + adj + opp);
        poly.addPoint(x - adj, y + opp);
        poly.addPoint(x, y);
        poly.npoints = 5;
        return poly;

    }
}

Related Tutorials