将三角形绘制到像素阵列

时间:2018-03-01 10:07:40

标签: javascript algorithm 2d rasterizing

我必须在像素数组中添加一个三角形。这就是我所拥有的:

  • 表示此形式像素的数组:[r,g,b,a,r,g,b,a,...]
  • 像素密度
  • 图像宽度&高度
  • 我的三角形:{p0:{x:5,y:20},p1:{x:65,y:220},p2:{x:10,y:143}}
  • 三角色{r:100,g:214,b:45,a:200}

我可以使用以下代码填充我的图片:

for (let i = 0; i < pixels.length; i+=4) {
  pixels[i] = color.r;
  pixels[i+1] = color.g;
  pixels[i+2] = color.b;
  pixels[i+3] = color.a;
}

如何计算需要用颜色填充哪些像素?

谢谢!

编辑:也许我应该发展一点点。我设法绘制了三角形的点,但我不知道如何填充该区域。我还想考虑性能,避免查看每个像素。以下是我设法得出分数的方法:

static getIndex(x, y, width) {
  return 4 * (width * y + x);
}

static drawTriangle(triangle, img) {
  for (let i = 0; i < triangle.points.length; i++) {
    let point = triangle.points[i];
    for (let a = -5; a < 6; a++) {
      let idx = this.getIndex(point.x + a, point.y, img.width);
      img.pixels[idx] = triangle.color.r;
      img.pixels[idx + 1] = triangle.color.g;
      img.pixels[idx + 2] = triangle.color.b;
      img.pixels[idx + 3] = triangle.color.a;

      idx = this.getIndex(point.x, point.y + a, img.width);
      img.pixels[idx] = triangle.color.r;
      img.pixels[idx + 1] = triangle.color.g;
      img.pixels[idx + 2] = triangle.color.b;
      img.pixels[idx + 3] = triangle.color.a;
    }
  };
}

1 个答案:

答案 0 :(得分:3)

所以我终于设法得到了我想要的东西。我不知道三角形光栅化,感谢链接。

这就是我所拥有的:

getIndex方法,用于查找像素数组中x,y坐标的索引

static getIndex(x, y, width) {
  return 4 * (width * y + x);
}

然后是绘图功能,用一种颜色填充x,y坐标。我也添加了alpha混合。

static plot(x, y, color, img) {
  let idx = this.getIndex(x, y, img.width);

  let added = [color.r, color.g, color.b, map(color.a, 0, 255, 0, 1)];
  let base = [img.pixels[idx], img.pixels[idx + 1], img.pixels[idx + 2], map(img.pixels[idx + 3], 0, 255, 0, 1)];
  let a01 = 1 - (1 - added[3]) * (1 - base[3]);

  img.pixels[idx + 0] = Math.round((added[0] * added[3] / a01) + (base[0] * base[3] * (1 - added[3]) / a01)); // red
  img.pixels[idx + 1] = Math.round((added[1] * added[3] / a01) + (base[1] * base[3] * (1 - added[3]) / a01)); // green
  img.pixels[idx + 2] = Math.round((added[2] * added[3] / a01) + (base[2] * base[3] * (1 - added[3]) / a01)); // blue
  img.pixels[idx + 3] = Math.round(map(a01, 0, 1, 0, 255));
 }

我还需要能够绘制线条:

static line(x0, y0, x1, y1, img, color) {
  x0 = Math.round(x0);
  y0 = Math.round(y0);
  x1 = Math.round(x1);
  y1 = Math.round(y1);
  var dx = Math.abs(x1 - x0);
  var dy = Math.abs(y1 - y0);
  var sx = x0 < x1 ? 1 : -1;
  var sy = y0 < y1 ? 1 : -1;
  var err = dx - dy;

  let itt = 0;
  while (true) {
    this.plot(x0, y0, color, img);
    itt ++;
    if (x0 == x1 && y0 == y1) break;
    var e2 = 2 * err;
    if (e2 > -dy) {
      err -= dy;
      x0 += sx;
    }
    if (e2 < dx) {
      err += dx;
      y0 += sy;
    }
  }
}

然后是光栅化,我使用了以下链接:http://www.sunshine2k.de/coding/java/TriangleRasterization/TriangleRasterization.html

static fillTriangle(triangle, img) {
  let vertices = Array.from(triangle.points);
  vertices.sort((a, b) => a.y > b.y);
  if (vertices[1].y == vertices[2].y) {
    this.fillBottomFlatTriangle(vertices[0], vertices[1], vertices[2], img, triangle.color);
  } else if (vertices[0].y == vertices[1].y) {
    this.fillTopFlatTriangle(g, vertices[0], vertices[1], vertices[2], img, triangle.color);
  } else {
    let v4 = {
      x: vertices[0].x + float(vertices[1].y - vertices[0].y) / float(vertices[2].y - vertices[0].y) * (vertices[2].x - vertices[0].x),
      y: vertices[1].y
    };
    this.fillBottomFlatTriangle(vertices[0], vertices[1], v4, img, triangle.color);
    this.fillTopFlatTriangle(vertices[1], v4, vertices[2], img, triangle.color);
  }
}

static fillBottomFlatTriangle(v1, v2, v3, img, color) {
  let invslope1 = (v2.x - v1.x) / (v2.y - v1.y);
  let invslope2 = (v3.x - v1.x) / (v3.y - v1.y);

  let curx1 = v1.x;
  let curx2 = v1.x;

  for (let scanlineY = v1.y; scanlineY <= v2.y; scanlineY++) {
    this.line(curx1, scanlineY, curx2, scanlineY, img, color);
    curx1 += invslope1;
    curx2 += invslope2;
  }
}

static fillTopFlatTriangle(v1, v2, v3, img, color) {
  let invslope1 = (v3.x - v1.x) / (v3.y - v1.y);
  let invslope2 = (v3.x - v2.x) / (v3.y - v2.y);

  let curx1 = v3.x;
  let curx2 = v3.x;

  for (let scanlineY = v3.y; scanlineY > v1.y; scanlineY--) {
    this.line(curx1, scanlineY, curx2, scanlineY, img, color);
    curx1 -= invslope1;
    curx2 -= invslope2;
  }
}

瞧,我可以画三角形。它可能不是火箭科学,但我不认为人们可以认为它是“基本的”......

感谢您的帮助!