public VectorNf add(VectorNf v) { return add(v.vec); }
public float distance(VectorNf v) { return distance(v.vec); }
/** * Attempts to normalize a vector. If this fails, the method catches the exception and return a zero vector of the same dimension instead. * * @param v The vector to attempt to normalize * @return The normalized vector, or the zero vector if it couldn't be normalized. */ public static VectorNf normalizeSafe(VectorNf v) { try { return v.normalize(); } catch (ArithmeticException ex) { return new VectorNf(v.size()); } }
public VectorNf transform(float... vec) { final int size = size(); if (size != vec.length) { throw new IllegalArgumentException("Matrix and vector sizes must be the same"); } final VectorNf d = new VectorNf(size); for (int row = 0; row < size; row++) { float dot = 0; for (int col = 0; col < size; col++) { dot += mat[row][col] * vec[col]; } d.set(row, dot); } return d; }
@Override public VectorNf normalize() { final float length = length(); if (Math.abs(length) < GenericMath.FLT_EPSILON) { throw new ArithmeticException("Cannot normalize the zero vector"); } final int size = size(); final VectorNf d = new VectorNf(size); for (int comp = 0; comp < size; comp++) { d.vec[comp] = (float) (vec[comp] / length); } return d; }
public VectorNf project(float... v) { final int size = size(); if (size != v.length) { throw new IllegalArgumentException("Vector sizes must be the same"); } float lengthSquared = 0; for (int comp = 0; comp < size; comp++) { lengthSquared += v[comp] * v[comp]; } if (Math.abs(lengthSquared) < GenericMath.FLT_EPSILON) { throw new ArithmeticException("Cannot project onto the zero vector"); } final float a = dot(v) / lengthSquared; final VectorNf d = new VectorNf(size); for (int comp = 0; comp < size; comp++) { d.vec[comp] = a * v[comp]; } return d; }
@Override public VectorNf clone() { return new VectorNf(this); }
@Override public int compareTo(VectorNf v) { return (int) Math.signum(lengthSquared() - v.lengthSquared()); }
public VectorNf div(VectorNf v) { return div(v.vec); }
public float distanceSquared(VectorNf v) { return distanceSquared(v.vec); }
public VectorNf max(VectorNf v) { return max(v.vec); }
public float dot(VectorNf v) { return dot(v.vec); }
public VectorNf transform(float... vec) { final int size = size(); if (size != vec.length) { throw new IllegalArgumentException("Matrix and vector sizes must be the same"); } final VectorNf d = new VectorNf(size); for (int row = 0; row < size; row++) { float dot = 0; for (int col = 0; col < size; col++) { dot += mat[row][col] * vec[col]; } d.set(row, dot); } return d; }
@Override public VectorNf normalize() { final float length = length(); if (Math.abs(length) < GenericMath.FLT_EPSILON) { throw new ArithmeticException("Cannot normalize the zero vector"); } final int size = size(); final VectorNf d = new VectorNf(size); for (int comp = 0; comp < size; comp++) { d.vec[comp] = (float) (vec[comp] / length); } return d; }
/** * Attempts to normalize a vector. If this fails, the method catches the exception and return a zero vector of the same dimension instead. * * @param v The vector to attempt to normalize * @return The normalized vector, or the zero vector if it couldn't be normalized. */ public static VectorNf normalizeSafe(VectorNf v) { try { return v.normalize(); } catch (ArithmeticException ex) { return new VectorNf(v.size()); } }
public VectorNf project(float... v) { final int size = size(); if (size != v.length) { throw new IllegalArgumentException("Vector sizes must be the same"); } float lengthSquared = 0; for (int comp = 0; comp < size; comp++) { lengthSquared += v[comp] * v[comp]; } if (Math.abs(lengthSquared) < GenericMath.FLT_EPSILON) { throw new ArithmeticException("Cannot project onto the zero vector"); } final float a = dot(v) / lengthSquared; final VectorNf d = new VectorNf(size); for (int comp = 0; comp < size; comp++) { d.vec[comp] = a * v[comp]; } return d; }