# Implementation of the 4 dimensional vector. : Geometry « 2D Graphics GUI « Java

Implementation of the 4 dimensional vector.

```
/* \$Id: Vector4f.java,v 1.1 2005/08/20 10:15:55 joda Exp \$
* Created on 22.07.2004
*/
//package net.sourceforge.ftgl.util;

/**
* Implementation of the 4 dimensional vector.
*
* @author Ralf Petring
* @author funsheep
*/
public class Vector4f implements Cloneable {
/** x component */
public float x;
/** y component */
public float y;
/** z component */
public float z;
/** w component */
public float w;

/**
* Constructs a new vector whith (0,0,0,1) as coordinates.
*/
public Vector4f() {
this(0, 0, 0, 1);
}

/**
* Creates a new vector.
*
* @param x
*            the x coordinate
* @param y
*            the y coordinate
* @param z
*            the z coordinate
* @param w
*            the w coordinate
*/
public Vector4f(final float x, final float y, final float z, final float w) {
this.x = x;
this.y = y;
this.z = z;
this.w = w;
}

/**
* Copyconstructor.
*
* @param v
*            the vector to copy
*/
public Vector4f(final Vector4f v) {
this(v.x, v.y, v.z, v.w);
}

/**
* Adds the given values to this vector.
*
* @param dx
*            the x value to add
* @param dy
*            the y value to add
* @param dz
*            the z value to add
* @return this vector
*/
public Vector4f add(final float dx, final float dy, final float dz) {
this.x += dx;
this.y += dy;
this.z += dz;
return this;
}

/**
* Adds a vector to this vector.
*
* @param v
*            the vector to add
* @return this vector
*/
public Vector4f add(final Vector4f v) {
return this.add(v.x, v.y, v.z);
}

/**
* Substracts the given values from this vector.
*
* @param dx
*            the x value to substract
* @param dy
*            the y value to substract
* @param dz
*            the z value to substract
* @return this vector
*/
public Vector4f sub(final float dx, final float dy, final float dz) {
return this.add(-dx, -dy, -dz);
}

/**
* Substracts a given vector from this one.
*
* @param v
*            the vector to substract
* @return this vector
*/
public Vector4f sub(final Vector4f v) {
return this.add(-v.x, -v.y, -v.z);
}

/**
* Multiplies the coordinates of this vector with the given float values.
*
* @param dx
*            the value to be multiplied the x coordinate
* @param dy
*            the value to be multiplied the y coordinate
* @param dz
*            the value to be multiplied the z coordinate
* @return this vector
*/
public Vector4f scale(final float dx, final float dy, final float dz) {
this.x *= dx;
this.y *= dy;
this.z *= dz;
return this;
}

/**
* Multiplies the coordinates of this vector with the given float value.
*
* @param d
*            the value to be multiplied with all coordinates
* @return this vector
*/
public Vector4f scale(final float d) {
return this.scale(d, d, d);
}

/**
* Returns the squared length of this vector.
*
* @return the squared length of this vector
*/
public final float lengthSquared() {
return (this.x * this.x + this.y * this.y + this.z * this.z); // TODO
// needs
// w?
}

/**
* Returns the length of this vector.
*
* @return the length of this vector
*/
public final float length() {
// TODO needs w?
return (float) Math.sqrt(this.lengthSquared());
}

/**
* Normalizes this vector.
*
* @return this vector
*/
public final Vector4f normalize() {
// TODO needs w?
float norm = 1f / this.length();

this.x *= norm;
this.y *= norm;
this.z *= norm;

return this;
}

/**
* Calculates the dotprodukt of this vector and the given vector.
*
* @param v
*            the second vector
* @return the scalarproduct between this and the second vector
*/
public final float dot(final Vector4f v) {
return this.x * v.x + this.y * v.y + this.z * v.z + this.w * v.w; // TODO
// w
// component
// also?
}

/**
* {@inheritDoc}
*/
public Object clone() {
try {
return super.clone();
} catch (CloneNotSupportedException e) {
throw new RuntimeException("the roof is on fire", e);
}
}

/**
* {@inheritDoc}
*/
public String toString() {
return "[" + this.x + "," + this.y + "," + this.z + "," + this.w + "]";
}

}

```

### Related examples in the same category

 1 Collection of geometry utility methods 2 Unions Rectangle2D 3 Interpolates points given in the 2D plane 4 Returns distance between two sets of coords 5 Returns distance between 3D set of coords 6 Returns closest point on segment to point 7 Calculate Angle From 8 Returns distance to segment 9 Hexagon demo 10 Implements an Vector in 3D space. 11 Quaternion 12 Circle shape 13 Geometry Utilities 14 This is a Polygon that allows the user to flip and swap the points along it's axis. 15 Fast trigonometric operations 16 A class to represent a latitude and longitude 17 An undirected graph that keeps track of connected components (groups). 18 Generates n logarithmically-spaced points between d1 and d2 using the provided base. 19 Returns a dimension where width and height are inside the bounds of the maxWidth and maxHeight parameters