Linearly interpolates between two points. - Java java.lang

Java examples for java.lang:Math Geometry Line

Description

Linearly interpolates between two points.

Demo Code


//package com.java2s;

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

public class Main {
    /**//from   w w w . j  a  v a 2s .  c  o m
     * Linearly interpolates between two points.
     * 
     * @param from The starting point.
     * @param to The ending point.
     * @param t The interpolation progress starting at 0 and going to 1.
     * @return The interpolated point.
     */
    public static Point2D interpolate(final Point2D from, final Point2D to,
            final double t) {
        return new Point2D.Double(from.getX() * (1 - t) + to.getX() * t,
                from.getY() * (1 - t) + to.getY() * t);
    }

    /**
     * Linearly interpolates between two rectangles.
     * 
     * @param from The starting rectangle.
     * @param to The ending rectangle.
     * @param t The interpolation progress starting at 0 and going to 1.
     * @return The interpolated rectangle.
     */
    public static Rectangle2D interpolate(final Rectangle2D from,
            final Rectangle2D to, final double t) {
        final double f = 1 - t;
        return new Rectangle2D.Double(from.getX() * f + to.getX() * t,
                from.getY() * f + to.getY() * t, from.getWidth() * f
                        + to.getWidth() * t, from.getHeight() * f
                        + to.getHeight() * t);
    }
}

Related Tutorials