HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

3-D Space Vector

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
spacevectorstackoverflow

Problem

I wrote the following implementation of a Vector in Java. I was wondering what you folks thought of it.

package space;

public class SpaceVector {

private final double x;
private final double y;
private final double z;

public SpaceVector(double x, double y, double z){
this.x = x;
this.y = y;
this.z = z;
}

public SpaceVector minus(SpaceVector o) {
return new SpaceVector(x - s.x, y - s.y, z - s.z);
}

public double dot(SpaceVector o) {
return x o.x + y o.y + z * o.z;
}

public double abs() {
return Math.sqrt(x x + y y + z * z);
}

public SpaceVector cross(SpaceVector o) {
double i, j, k;
i = y o.z - z o.y;
j = -(x o.z - z o.x);
k = x o.y - y o.x;
return new SpaceVector(i, j, k);
}
}

Solution

Within a mathematical tradition,

-
minus is an unary operator, as in

public minus() {
    return new SpaceVector(-x, -y, -z);
}


-
A vector-by-scalar multiplication

public scale_by(double scalar)


is expected (notice that minus is actually scale_by(-1)).

-
Vector addition

public add(SpaceVector other)


is expected.

-
abs is usually called norm.

Code Snippets

public minus() {
    return new SpaceVector(-x, -y, -z);
}
public scale_by(double scalar)
public add(SpaceVector other)

Context

StackExchange Code Review Q#82529, answer score: 5

Revisions (0)

No revisions yet.