Not working
This commit is contained in:
125
src/Vector.java
Normal file
125
src/Vector.java
Normal file
@ -0,0 +1,125 @@
|
||||
import codedraw.CodeDraw;
|
||||
|
||||
/**
|
||||
* This class represents vectors in a 3D vector space.
|
||||
*/
|
||||
public class Vector {
|
||||
private double x;
|
||||
private double y;
|
||||
private double z;
|
||||
|
||||
public Vector() {
|
||||
this(0);
|
||||
}
|
||||
|
||||
public Vector(double v) {
|
||||
this(v, v, v);
|
||||
}
|
||||
|
||||
public Vector(double x, double y, double z) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
}
|
||||
|
||||
public Vector(Vector other) {
|
||||
this(other.x, other.y, other.z);
|
||||
}
|
||||
|
||||
public double getX() {
|
||||
return this.x;
|
||||
}
|
||||
|
||||
public double getY() {
|
||||
return this.y;
|
||||
}
|
||||
|
||||
public double getZ() {
|
||||
return this.z;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the sum of this vector and vector 'v'.
|
||||
*/
|
||||
public Vector plus(Vector v) {
|
||||
return new Vector(x + v.x, y + v.y, z + v.z);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the product of this vector and 'd'.
|
||||
*/
|
||||
public Vector times(double d) {
|
||||
return new Vector(x * d, y * d, z * d);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the sum of this vector and -1*v.
|
||||
*/
|
||||
public Vector minus(Vector v) {
|
||||
return new Vector(x - v.x, y - v.y, z - v.z);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the Euclidean distance of this vector
|
||||
* to the specified vector 'v'.
|
||||
*/
|
||||
public double distanceTo(Vector v) {
|
||||
double dX = x - v.x;
|
||||
double dY = y - v.y;
|
||||
double dZ = z - v.z;
|
||||
return Math.sqrt(dX * dX + dY * dY + dZ * dZ);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the length (norm) of this vector.
|
||||
*/
|
||||
public double length() {
|
||||
return distanceTo(new Vector());
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalizes this vector: changes the length of this vector such that it becomes 1.
|
||||
* The direction and orientation of the vector is not affected.
|
||||
*/
|
||||
public void normalize() {
|
||||
double length = length();
|
||||
x /= length;
|
||||
y /= length;
|
||||
z /= length;
|
||||
}
|
||||
|
||||
public double getScreenX(CodeDraw cd) {
|
||||
return cd.getWidth() * (this.x + Simulation.SECTION_SIZE / 2) / Simulation.SECTION_SIZE;
|
||||
}
|
||||
|
||||
public double getScreenY(CodeDraw cd) {
|
||||
return cd.getWidth() * (this.y + Simulation.SECTION_SIZE / 2) / Simulation.SECTION_SIZE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Draws a filled circle with a specified radius centered at the (x,y) coordinates of this vector
|
||||
* in the canvas associated with 'cd'. The z-coordinate is not used.
|
||||
*/
|
||||
public void drawAsFilledCircle(CodeDraw cd, double radius) {
|
||||
radius = cd.getWidth() * radius / Simulation.SECTION_SIZE;
|
||||
cd.fillCircle(getScreenX(cd), getScreenY(cd), Math.max(radius, 1.5));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the coordinates of this vector in brackets as a string
|
||||
* in the form "[x,y,z]", e.g., "[1.48E11,0.0,0.0]".
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("[%g,%g,%g]", x, y, z);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
if (other.getClass() != Vector.class) {
|
||||
return false;
|
||||
}
|
||||
Vector v = (Vector) other;
|
||||
return this.x == v.x && this.y == v.y && this.z == v.z;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user