gpt4 book ai didi

javascript - 绘制一个三 Angular 形到像素数组

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:10:49 25 4
gpt4 key购买 nike

我必须向像素阵列添加一个三 Angular 形。这是我所拥有的:

  • 以这种形式表示像素的数组:[r, g, b, a, r, g, b, a, ...]
  • 像素密度
  • 图片宽度和高度
  • 我的三 Angular 形:{p0: {x: 5, y: 20}, p1: {x: 65, y: 220}, p2: {x: 10, y: 143}}
  • 三 Angular 形颜色{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;
}

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

谢谢!

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

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

最佳答案

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

这是我所拥有的:

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

瞧,我可以画三 Angular 形了。这可能不是火箭科学,但我认为不能将其视为“基础”...

感谢您的帮助!

关于javascript - 绘制一个三 Angular 形到像素数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49047229/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com