Calculates the vector in the middle of the two given vectors. - Java java.lang

Java examples for java.lang:Math Vector

Description

Calculates the vector in the middle of the two given vectors.

Demo Code


//package com.java2s;

import java.awt.geom.Point2D;

public class Main {
    /**//from w w w . jav a2 s . c om
     * 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);
    }

    /**
     * Multiplies a point with a scalar.
     * 
     * @param v Point.
     * @param s Scalar.
     * @return The scaled vector.
     */
    public static Point2D mulVec(final Point2D v, final double s) {
        return new Point2D.Double(v.getX() * s, v.getY() * s);
    }

    /**
     * Adds two points.
     * 
     * @param a Point.
     * @param b Point.
     * @return The sum vector.
     */
    public static Point2D addVec(final Point2D a, final Point2D b) {
        return new Point2D.Double(a.getX() + b.getX(), a.getY() + b.getY());
    }
}

Related Tutorials