javascript commonclass



function isNumber(n){
  if ( typeof(n) === 'number' && Number.isFinite(n) ) {
    return true;
  }
  return false;
}

function isNumberAllowString(n) {
  const type = typeof(n);
  if ( type === 'number' && Number.isFinite(n) ) {
    return true;
  }
  if ( type === 'string' && n.trim() !== '' && Number.isFinite(n - 0) ) {
    return true;
  }
  return false;
}



class Col {
  constructor(r, g, b, a) {
    this.r = 0;
    this.g = 0;
    this.b = 0;
    this.a = 0;
    if((isNumber(r) == true)&&(4 <=r.length)){
      this.r = Number(r[0]);
      this.g = Number(r[1]);
      this.b = Number(r[2]);
      this.a = Number(r[3]);
      return this;
    }
    if(a != undefined){
      this.r = Number(r);
      this.g = Number(g);
      this.b = Number(b);
      this.a = Number(a);
      return this;
    }
    return this;
  }
}

class Vec2 {
  constructor(x, y) {
    this.typename = "Vec2";
    this.x = 0;
    this.y = 0;
    if(x != undefined){
      if(x.typename == "Vec2"){
        this.x = x.x;
        this.y = x.y;
        return this;
      }
      else if((isNumber(x) == true)&&(2 <=x.length)){
        this.x = Number(x[0]);
        this.y = Number(x[1]);
        return this;
      }
      else if(y != undefined){
        this.x = Number(x);
        this.y = Number(y);
        return this;
      }
    }
    return this;
  }
}

class Vec3 {
  constructor(x, y, z) {
    this.typename = "Vec3";
    this.x = 0;
    this.y = 0;
    this.z = 0;
    if(x != undefined){
      if(x.typename == "Vec3"){
        this.x = x.x;
        this.y = x.y;
        this.z = x.z;
        return this;
      }
      else if((isNumber(x) == true)&&(3 <=x.length)){
        this.x = Number(x[0]);
        this.y = Number(x[1]);
        this.z = Number(x[2]);
        return this;
      }
      else if(z != undefined){
        this.x = Number(x);
        this.y = Number(y);
        this.z = Number(z);
        return this;
      }
    }
    return this;
  }
}

class Vec4 {
  constructor(x, y, z, w) {
    this.typename = "Vec4";
    this.x = 0;
    this.y = 0;
    this.z = 0;
    this.w = 0;
    if(x != undefined){
      if(x.typename == "Vec4"){
        this.x = x.x;
        this.y = x.y;
        this.z = x.z;
        this.w = x.w;
        return this;
      }
      else if((isNumber(x) == true)&&(4 <=x.length)){
        this.x = Number(x[0]);
        this.y = Number(x[1]);
        this.z = Number(x[2]);
        this.w = Number(x[3]);
        return this;
      }
      else if(w != undefined){
        this.x = Number(x);
        this.y = Number(y);
        this.z = Number(z);
        this.w = Number(w);
        return this;
      }
    }
    return this;
  }
}

class Rectangle {
  constructor(x, y, w, h) {
    this.typename = "Rectangle";
    this.x = 0;
    this.y = 0;
    this.w = 0;
    this.h = 0;
    if(x != undefined){
      if(x.typename == "Rectangle"){
        this.x = x.x;
        this.y = x.y;
        this.w = x.w;
        this.h = x.h;
        return this;
      }
      else if((isNumber(x) == true)&&(4 <=x.length)){
        this.x = Number(x[0]);
        this.y = Number(x[1]);
        this.w = Number(x[2]);
        this.h = Number(x[3]);
        return this;
      }
      else if(h != undefined){
        this.x = Number(x);
        this.y = Number(y);
        this.w = Number(w);
        this.h = Number(h);
        return this;
      }
    }
    return this;
  }

  create(x, y){
    var l = x.x;
    var r = y.x;
    var u = x.y;
    var d = y.y;
    if(r < l) { l = y.x; r = x.x; }
    if(d < u) { u = y.y; d = x.y; }
    this.x = l;
    this.y = u;
    this.w = r - l;
    this.h = d - u;

    return this;
  }
}




構築方法は

Vec4 construction
1.
var v4 = new Vec4(1,2,3,4);
2.
var v4 = new Vec4([1,2,3,4]);
3.
var v4 = new Vec4(1,2,3,4);
var nv4 = new Vec4(v4);


Rectangle construction
1.
var r = new Rectangle(1,2,3,4);
2.
var r = new Rectangle([1,2,3,4]);
3.
var r= new Rectangle(1,2,3,4);
var nr = new Rectangle(r);
4.
var v2a = new Vec2(1,2);
var v2b = new Vec2(3,4);
var r = new Rectangle();
r = r.create(v2a, v2b);
このような感じです

xy座標で1ビット移動する処理の場合は
var nDirAdd4 = { 'null' : new Vec2(0, 0), 'up' : new Vec2(0, 1), 'left' : new Vec2(-1, 0), 'right' : new Vec2(1, 0), 'down' : new Vec2(0, -1)};
var v2 = new Vec2(1,2);
v2.x = v2.x + nDirAdd4['up'].x;
v2.y = v2.y + nDirAdd4['up'].y;
こんな感じに連想配列テーブルを使うと便利です
そのテストコード




後は好きに使ってください





 ■■■ 3Dプログラミング入門講座 ■■■ 
NAS6LIB
ポリゴンテスト解説・x3dom使い方
その1:ベクトル
その2:行列
その3:クォータニオン
その4:基本総復習・実践
その5:応用その1・メッシュアニメーション、動的テクスチャ
その6:応用その2・GLSL、カスタムシェーダー、キーボード、ファイル
その7:応用その3・ゲームプログラミング、タグの動的追加
その8:応用その4・GLSL、シェーダー、その2
その9:物理演算その1・電卓で相対性理論を解く方法
その10:物理演算その2・相対性理論的ニュートン力学
その11:物理演算その3・ケプラー方程式で惑星軌道シミュレーターを作る

その12:物理演算その4・ルンゲクッタ法で作った 相対性理論的ニュートン力学物理エンジンで惑星軌道シミュレーターを作る

その13:経路探索(A*:A-STAR)&巡回セールスマン問題 : 巨大サイズ : くろにゃんこ

その14:プログラミングにおける配列テーブルテクニック
その15:javascriptのクラス活用法
その16:透視射影公式テスト

その17:ケプラー方程式カプセルライブラリ使用法
その18:CSVファイル処理
その19:物理演算その5・重力多体問題
その20:同次座標について(3D座標系の基本の基本)
その21:おさらいコモンクラスの宣言
その22:物理エンジンライブラリ解説(ケプラー方程式・ルンゲクッタ・相対論的万有引力)


 ■■■ THREE.JSプログラミング講座 ■■■ 
THREE.JSテスト解説・THREE.JS使い方
THREE.JS examplesをいじってみた(フレネル反射透過シェーダー)

THREE.JS (半透明シェーダー)

THREE.JS 3D演算で必要な計算(具体例)★とても重要★
THREE.JS THREE-VRM をいじってみた



<<prev 同次座標について : コモンクラスの宣言 next>>






戻る