gpt4 book ai didi

java - 2D vector 库

转载 作者:行者123 更新时间:2023-12-02 02:22:38 27 4
gpt4 key购买 nike

我正在尝试创建一个 2D vector 数学库,并创建了适当的类和函数来执行特定的计算,但是当我尝试从我的主类中调用这些方法时,不会返回任何内容,或者如果确实返回了,它将是 0。我不知道我哪里出了问题,有人可以帮忙吗?

public class Vector2D {

public double x = 0.0;
public double y = 0.0;

public Vector2D(){
}

public Vector2D(double vectorX, double vectorY){

vectorX = x;
vectorY = y;
}

}

这是我的实际 vector 本身的类。

public class Vector2DMaths {

public static Vector2D addition2D(Vector2D vec1, Vector2D vec2){

Vector2D vecAddResult2D = new Vector2D();

vecAddResult2D.x = vec1.x + vec2.x;
vecAddResult2D.y = vec1.y + vec2.y;

return vecAddResult2D;
}

public static Vector2D subtraction2D(Vector2D vec3, Vector2D vec4){

Vector2D vecSubtractResult2D = new Vector2D();

vecSubtractResult2D.x = vec3.x - vec4.x;
vecSubtractResult2D.y = vec3.y - vec4.y;

return vecSubtractResult2D;
}

public static double findMagnitude2D(Vector2D vec5){

double d;
double magnitude;

d = (Math.pow((vec5.x), 2) + Math.pow((vec5.y), 2));

magnitude = Math.sqrt(d);

return magnitude;
}

public static double dotProduct2D(Vector2D a, Vector2D b){

double aDOTb;
double aMag;
double bMag;
double angle;

aDOTb = (a.x * b.x) + (a.y + b.y);

aMag = findMagnitude2D(a);
bMag = findMagnitude2D(b);

angle = Math.acos((aDOTb)/(aMag * bMag));
angle = angle * (180/Math.PI);

return angle;
}

public static Vector2D unitNormal2D(Vector2D vec6){

double vec6Mag;
Vector2D unitN2D = new Vector2D();

vec6Mag = findMagnitude2D(vec6);

unitN2D.x = vec6.x / vec6Mag;
unitN2D.y = vec6.y / vec6Mag;

return unitN2D;
}

public static Vector2D rotation(Vector2D vec7, double angle){

Vector2D rotatedVec = new Vector2D();

rotatedVec.x = (Math.cos(angle)*vec7.x) - (Math.sin(angle)*vec7.y);
rotatedVec.y = (Math.sin(angle)*vec7.x) - (Math.cos(angle)*vec7.y);

return rotatedVec;
}

public static Vector2D velocityVector(double speed, double launchAngle){

Vector2D vec8 = new Vector2D();
Vector2D velocityVec = new Vector2D();
double vMag, velocity;

vec8.x = Math.cos(launchAngle);
vec8.y = Math.sin(launchAngle);

vMag = findMagnitude2D(vec8);

velocity = speed / vMag;

velocityVec.x = vec8.x * velocity;
velocityVec.y = vec8.y * velocity;

return velocityVec;
}
}

这是我的实际数学库类(class)。

