只绘制了一个三角形,而没有绘制第二个。

huangapple go评论96阅读模式
英文:

Only one triangle is drawn, not the second one

问题

I have this code to draw two triangles. But it only draws the first triangle, not the second one. I cannot figure out the bug.

module draw_triangles(indices, vertices, triangle_color=[1, 1, 1, 0.4]) {
  assert(len(indices) % 3 == 0, "indices must have a length that is a multiple of 3");

  for (i = [0:len(indices)-1:3]) {
    triangle(
      vertices[indices[i]],
      vertices[indices[i+1]],
      vertices[indices[i+2]],
      triangle_color
    );
  }
}

module triangle(p1, p2, p3, triangle_color) {
  color(triangle_color) polyhedron(points=[p1, p2, p3], faces=[[0, 1, 2]]);
}

edges = [
    [80/2, 0, 0],
    [80, 80/2, 0],
    [80/2, 80, 0],
    [0, 80/2, 0],
    [80/2, 0, 80],
    [80, 80/2, 80],
    [80/2, 80, 80],
    [0, 80/2, 80],
    [0, 0, 80/2],
    [80, 0, 80/2],
    [80, 80, 80/2],
    [0, 80, 80/2],
];

triangle_table = [1, 8, 3, 9, 8, 1];
draw_triangles(indices = triangle_table, vertices = edges, triangle_color=[0, 1, 1, .4]);

只绘制了一个三角形,而没有绘制第二个。

英文:

I have this code to draw two triangles. But it only draws the first triangle, not the second one. I cannot figure out the bug.

module draw_triangles(indices, vertices, triangle_color=[1, 1, 1, 0.4]) {
  assert(len(indices) % 3 == 0, "indices must have a length that is a multiple of 3");

  for (i = [0:len(indices)-1:3]) {
    triangle(
      vertices[indices[i]],
      vertices[indices[i+1]],
      vertices[indices[i+2]],
      triangle_color
    );
  }
}

module triangle(p1, p2, p3, triangle_color) {
  color(triangle_color) polyhedron(points=[p1, p2, p3], faces=[[0, 1, 2]]);
}

edges = [
    [80/2, 0, 0],
    [80, 80/2, 0],
    [80/2, 80, 0],
    [0, 80/2, 0],
    [80/2, 0, 80],
    [80, 80/2, 80],
    [80/2, 80, 80],
    [0, 80/2, 80],
    [0, 0, 80/2],
    [80, 0, 80/2],
    [80, 80, 80/2],
    [0, 80, 80/2],
];

triangle_table = [1, 8, 3, 9, 8, 1];
draw_triangles(indices = triangle_table, vertices = edges, triangle_color=[0, 1, 1, .4]);

只绘制了一个三角形,而没有绘制第二个。

答案1

得分: 1

你在编写for循环时犯了一个错误:应该是[start:step:end]而不是[start:end:step],因此你必须将你的for (i = [0:len(indices)-1:3])替换为for (i = [0:3:len(indices)-1])

英文:

You made a mistake when you wrote your for loop : it's [start:step:end] not [start:end:step] so you must replace your for (i = [0:len(indices)-1:3]) by for (i = [0:3:len(indices)-1])

答案2

得分: 0

避免循环 step

我已经通过完全不使用循环 step 来解决了我的问题。

通过使用嵌套数组来彻底修复了这个错误:

triangle_table = [
    [ 1, 8, 3 ],
    [ 9, 8, 1 ],
];

draw_triangles(triangle_table);

循环实现如下:

module draw_triangles(triangle_table)
{
    for (i = [0:len(triangle_table) - 1])
    {
        indices = triangle_table[i];

        p0 = edges[indices[0]];
        p1 = edges[indices[1]];
        p2 = edges[indices[2]];

        color([ 1, 1, 1, 0.4 ]) polyhedron(points = [ p0, p1, p2 ], faces = [[ 0, 1, 2 ]]);

        echo("绘制三角形 ", i, " 索引: ", indices);
    }
}

<s>

通过将以下内容替换:

  for (i = [0:len(indices)-1:3]) {

替换为:

  for (i = [0:len(indices)-3:3]) {

</s>

只绘制了一个三角形,而没有绘制第二个。

英文:

Avoid loop step

I already solved my problem by not using the loop step at all.

The bug was robustly fixed by using a nested array like this:

triangle_table = [
    [ 1, 8, 3 ],
    [ 9, 8, 1 ],
];

draw_triangles(triangle_table);

The loop is implemented like this:

module draw_triangles(triangle_table)
{
    for (i = [0:len(triangle_table) - 1])
    {
        indices = triangle_table[i];

        p0 = edges[indices[0]];
        p1 = edges[indices[1]];
        p2 = edges[indices[2]];

        color([ 1, 1, 1, 0.4 ]) polyhedron(points = [ p0, p1, p2 ], faces = [[ 0, 1, 2 ]]);

        echo(&quot;Draw triangle &quot;, i, &quot;indices: &quot;, indices);
    }
}

<s>

The bug was fixed by replacing:

  for (i = [0:len(indices)-1:3]) {

with:

  for (i = [0:len(indices)-3:3]) {

</s>

只绘制了一个三角形,而没有绘制第二个。

huangapple
  • 本文由 发表于 2023年5月14日 14:37:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76246164.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定