Finished AB 1

This commit is contained in:
2022-03-17 13:53:41 +01:00
parent 971beaf1ae
commit 155dc74001
4 changed files with 107 additions and 216 deletions

View File

@ -1,73 +1,89 @@
import codedraw.CodeDraw;
import java.awt.*;
// This class represents vectors in a 3D vector space.
public class Vector3 {
//TODO: change modifiers.
public double x;
public double y;
public double z;
private double x;
private double y;
private double z;
//TODO: define constructor.
public Vector3() {
this(0);
}
public Vector3(double v) {
this(v, v, v);
}
public Vector3(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
// Returns the sum of this vector and vector 'v'.
public Vector3 plus(Vector3 v) {
//TODO: implement method.
return null;
Vector3 result = new Vector3();
result.x = x + v.x;
result.y = y + v.y;
result.z = z + v.z;
return result;
}
// Returns the product of this vector and 'd'.
public Vector3 times(double d) {
//TODO: implement method.
return null;
Vector3 result = new Vector3();
result.x = x * d;
result.y = y * d;
result.z = z * d;
return result;
}
// Returns the sum of this vector and -1*v.
public Vector3 minus(Vector3 v) {
//TODO: implement method.
return null;
Vector3 result = new Vector3();
result.x = x - v.x;
result.y = y - v.y;
result.z = z - v.z;
return result;
}
// Returns the Euclidean distance of this vector
// to the specified vector 'v'.
public double distanceTo(Vector3 v) {
//TODO: implement method.
return -1d;
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() {
//TODO: implement method.
return 0;
return distanceTo(new Vector3());
}
// 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() {
//TODO: implement method.
double length = length();
x /= length;
y /= length;
z /= length;
}
// 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) {
//TODO: implement method.
double x = cd.getWidth() * (this.x + Simulation.SECTION_SIZE / 2) / Simulation.SECTION_SIZE;
double y = cd.getWidth() * (this.y + Simulation.SECTION_SIZE / 2) / Simulation.SECTION_SIZE;
radius = cd.getWidth() * radius / Simulation.SECTION_SIZE;
cd.fillCircle(x, y, 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]".
public String toString() {
//TODO: implement method.
return "";
return String.format("[%f,%f,%f]", x, y, z);
}
}