switch (options){

case "1":

System.out.println("Vector 1: (3.3,6.2)");
System.out.println("Vector 2: (4.2,12.7)");

Vector2D option1Vec1 = new Vector2D(3.3,6.2);

Vector2D option1Vec2 = new Vector2D(4.2, 12.7);

System.out.println("The product of these two vectors are " + Vector2DMaths.addition2D(option1Vec1, option1Vec2));
break;

case "2":

System.out.println("Vector 1: (1.0, 4.5, 8.3)");
System.out.println("Vector 2: (12.6, -4.5, 6.7)");

Vector3D option2Vec1 = new Vector3D(1.0, 4.5, 8.3);

Vector3D option2Vec2 = new Vector3D(12.6, -4.5, 6.7);

System.out.println("The product of these two vectors are " + Vector3DMaths.addition3D(option2Vec1, option2Vec2));
break;

case "3":

System.out.println("Vector 1: (3.3,6.2)");
System.out.println("Vector 2: (4.2,12.7)");

Vector2D option3Vec1 = new Vector2D(3.3,6.2);

Vector2D option3Vec2 = new Vector2D(4.2, 12.7);

System.out.println("The difference of these two vectors are " + Vector2DMaths.subtraction2D(option3Vec1, option3Vec2));
break;

case "4":

System.out.println("Vector 1: (1.0, 4.5, 8.3)");
System.out.println("Vector 2: (12.6, -4.5, 6.7)");

Vector3D option4Vec1 = new Vector3D(1.0, 4.5, 8.3);

Vector3D option4Vec2 = new Vector3D(12.6, -4.5, 6.7);

System.out.println("The difference of these two vectors are " + Vector3DMaths.subtraction3D(option4Vec1, option4Vec2));
break;

case "5":

System.out.println("Vector 1: (7.5,7.5)");
System.out.println("Vector 2: (12.0,0.0)");

Vector2D option5Vec1 = new Vector2D(7.5,7.5);

Vector2D option5Vec2 = new Vector2D(12.0,0.0);

System.out.println("The angle found by doing the dot product of these two vectors is " + Vector2DMaths.dotProduct2D(option5Vec1, option5Vec2));
break;

case "6":

System.out.println("Vector 1: (1.0, 4.5, 8.3)");
System.out.println("Vector 2: (12.6, -4.5, 6.7)");

Vector3D option6Vec1 = new Vector3D(1.0, 4.5, 8.3);

Vector3D option6Vec2 = new Vector3D(12.6, 0.0, 6.7);

System.out.println("The angle found by doing the dot product of these two vectors is " + Vector3DMaths.dotProduct3D(option6Vec1, option6Vec2));
break;

case "7":

System.out.println("Vector 1: (7.5,7.5)");

Vector2D option7Vec1 = new Vector2D(7.5,7.5);

System.out.println("The magnitude of this vector is " + Vector2DMaths.findMagnitude2D(option7Vec1));
break;

case "8":

System.out.println("Vector 1: (1.0, 4.5, 8.3)");

Vector3D option8Vec1 = new Vector3D(1.0, 4.5, 8.3);

System.out.println("The magnitude of this vector is " + Vector3DMaths.findMagnitude3D(option8Vec1));
break;

case "9":

System.out.println("Vector 1: (7.5,7.5)");
System.out.println("Vector 2: (12.0,0.0)");

Vector2D option9Vec1 = new Vector2D(7.5,7.5);

Vector2D option9Vec2 = new Vector2D(12.0,0.0);

System.out.println("The unit normals for these vectors are " + Vector2DMaths.unitNormal2D(option9Vec1) + " and " + Vector2DMaths.unitNormal2D(option9Vec2) + " respectively");
break;

case "10":

System.out.println("Vector 1: (1.0, 4.5, 8.3)");
System.out.println("Vector 2: (12.6, -4.5, 6.7)");

Vector3D option10Vec1 = new Vector3D(1.0, 4.5, 8.3);

Vector3D option10Vec2 = new Vector3D(12.6, 0.0, 6.7);

System.out.println("The unit normals for these vectors are " + Vector3DMaths.unitNormal3D(option10Vec1) + " and " + Vector3DMaths.unitNormal3D(option10Vec2) + " respectively");
break;

case "11":

System.out.println("Vector: (3.0,4.0)");
System.out.println("Angle: 60 degrees");

Vector2D option11Vec = new Vector2D(3.0,4.0);

double angle = 60.0;

System.out.println("After the rotation, the vector now has a quantity of " + Vector2DMaths.rotation(option11Vec, angle));
break;

case "12":

System.out.println("Speed: 20 m/s");
System.out.println("Angle: 45 degrees");

double speed = 20.0;
double launchAngle = 45.0;

System.out.println("The velocity vector from this speed and angle is " + Vector2DMaths.velocityVector(speed, launchAngle));
break;
}

我在这里制作了一个开关板,您可以在其中调用不同的方法。当我返回一个 Vector2D 对象时,会返回一些不连贯的东西,当我返回一个 double 时,会返回 0.0。据我所知,数学是正确的,但我只是不知道如何获得返回的实际值。

最佳答案

首先,您的 Vector2D 类的构造函数是错误的。变量 x 和 y 永远不会更新。这就是为什么所有 double 返回都为 0(注意:java 将 double 初始化为零)。你的构造函数应该是:

public Vector2D(double vectorX, double vectorY) {
this.x = vectorX;
this.y = vectorY;
}

接下来,正如 @Johnny Mopp 建议的那样,您需要向 Vector2D 类添加一个 toString() 方法,以便 java 正确打印 vector 实例。否则java只是打印对象的内存位置(这看起来像乱码)。这可以通过以下方式实现:

@override
public String toString() {
// Can add more fancy print formats here.
return this.x+" "+this.y;
}

关于java - 2D vector 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48287927/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com