/** * Returns reflection of this vector through a plane specified by a point on the plane and a * vector normal to the plane. * * @param x0 point on the plane * @param normal normal to the plane * @return reflected position */ public Vector3d reflectionThroughPlaneAt(Vector3d x0, Vector3d normal) { Vector3d x1 = this.minus(x0); double alpha = -2 * x1.dotProduct(normal) / normal.dotProduct(normal); Vector3d reflection = Vector3d.linearCombination(1, this, alpha, normal); return reflection; }
/** Returns cosine of the angle between this vector and vector v. */ public double cosAngleWith(Vector3d v) { if (isZero() || v.isZero()) { return 1; } double cosAngle = dotProduct(v) / (norm() * v.norm()); cosAngle = Math.min(1, Math.max(-1, cosAngle)); return cosAngle; }
public static Vector3d linearCombination( double a1, Vector3d v1, double a2, Vector3d v2, double a3, Vector3d v3) { Vector3d v = new Vector3d( (a1 * v1.getX()) + (a2 * v2.getX()) + (a3 * v3.getX()), (a1 * v1.getY()) + (a2 * v2.getY()) + (a3 * v3.getY()), (a1 * v1.getZ()) + (a2 * v2.getZ()) + (a3 * v3.getZ())); return v; }
/** * Returns the projection of this vector onto the vector v. Lenient version treats zero vector for * v as (1,0,0) in order to avoid returning vector containing NaN values. */ public Vector3d projectionOnto(Vector3d v, boolean isLenient) { if (isLenient && v.isZero()) { v = new Vector3d(1, 0, 0); } return projectOnto(v); }
public static Vector3d linearCombination(double a1, Vector3d v1, double a2, Vector3d v2) { return new Vector3d( (a1 * v1.getX()) + (a2 * v2.getX()), (a1 * v1.getY()) + (a2 * v2.getY()), (a1 * v1.getZ()) + (a2 * v2.getZ())); }
/** * Returns the projection of this vector onto the vector v. * * @see #projectionOnto(Vector2d, boolean) for lenient handling of v */ public Vector3d projectOnto(Vector3d v) { double scale = dotProduct(v) / v.normSquared(); return v.scaledBy(scale); }