Package engine.utility

Source Code of engine.utility.Transform

package engine.utility;

import engine.geometry.Circle;
import engine.geometry.Line;
import engine.geometry.Point;
import engine.geometry.Polygon;
import engine.geometry.Rectangle;
import engine.geometry.Shape;
import engine.geometry.Vector;

public final class Transform {
    public static void translate(final Shape shape, final Vector vector) {
        translate(shape, vector.getX(), vector.getY());
    }

    public static void translate(final Shape shape, final double x, final double y) {
        if (shape instanceof Point) {
            Point point = (Point) shape;
            point.setX(point.getX() + x);
            point.setY(point.getY() + y);
        } else if (shape instanceof Line) {
            Line line = (Line) shape;
            line.setX1(line.getX1() + x);
            line.setY1(line.getY1() + y);
            line.setX2(line.getX2() + x);
            line.setY2(line.getY2() + y);
        } else if (shape instanceof Rectangle) {
            Rectangle rectangle = (Rectangle) shape;
            rectangle.setX(rectangle.getX() + x);
            rectangle.setY(rectangle.getY() + y);
        } else if (shape instanceof Circle) {
            Circle circle = (Circle) shape;
            circle.setX(circle.getX() + x);
            circle.setY(circle.getY() + y);
        } else if (shape instanceof Polygon) {
            Polygon polygon = (Polygon) shape;
            final int size = polygon.getSize();
            for (int i = 0; i < size; i++) {
                polygon.setVertex(i, polygon.getX(i) + x, polygon.getY(i) + y);
            }
        } else {
            throw new UnsupportedOperationException();
        }
    }

    public static void scale(final Shape shape, final Vector vector) {
        scale(shape, vector.getX(), vector.getY());
    }

    public static void scale(final Shape shape, final double x, final double y) {
        if (shape instanceof Point) {
            Point point = (Point) shape;
            point.setX(point.getX() * x);
            point.setY(point.getY() * y);
        } else if (shape instanceof Line) {
            Line line = (Line) shape;
            line.setX1(line.getX1() * x);
            line.setY1(line.getY1() * y);
            line.setX2(line.getX2() * x);
            line.setY2(line.getY2() * y);
        } else if (shape instanceof Rectangle) {
            Rectangle rectangle = (Rectangle) shape;
            rectangle.setX(rectangle.getX() * x);
            rectangle.setY(rectangle.getY() * y);
            rectangle.setWidth(rectangle.getWidth() * x);
            rectangle.setHeight(rectangle.getHeight() * y);
        } else if (shape instanceof Polygon) {
            Polygon polygon = (Polygon) shape;
            final int size = polygon.getSize();
            for (int i = 0; i < size; i++) {
                polygon.setVertex(i, polygon.getX(i) * x, polygon.getY(i) * y);
            }
        } else {
            throw new UnsupportedOperationException();
        }
    }

    public static void shear(final Shape shape, final Vector vector) {
        shear(shape, vector.getX(), vector.getY());
    }

    public static void shear(final Shape shape, final double x, final double y) {
        if (shape instanceof Point) {
            Point point = (Point) shape;
            point.set(point.getX() + x * point.getY(), point.getY() + y * point.getX());
        } else if (shape instanceof Line) {
            Line line = (Line) shape;
            line.setStart(line.getX1() + x * line.getY1(), line.getY1() + y * line.getX1());
            line.setEnd(line.getX2() + x * line.getY2(), line.getY2() + y * line.getX2());
        } else if (shape instanceof Polygon) {
            Polygon polygon = (Polygon) shape;
            final int size = polygon.getSize();
            for (int i = 0; i < size; i++) {
                polygon.setVertex(i, polygon.getX(i) + x * polygon.getY(i), polygon.getY(i) + y * polygon.getX(i));
            }
        } else {
            throw new UnsupportedOperationException();
        }
    }

    public static void rotate(final Shape shape, final double angle) {
        throw new UnsupportedOperationException();
    }

    private Transform() {
        throw new UnsupportedOperationException();
    }
}
TOP

Related Classes of engine.utility.Transform

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.