Vector2d structure : Vector « Game « Android






Vector2d structure

 

//package com.androidtowerwars.util;

class Vector2d {
    private float x;
    private float y;

    public Vector2d(float x, float y) {
        this.x = x;
        this.y = y;
    }

    public Vector2d(float[] v) {
        this.x = v[0];
        this.y = v[1];
    }

    public Vector2d(float[] v1, float[] v2) {
        this.x = v2[0] - v1[0];
        this.y = v2[1] - v1[1];
    }

    public Vector2d(float x1, float y1, float x2, float y2) {
        this.x = x2 - x1;
        this.y = y2 - y1;
    }

    public float getLength() {
        return (float) Math.sqrt(Math.pow(x,2)+Math.pow(y,2));
    }

    public final void normalize() {
        if (getLength() != 0) {
            float length = getLength();
            x            = x/length;
            y            = y/length;
        }
    }

    public float getX() {
        return x;
    }
    
    public float getY() {
        return y;
    }
}

   
  








Related examples in the same category

1.Wrapper activity demonstrating the use of the new SensorEvent rotation vector sensor, Sensor#TYPE_ROTATION_VECTOR TYPE_ROTATION_VECTOR}).
2.Vector2 Structure
3.Convenience library to do vector calculations
4.Vector3
5.Bit Vector
6.Compute the dot product of two vectors
7.Compute the cross product of two vectors
8.Compute the magnitude (length) of a vector
9.Vector3 structure