halive.shootinoutside.util.VectorUtils.java Source code

Java tutorial

Introduction

Here is the source code for halive.shootinoutside.util.VectorUtils.java

Source

/*******************************************************************************
 * Copyright (c) HALive, 2015.
 * For Licence information see LICENSE.md
 ******************************************************************************/

package halive.shootinoutside.util;

import com.badlogic.gdx.math.Vector2;
import halive.shootinoutside.common.util.Vector2D;

public class VectorUtils {

    public static Vector2 getUnitVector(Vector2 v) {
        Vector2 uv = new Vector2();
        double vx = v.x / (Math.sqrt(Math.pow(v.x, 2.0D) + Math.pow(v.y, 2.0D)));
        double vy = v.y / (Math.sqrt(Math.pow(v.x, 2.0D) + Math.pow(v.y, 2.0D)));
        uv.set((float) vx, (float) vy);
        return uv;
    }

    public static Vector2D toVector2D(Vector2 v) {
        return new Vector2D(v.x, v.y);
    }

    public static Vector2 toVector2(Vector2D v) {
        return new Vector2(v.getX(), v.getY());
    }

    public static Vector2 getDirectionUnitVector(Vector2 v1, Vector2 v2) {
        Vector2 direction = v2.sub(v1);
        return getUnitVector(direction);
    }

    public static Vector2 getDirectionVector(Vector2 v1, Vector2 v2) {
        Vector2 direction = v2.sub(v1);
        return direction;
    }

    public static enum Quadrant {
        QUADRANT1(1, 1), QUADRANT2(-1, 1), QUADRANT3(-1, -1), QUADRANT4(1, -1);

        private float xRatio;
        private float yRatio;

        private Quadrant(float xRatio, float yRatio) {
            this.xRatio = xRatio;
            this.yRatio = yRatio;
        }

        public static Quadrant determineQuadrant(float x, float y, float w, float h) {
            float newX = Math.signum(x - (w / 2));
            float newY = Math.signum((h / 2) - y);
            float xD = newX == 0 ? 1 : newX;
            float yD = newY == 0 ? 1 : newY;
            //System.out.printf("x=%f y=%f\n", xD, yD);
            for (Quadrant q : values()) {
                if (xD == q.xRatio && yD == q.yRatio) {
                    return q;
                }
            }
            return null;
        }

    }
}