gpt4 book ai didi

Javascript 矩阵乘以标量

转载 作者:行者123 更新时间:2023-12-03 11:23:58 24 4
gpt4 key购买 nike

我有一个名为 Cube 的对象。是这样设置的

//Cube object
function Cube(vertices, color, scale)
{
//this.vertices = vertices;
this.setColor(color);
this.setScale(vertices, 1);
}

我注释掉了 //this.vertices = vertices; 因为我不确定是否必须在此处设置顶点或在 setScale() 函数中设置它们。
我想在矩阵上设置一个比例。矩阵为:

var verts = [
// Front face
-1.0, -1.0, 1.0,
1.0, -1.0, 1.0,
1.0, 1.0, 1.0,
-1.0, 1.0, 1.0,

// Back face
-1.0, -1.0, -1.0,
-1.0, 1.0, -1.0,
1.0, 1.0, -1.0,
1.0, -1.0, -1.0,

// Top face
-1.0, 1.0, -1.0,
-1.0, 1.0, 1.0,
1.0, 1.0, 1.0,
1.0, 1.0, -1.0,

// Bottom face
-1.0, -1.0, -1.0,
1.0, -1.0, -1.0,
1.0, -1.0, 1.0,
-1.0, -1.0, 1.0,

// Right face
1.0, -1.0, -1.0,
1.0, 1.0, -1.0,
1.0, 1.0, 1.0,
1.0, -1.0, 1.0,

// Left face
-1.0, -1.0, -1.0,
-1.0, -1.0, 1.0,
-1.0, 1.0, 1.0,
-1.0, 1.0, -1.0
];

我用来设置比例的函数是这样的:

Cube.prototype.setScale = function(vertices, scale) 
{
var length = vertices.length;
for( var i = 0; i < length; i++)
{
//alert("before "+ vertices[i]+ " i "+ i);
vertices[i] *= scale;
//alert("after "+ vertices[i]);
}

我认为通过这样做,for循环应该获取矩阵长度并启动for循环。
在这个 for 循环中,我将获取 i 处的顶点并将其乘以比例。
当我这样做时,for 循环会重复。 IE。当我达到 72 时,循环不会停止。

最佳答案

I commented out //this.vertices = vertices; because im not sure if I have to set vertices here

您需要将其设置在那里或构造函数的末尾。您的 setScale 函数与 vertices 数组的存储位置很好地分离,我会保持这种方式。但您可能会考虑将其重命名为 scaleVertices()

When I do it like this though the for loop will reiterate. I.e. when i hits 72 the loop does not stop.

真的吗?这很奇怪。我肯定错过了什么;代码看起来是正确的。

不要使用alert(),顺便说一句,调试任何东西都会让你的生活变得一团糟。使用 console.log() 会更好。

关于Javascript 矩阵乘以标量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26995548/

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