2D矢量库

时间:2018-01-16 18:47:53

标签: java vector

我试图创建一个2D矢量数学库,并制作了适当的类和函数来执行特定的计算,但是当我尝试从我的主类调用这些所述方法时,没有任何东西被返回或者如果它确实得到了回来了,它会是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;
    }

}

这是我的实际矢量本身的课程。

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;
    }  
}

这是我的实际数学库的课程。

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。据我所知,数学是正确的,但我不知道如何获得实际值。

1 个答案:

答案 0 :(得分:2)

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

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

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

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