Newer
Older
EPQ-3D-renderer / src / main / java / uk / org / floop / epq3d / Vector2D.java
package uk.org.floop.epq3d;

import static java.lang.Double.isFinite;

public class Vector2D {
    // technically not required as vectors have the same information as points, but it's useful to have separate vector and point things...
    public double x;
    public double y;

    public Vector2D(double _x, double _y){
        x = _x;
        y = _y;
    }
    public void createFrom2Points(Point2D start, Point2D end){
        x = end.x - start.x;
        y = end.y - start.y;
    }
    public double getLength(){
        return Math.sqrt(x*x + y*y);
    }
    public double angleTo(Vector2D vec2){
        double result = Math.acos(
                dot(vec2)
                / (getLength()*vec2.getLength()))
                * Math.signum(x*vec2.y - y *vec2.x);
        if(!isFinite(result)){result = 0;}
        return result;
    }
    public double dot(Vector2D vec2){
        return (x*vec2.x) + (y*vec2.y);
    }
}