Hallo,
ich habe ein Programm geschrieben, dass das Thema Vector umfasst...jedoch bei der Vectoraddition kommt immer nicht das richtige raus. Es gibt ei Ergebnis raus, alleridungs nicht das richtige.
Könnt ihr mir vielleicht helfen? Ich weiß nicht mehr weiter...
Vielen Dank schon mal!
ich habe ein Programm geschrieben, dass das Thema Vector umfasst...jedoch bei der Vectoraddition kommt immer nicht das richtige raus. Es gibt ei Ergebnis raus, alleridungs nicht das richtige.
Könnt ihr mir vielleicht helfen? Ich weiß nicht mehr weiter...
Java:
public final class Vector {
/**************************
Membervariablen
**************************/
private double Direction;
private double Length;
/**************************
Konstruktoren
**************************/
public Vector() {
this.Length = 0.0;
}
public Vector(double Direction) {
this.Length = 1.0;
this.Direction = Direction;
}
public Vector(double Direction, double Length) {
if(Length < 0){
this.Length = -Length;
this.Direction = Direction+Math.PI;
}
else{
this.Length = Length;
this.Direction = Direction;
}
while(this.Direction >= 2*Math.PI){
this.Direction -= 2*Math.PI;
}
}
/**************************
Getter
**************************/
private double getLength() {
return Math.abs(this.Length);
}
private double getDirection() {
return this.Direction;
}
/**************************
Öffentliche Methoden
**************************/
public boolean isNull() {
// Wenn this.Length dem Wert 0.0 entspricht gebe true zurück, wenn nicht dann false
return this.getLength() == 0.0 ? true : false;
}
public boolean isParallel(Vector more) {
if( (this.isNull()) || (more.isNull()) )
{ return true; }
if( (near(this.getDirection(), more.getDirection())) || (near(this.getDirection() + Math.PI, more.getDirection())) ||(near(this.getDirection(), more.getDirection() + Math.PI)) )
{ return true; }
return false;
}
public boolean isSameAs(Vector more){
if( (near(this.getLength(), more.getLength())) && (near(this.getDirection(), more.getDirection())) ||((this.isNull()) || (more.isNull())) )
{ return true; }
return false;
}
public Vector scaledBy(double scale) {
return new Vector( scale * this.getLength(), this.getDirection() );
}
Vector add(Vector more) {
double newLength;
double newDirection;
double xLength = Math.cos(getDirection()) * getLength();
double yLength = Math.sin(getDirection()) * getLength();
double xDirection = Math.cos(more.getDirection()) * more.getLength();
double yDirection = Math.sin(more.getDirection()) * more.getLength();
xLength += xDirection;
yLength += yDirection;
newLength = to(Math.sqrt(xLength * xLength + yLength * yLength));
if(Math.atan2(yLength, xLength) < 0) {
newDirection = to((Math.atan2(yLength, xLength)) + 2 * Math.PI);
}
else {
newDirection = to(Math.atan2(yLength, xLength));
}
return new Vector(newLength, newDirection);
}
public void print(){
System.out.println("Länge = " + to(this.getLength()) + " ,Direction = " + to(this.getDirection()) + " rad");
}
public void print(String prefix) {
System.out.print(prefix + ": ");
this.print();
}
/**************************
Private Methoden
**************************/
private boolean near(double val1, double val2) {
// Differenz der beiden Werte berechnen
double diference = Math.abs(val1 - val2);
// Prüfung ob Differenz kleiner 1e-8 ist und Rückgabe von true oder false
return diference < 1e-8 ? true : false;
}
private double to(double zahl) {
if (zahl != Math.PI){
zahl *= 100000;
zahl = Math.rint(zahl);
zahl /= 100000;
return zahl;
}
else{
return zahl;
}
}
}
Java:
public class VectorMain {
public static void main(String[] args) {
double Length1 = Double.parseDouble(args[0]);
double Direction1 = Double.parseDouble(args[1]);
double factor1 = Double.parseDouble(args[2]);
double Length2 = Double.parseDouble(args[3]);
double Direction2 = Double.parseDouble(args[4]);
Vector v1 = new Vector();
Vector v2 = new Vector(Length1, Direction1);
Vector v3 = new Vector(Length2, Direction2);
Vector v4 = v3.scaledBy(factor1);
v4.print("V 4");
Vector v5 = v1.add(v2);
v5.print("V 5");
System.out.println( v3.isNull() );
if(v2.isParallel(v1)) {
System.out.println("Vector 2 und Vector 1 sind parallel");}
else {
System.out.println("Vector 2 und Vector 1 sind nicht parallel");
}
if(v2.isParallel(v3)) {
System.out.println("Vector 2 und Vector 3 sind parallel");
}
else {
System.out.println("Vector 2 und Vector 3 sind nicht parallel");
}
if(v2.isSameAs(v1)) {
System.out.println("Vector 2 und Vector 1 sind gleich");
}
else {
System.out.println("Vector 2 und Vector 1 sind nicht gleich");
}
if(v2.isSameAs(v3)) {
System.out.println("Vector 2 und Vector 3 sind gleich");
}
else {
System.out.println("Vector 2 und Vector 3 sind nicht gleich");
}
}
}
Vielen Dank schon mal!