【问题标题】:Q Dotproduct of a vector向量的 Q 点积
【发布时间】:2016-12-06 21:59:43
【问题描述】:

我有我的矢量对象,并试图让它通过一些测试。 首先,第一个 dotProduct 函数通过了“是肯定的测试”。 但我正在努力使其通过“为阴性”和“为零”的另外两个测试。

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.setZ = function () {
    return this.mZ;
};
Vector.prototype.getZ = function (pZ) {
    this.mZ = pZ;
};
Vector.prototype.add = function (v) {
    return new Vector(this.getX() + v.getX(), this.getY() + v.getY());
};
Vector.prototype.subtract = function (s) {
    return new Vector(this.getX() - s.getX(), this.getY() - s.getY());
};
Vector.prototype.multiply = function (scalar) {
    return new Vector(this.getX() * scalar, this.getY() * scalar);
};
Vector.prototype.divide = function (scalar) {
    return new Vector(this.getX() / scalar, this.getY() / scalar);
};
Vector.prototype.magnitude = function () {
    return Math.sqrt(this.getX() * this.getX() + this.getY() * this.getY());
};
Vector.prototype.normalise = function () {
    return new Vector(this.getX() / this.magnitude(), this.getY() / this.magnitude());
};
Vector.prototype.limitTo = function (Scalar) {
    this.normalise();
    this.multiply(Scalar);
    return new Vector(this.getX(), this.getY(), this.getZ());
};

//Vector.prototype.dotProduct = function () {
//    var secondVector = new Vector(100, 400);

//    return new Vector(this.getX() * secondVector.getX() +
//        this.getY() * secondVector.getY());
//};
Vector.prototype.dotProduct = function (secondVector) {
    secondVector = new Vector(-100, -400);

    return new Vector(this.getX() * secondVector.getX() +
        this.getY() * secondVector.getY() / Math.acos(90));
};

return Vector;

}());

测试条件:

describe("Dot Product", function () {

    it("Result is zero", function () {
        var secondVector, dotProduct;
        secondVector = new Vector(40, -30, 0);
        dotProduct = vector.dotProduct(secondVector);

        expect(dotProduct).toEqual(0);
    });

    it("Result is positive", function () {
        var secondVector, dotProduct;
        secondVector = new Vector(50, 0, 0);
        dotProduct = vector.dotProduct(secondVector);

        expect(dotProduct).not.toBeLessThan(0);
    });

    it("Result is negative", function () {
        var secondVector, dotProduct;
        secondVector = new Vector(0, -50, 1);
        dotProduct = vector.dotProduct(secondVector);

        expect(dotProduct).toBeLessThan(0);
    });
});

这是“this”所指的第一个向量。

housePosition = new Vector(150, 100);

这是函数应该做什么的描述:

"你的 Vector 对象应该有一个 'dotProduct' 函数,该函数将单个 Vector 对象作为其参数。该函数应该返回一个标量值,它是 'this' Vector 和参数 Vector 的点积的结果"

我这样做对吗? 我怎样才能让它通过这些测试?只需将向量设为负数,或者我需要做些什么。

【问题讨论】:

  • 考虑到您有 2 个 dotProduct 定义,并且在每个定义中您都有 secondVector 硬编码(并且没有传入参数),您的代码可能是错误的。您可能在 set/get 方法中遇到问题,这些问题实际上会使您的计算始终为 0。
  • 即使其中一个被注释掉了,我仍然只有正确工作的“积极”。
  • 在它们中的每一个中,您都有 secondVecor 的硬编码值,并且您没有使用任何输入参数。照原样,这些方法不以任何方式依赖于输入。此外,您没有显示您的 setX 和 getX 方法。
  • 现在添加了我的整个矢量对象
  • 他们接受了使用 chutzpah 扩展的测试,它使用 jasmine

标签: javascript vector jasmine dot-product


【解决方案1】:

好的。你的 set 和 get 方法没问题。让我们看看您的 dotProduct 方法。

Vector.prototype.dotProduct = function () {
    var secondVector = new Vector(100, 400);

    return new Vector(this.getX() * secondVector.getX() +
        this.getY() * secondVector.getY() * Math.acos(90));
};

1) 它不使用任何输入参数!

2) 它使用硬编码向量 (100, 400) 作为乘数

3) 它返回一个新的向量对象,该对象由一个....的单个参数构造而成

this.getX() * secondVector.getX() +
        this.getY() * secondVector.getY() * Math.acos(90)

4) Math.acos(90) 返回 NaN,因为 cos 的值介于 1 和 -1 之间。即使你想得到一个角度的余弦,它仍然是错误的,因为 Math.cos 的角度单位是弧度而不是度数。

所以任何 dotProduct 调用的结果都是带有 mX:NaN 和 mY:undefined 的 Vector 对象。

顺便说一句,您注释掉的 dotProduct 方法看起来更像一个 dotProduct。您只需要返回数字,而不是向量。 (至少,您希望在单元测试中有一个数字)。所以我认为你正在寻找这样的东西:

Vector.prototype.dotProduct = function ( secondVector ) {
    return this.getX()*secondVector.getX() + this.getY()*secondVector.getY();
};

【讨论】:

  • 好吧,我不需要 Secondvector 变量,我需要返回一个值而不是一个向量。
  • 你需要一个正确的向量点积公式。此外,在编写测试之前使用浏览器开发人员控制台尝试和调试您的代码也很有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多