英文:
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("Draw triangle ", i, "indices: ", indices);
}
}
<s>
The bug was fixed by replacing:
for (i = [0:len(indices)-1:3]) {
with:
for (i = [0:len(indices)-3:3]) {
</s>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论