规范化矢量JavaScript

时间:2016-11-15 01:13:21

标签: javascript vector

我目前正在尝试为我正在做的2D图形中的模块传递一些矢量测试。

我目前正在努力进行以下测试:

规范化功能 - 你的Vector对象应该有一个'normalize'函数,它不需要 参数。该函数应该返回一个新构造的Vector对象,该对象是结果 规范化'this'向量(从中制作单位向量)。

这是我到目前为止所做的:

规范化函数 - 你的Vector对象应该有一个'normalize'函数,它不需要 参数。该函数应该返回一个新构造的Vector对象,该对象是结果 规范化'this'向量(从中制作单位向量)。

 var Vector = (function () {
function Vector(pX, pY, pZ) {
    this.setX(pX);
    this.setY(pY);
    this.setZ(pZ);

}
Vector.prototype.getX = function () {
    return this.mX;
};
Vector.prototype.setX = function (pX) {
    this.mX = pX;
};
Vector.prototype.getY = function () {
    return this.mY;
};
Vector.prototype.setY = function (pY) {
    this.mY = pY;
}
Vector.prototype.getZ = function () {
    return this.mZ;
}
Vector.prototype.setZ = function (pZ) {
    this.mZ = pZ;
}

Vector.prototype.add = function (v) {
    return new Vector(this.getX() + v.getX(), this.getY() + v.getY(), this.getZ() + v.getZ());
}

Vector.prototype.subtract = function (v) {
    return new Vector(this.getX() - v.getX(), this.getY() - v.getY(), this.getZ() - v.getZ());
}

Vector.prototype.multiply = function (scalar) {
    return new Vector(this.getX() * scalar, this.getY() * scalar, this.getZ() * scalar);
};

Vector.prototype.divide = function (scalar) {
    return new Vector(this.getX() / scalar, this.getY() / scalar, this.getZ() /scalar);
};

Vector.prototype.magnitude = function () {
    return Math.sqrt(this.getX() * this.getX() + this.getY() * this.getY() + this.getZ() + this.getZ())
}


 //this is the vector I have tried for the normalisation
Vector.prototype.normalisedVector = function () {
    var vec = new Vector(this.getX(), this.getY(), this.getZ());
    return new Vector(vec.divide(this.magnitude()));
}
return Vector;
 }());

然而,我所尝试的并不起作用,我真的不明白为什么。任何帮助将不胜感激! 谢谢!

编辑 - 再次阅读它,我不认为我正在返回它所要求的单位向量。有谁知道我会怎么做呢?

2 个答案:

答案 0 :(得分:3)

find

应该是

nil

答案 1 :(得分:1)

Please choose the number of rows: Please enter a number bigger/equal to 5: 3 Please choose the number of columns: Please enter a number bigger/equal to 5: 5 Please enter a number bigger/equal to 5: 6 6 5 需要3个参数,Please choose the number of rows: Please enter a number bigger/equal to 5: 4 Please enter a number bigger/equal to 5: 5 Please choose the number of columns: Please enter a number bigger/equal to 5: 3 Please enter a number bigger/equal to 5: 7 5 7 while(results.next()) { // Put into interactive list a.add(hospital); hospital = results.getString("hospitalName"); { // Loops each hospital via popup, needs to be added to a selection menu //JOptionPane.showMessageDialog(null, hospital, "Hospital List", JOptionPane.INFORMATION_MESSAGE); System.out.println(hospital); } } { // Displays list of hospitals JOptionPane.showInputDialog(null, "Please choose a hospital", "Determined Hospitals", JOptionPane.QUESTION_MESSAGE, null, new Object[] {hospital}, a); } new Vector()。因此,您应该将当前向量的每个坐标除以X,并将它们用作参数。

Y