6

JavaScriptでvector3の配列を作成しようとしている、openGLキューブの頂点、エッジ、面の構造を作成しようとしている、JavaScriptはまったく新しいが、HTML5がサポートしているので、JSは私が理解できる言語である必要があると感じています。 :)、

JSで構造体を宣言する方法と、それを配列型に実装する方法がわかりません。

私はこのようなものを持っていますが、それが正しいかどうかはわかりません。

var vector3 = (x=0,y=0,z=0);

しかし、それを配列にどのように使用しますか?

助けてくれてありがとう。

4

4 に答える 4

1

オブジェクトを作成します:

var vector3 = {
    x:0,
    y:0,
    z:0
};

次のようなコードを使用して、個々のフィールドにアクセスできます。

var tmp = vector3.x;

ベクトルにポイントを配置するには

var myPolygon = [
    {x: 3, y: 8, z: -8},
    {x: 3, y: 4, z: 10},
    {x: 9, y: 8, z: -8},
];

xこれを使ってベクトル型を書くこともできるので、、、yz毎回書く必要はありません。

var vec3 = {x:0,y:0,z:0};

var demoVec = vec3;
var demo2Vec = vec3;
demoVec.x+=demo2Vec.y;
于 2012-10-21T16:11:34.473 に答える
1

javascriptオブジェクトを使用します。

var vector1 = {x:0,y:0,z:0};
var vector2 = {x:10,y:0,z:0};

//example function to find scalar distance between two points
function distance(v1,v2){
    return Math.sqrt(Math.pow((v1.x-v2.x),2) + Math.pow((v1.y-v2.y),2) + Math.pow((v1.z-v2.z),2));
}
var d = distance(vector1,vector2); //returns 10
console.log(d);
于 2012-10-21T16:14:13.487 に答える
1

Tobias Springerのソリューションが好きですが、ユーティリティメソッドを使用してベクトルオブジェクトを作成することもできます。

Vector = function(x, y, z) {
  this._init(x, y, z);
};

Vector.prototype = {

  /**
   * Fixed Constructor.
   */
  constructor: Vector,

  x: null,

  y: null,

  z: null,

  /**
   * Add documentation!
   */
  _init: function(x, y, z) {
    this.x = x;
    this.y = y;
    this.z = z;
  },

  /**
   * Add documentation!
   */
  add: function(otherVector) {
    return new Vector(this.x + otherVector.x,
        this.y + otherVector.y, this.z + otherVector.z);
  },

  /**
   * Add documentation!
   */
  scalarProduct: function(otherVector) {
    return this.x * otherVector.x + this.y * otherVector.y
        + this.z * otherVector.z;
  },

  /**
   * From Asad's answer. Returns the distance between this vector
   * and <code>otherVector</code>.
   * @param otherVector {Vector}
   * @returns {Number}
   */
  distance: function(otherVector) {
    return Math.sqrt(Math.pow((this.x-otherVector.x),2)
        + Math.pow((this.y-otherVector.y),2)
        + Math.pow((this.z-otherVector.z),2));
  }
  // and so on....
};

したがって、次のように使用します。

var vector1 = new Vector (1, 1, 1);
var vector2 = new Vector (1, 0, 1);

var addedVector = vector1.add(vector2); // --> = (2, 1, 2)
var scalarProduct = vector1.scalarProduct(vector2); // --> = 2
于 2012-10-21T16:18:23.800 に答える
1

コンストラクターを書きます

function Vector(x,y,z){
    this.dimension = 0;
    if( undefined !== x ) this.dimension = 1, this.x = x || 0;
    else this.x = 0;
    if( undefined !== y ) this.dimension = 2, this.y = y || 0;
    else this.y = 0;
    if( undefined !== z ) this.dimension = 3, this.z = z || 0;
    else this.z = 0;
}
Vector.prototype = Object.create(null, {
    length: {
        get: function(){
            return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
        }
    },
    add : {
        value: function(v){
            var d = Math.max(this.dimension, v.dimension), x, y, z;
            if( d > 0 ) x = (this.x || 0) + (v.x || 0);
            if( d > 1 ) y = (this.y || 0) + (v.y || 0);
            if( d > 2 ) z = (this.z || 0) + (v.z || 0);
            return new Vector(x, y, z);
        }
    }
});

var vector3 = new Vector(0,0,0);
于 2012-10-21T16:29:07.783 に答える