4

I have to add a triangle to a pixel array. Here's what I have :

  • An array representing pixels in this form : [r, g, b, a, r, g, b, a, ...]
  • pixel density
  • Image width & height
  • My triangle : {p0: {x: 5, y: 20}, p1: {x: 65, y: 220}, p2: {x: 10, y: 143}}
  • Triangle color {r: 100, g: 214, b: 45, a:200}

I am able to fill my image with this code:

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;
}

How can I compute which pixels need to be filled with the color ?

Thanks !

EDIT: maybe I should have developped a little bit. I managed to draw the points of the triangle, but I don't know how to fill the area. I would also like to take performance in consideration, and avoid looking at each pixels. Here's how I managed to draw the points:

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;
    }
  };
}
Spektre
  • 49,595
  • 11
  • 110
  • 380
Rylyn
  • 333
  • 3
  • 10
  • Or even just geometry – Nick is tired Mar 01 '18 at 10:24
  • Do you know you can use the canvas element to draw a triangle with three [`lineTo`](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/lineTo) calls, and then take the image bitmap from that with [`getImageData`](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/getImageData)? – trincot Mar 01 '18 at 11:26
  • 3
    You need to make a routine that fills horizontal segment `(y,x1,x2)` then use any algorithm of **triangle rasterization** – MBo Mar 01 '18 at 11:41
  • 1
    Possible duplicate of [Algorithm to fill triangle](https://stackoverflow.com/questions/39038505/algorithm-to-fill-triangle) – Spektre Mar 01 '18 at 12:37
  • your `pixels[]` organization does not make much sense usually 2D raster map is stored as 2D array of pixels not as a list... if your pixels are just encoded as 1D array so pixel x,y is accessed as `pixel[x+y*xs]` or `pixel[y+x*ys]` where `xs,ys` is the raster resolution you need to specify which it is ... MBo is right look at the duplicate I linked and also in the sublinks in there ... you should find there all you need to implement this **triangle rasterization**. – Spektre Mar 01 '18 at 13:40

1 Answers1

5

So I finally managed to get what I wanted. I did not know about triangle rasterization, thanks for the links.

Here's what I have :

A getIndex method, to find the index of x,y coordinates in the pixel array

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

Then a plot function, to fill x,y coordinates whith a color. I added alpha blending too.

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));
 }

I also needed to be able to draw lines:

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;
    }
  }
}

Then comes the rasterization, I have used the following link : 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;
  }
}

And voila, I can draw my triangles. It might not be rocket science, but I don't think that one can consider it as 'basic'...

Thanks for the help!

Rylyn
  • 333
  • 3
  • 10