# Convenience library to do vector calculations : Vector « Game « Android

Convenience library to do vector calculations

```
//package com.nomnom.common;

import android.graphics.PointF;

/**
* Convenience library to do vector calculations
*
* @author garysoed
*/
public class Vector {

private float x;
private float y;

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

public float getX() {
return x;
}

public float getY() {
return y;
}

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

public float length() {
return (float)Math.sqrt(lengthSquared());
}

public float lengthSquared() {
return this.dot(this);
}

public float dot(Vector v2) {
return x * v2.x + y * v2.y;
}

setXY(x + v2.x, y + v2.y);
return this;
}

public Vector substract(Vector v2) {
setXY(x - v2.x, y - v2.y);
return this;
}

public Vector multiply(float constant) {
setXY(x * constant, y * constant);
return this;
}

public Vector unitVector() {
float length = length();
return this.multiply(1 / length);
}

public Vector normal() {
setXY(-y, x);
return this;
}

public float bearing() {
return (float) Math.atan2(y, x);
}

public static Vector copyOf(Vector vector) {
return new Vector(vector.x, vector.y);
}

public static Vector fromPointF(PointF point) {
return new Vector(point.x, point.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 Vector2d structure 3 Vector2 Structure 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