Calculates the vector that lies between the given vectors from - a and from b with the given length. - Java java.lang

Java examples for java.lang:Math Vector

Description

Calculates the vector that lies between the given vectors from - a and from b with the given length.

Demo Code


import java.awt.geom.Line2D;
import java.awt.geom.Point2D;

public class Main{
    /**// www  . j  av  a 2  s  .c om
     * Calculates the vector that lies between the given vectors {@code from - a}
     * and {@code from b} with the given length.
     * 
     * @param from The starting point of both vectors.
     * @param a The first end point.
     * @param b The second end point.
     * @param len The length of the result.
     * @return The vector in the middle of the two.
     */
    public static Point2D middleVec(final Point2D from, final Point2D a,
            final Point2D b, final double len) {
        return addVec(
                mulVec(normVec(middleVec(subVec(a, from), subVec(b, from))),
                        len), from);
    }
    /**
     * Calculates the vector that lies between the given vectors {@code from - a}
     * and {@code from b}.
     * 
     * @param from The starting point of both vectors.
     * @param a The first end point.
     * @param b The second end point.
     * @return The vector in the middle of the two.
     */
    public static Point2D middleVec(final Point2D from, final Point2D a,
            final Point2D b) {
        return addVec(middleVec(subVec(a, from), subVec(b, from)), from);
    }
    /**
     * Calculates the vector in the middle of the two given vectors.
     * 
     * @param a The first vector.
     * @param b The second vector.
     * @return The middle vector.
     */
    public static Point2D middleVec(final Point2D a, final Point2D b) {
        return mulVec(addVec(a, b), 0.5);
    }
    /**
     * Adds two points.
     * 
     * @param a The first point.
     * @param b The second point.
     * @return The resulting point.
     */
    public static Point2D addVec(final Point2D a, final Point2D b) {
        return new Point2D.Double(a.getX() + b.getX(), a.getY() + b.getY());
    }
    /**
     * Multiplies a point with a scalar.
     * 
     * @param a The point.
     * @param scalar The scalar.
     * @return the scaled version.
     */
    public static Point2D mulVec(final Point2D a, final double scalar) {
        return new Point2D.Double(a.getX() * scalar, a.getY() * scalar);
    }
    /**
     * Normalizes a vector, such that its length is {@code 1.0}.
     * 
     * @param v The vector.
     * @return A vector with the length {@code 1.0}.
     */
    public static Point2D normVec(final Point2D v) {
        return mulVec(v, 1.0 / vecLength(v));
    }
    /**
     * Subtracts {@code b} from {@code a}.
     * 
     * @param a The minuend.
     * @param b The subtrahend.
     * @return The difference.
     */
    public static Point2D subVec(final Point2D a, final Point2D b) {
        return new Point2D.Double(a.getX() - b.getX(), a.getY() - b.getY());
    }
    /**
     * Calculates the length of a vector. This operation is relatively expensive.
     * To compare two distances, use {@link #vecLengthSqr(Point2D)}.
     * 
     * @param v The vector.
     * @return The length of the vector.
     */
    public static double vecLength(final Point2D v) {
        return Math.sqrt(vecLengthSqr(v));
    }
    /**
     * Calculates the squared length of a vector. This method is much cheaper than
     * {@link #vecLength(Point2D)}.
     * 
     * @param v The vector.
     * @return The squared length.
     */
    public static double vecLengthSqr(final Point2D v) {
        return dot(v, v);
    }
}

Related Tutorials