Dart中的函数绘图仪错误(使用three.dart):中断异常:null对象没有方法' crossInto'

时间:2014-06-22 21:05:02

标签: math web plot 3d dart

计算在以下代码中完成:

var MIN = -10.0,
    MAX = 10.0,
    RANGE = MAX - MIN;

getColor(max, min, val) {
  var MIN_L = 40,
      MAX_L = 100;
  var color = new Color();
  var h = 0 / 240;
  var s = 80 / 240;
  var l = (((MAX_L - MIN_L) / (max - min)) * val) / 240;
  color.setHSL(h, s, l);
  return color;
}
initGraph() {
  var x = MIN,
      y = MIN,
      z = 0.0;
  initData() {
    var data = [];
    for (var i = MIN; i < MAX; i++) {
      var row = [];
      for (var j = MIN; j < MAX; j++) {
        double z = 2*( x * x + y * y);
        print('$z');
        row.add({
          x: x,
          y: y,
          z: z
        });
        y++;
      }
      data.add(row);
      x++;
    }
    return data;
  }

  var data = initData();
  var geometry = new Geometry();
  var colors = [];

  var RANGE = data.length,
      height = data[0].length;
  data.forEach((col) {
    col.forEach((val) {
      geometry.vertices.add(new Vector3(x.toDouble(), y.toDouble(), z.toDouble()));
      colors.add(getColor(2.5, 0, z.toDouble()));
    });
  });

  offset(x, y) {
    return x * RANGE + y;
  }

  for (var x = 0; x < RANGE - 1; x++) {
    for (var y = 0; y < height - 1; y++) {
      Vector3 vec0;
      Vector3 vec1;
      Vector3 n_vec;
      // one of two triangle polygons in one rectangle
      vec0 = (geometry.vertices[offset(x, y)] - geometry.vertices[offset(x + 1, y)]);
      vec1 = (geometry.vertices[offset(x, y)] - geometry.vertices[offset(x, y + 1)]);
      n_vec.crossInto(vec0, vec1).normalize();
      geometry.faces.add(new Face3(offset(x, y), offset(x + 1, y), offset(x, y + 1), n_vec, [colors[offset(x, y)], colors[offset(x + 1, y)], colors[offset(x, y + 1)]]));
      geometry.faces.add(new Face3(offset(x, y), offset(x, y + 1), offset(x + 1, y), n_vec.negate(), [colors[offset(x, y)], colors[offset(x, y + 1)], colors[offset(x + 1, y)]]));
      // the other one
      vec0 = (geometry.vertices[offset(x + 1, y)] - geometry.vertices[offset(x + 1, y + 1)]);
      vec1 = (geometry.vertices[offset(x, y + 1)] - geometry.vertices[offset(x + 1, y + 1)]);
      n_vec.crossInto(vec0, vec1).normalize();
      geometry.faces.add(new Face3(offset(x + 1, y), offset(x + 1, y + 1), offset(x, y + 1), n_vec, [colors[offset(x + 1, y)], colors[offset(x + 1, y + 1)], colors[offset(x, y + 1)]]));
      geometry.faces.add(new Face3(offset(x + 1, y), offset(x, y + 1), offset(x + 1, y + 1), n_vec.negate(), [colors[offset(x + 1, y)], colors[offset(x, y + 1)], colors[offset(x + 1, y + 1)]]));
    }
  }
  var material = new MeshLambertMaterial(vertexColors: VertexColors);
  var mesh = new Mesh(geometry, material);
  scene.add(mesh);
}

错误似乎出现在这一行:

  n_vec.crossInto(vec0, vec1).normalize();

这里的null对象是什么,我该如何解决?变量&#39; z&#39;造成这个问题?它首先显示为null,并导致类似的错误(&#39; *&#39;无法应用)并且我将其声明为double并且已经解决了。我也怀疑以下几行:

 data.forEach((col) {
    col.forEach((val) {
      geometry.vertices.add(new Vector3(x.toDouble(), y.toDouble(), z.toDouble()));

1 个答案:

答案 0 :(得分:0)

n_vec永远不会使用Vector3的实例进行初始化。需要在实例上调用crossInto,要么先创建实例:

Vector3 n_vec = new Vector3.zero();
...
n_vec.crossInto(vec0, vec1).normalize();

或者您使用cross方法,但它创建了Vector3的新实例(您可能希望避免新实例,而不是将变量移出循环):

n_vec = vec0.cross(vec1).normalize();