简体   繁体   English

处理(Java / Android)尝试使用for循环绘制形状

[英]Processing (Java/Android) Trying to draw shape using for loop

I am trying to draw some triangles in 3D using Processing for Android. 我正在尝试使用Android的Processing在3D中绘制一些三角形。 When I hard code the triangles (say, a sample of 10 triangles), they are drawn perfectly. 当我对三角形进行硬编码(例如,有10个三角形的样本)时,它们绘制得很完美。 However, when I try to draw those same triangles using a for loop, only one triangle is drawn. 但是,当我尝试使用for循环绘制相同的三角形时,只会绘制一个三角形。 ( I am trying to draw 100 connected triangles, so hardcoding is not a really good solution) (我试图绘制100个相连的三角形,所以硬编码不是一个很好的解决方案)

Here is my for loop within the draw() function: 这是我在draw()函数中的for循环:

    for(int j=0;j<triangles.size();j++){
        beginShape(TRIANGLES);
    vertex(triangles.get(j)[0],triangles.get(j)[1],triangles.get(j)[2]); //triangle pt 1
    vertex(triangles.get(j)[3],triangles.get(j)[4],triangles.get(j)[5]); //triangle pt 2
    vertex(triangles.get(j)[6],triangles.get(j)[7],triangles.get(j)[8]); //triangle pt 3
    endShape(CLOSE);
    }

triangles is an ArrayList of float[9], where float[0-2] represents the x,y and z coordinates of the first point of the first triangle, float[3-5] represents the x,y and z coords of the second point of the first triangle and so on. 三角形是float [9]的ArrayList,其中float [0-2]表示第一个三角形的第一个点的x,y和z坐标,float [3-5]表示坐标的x,y和z坐标第一个三角形的第二个点,依此类推。

Visually it can be represented as: 在视觉上,它可以表示为:

ArrayList{
Triangle1.firstpoint.x(),Triangle1.firstpoint.y(),Triangle1.firstpoint.z(), Triangle1.secondpoint.x()...Triangle1.thirdpoint.x()...
Triangle2.firstpoint.x()...
and so on...
}

j represents the triangle number. j表示三角形数。 (first tri, second tri etc) (第一三,第二三等)

When I try to hardcode 10 triangles, however, it works perfectly. 但是,当我尝试对10个三角形进行硬编码时,它可以完美工作。

Hardcoding like this works perfectly: 这样的硬编码可以完美地工作:

    vertex((float) 12.0,(float)123.6016745697409,(float)13.154318796014476);
    vertex( (float)0.0,(float)197.68424385921847,(float)23.124972474732054);
    vertex((float) 20.0,(float)143.86712094263626,(float)44.91428537210794);

    vertex((float) 0.0,(float)197.68424385921847,(float)23.124972474732054);
    vertex( (float)24.0,(float)214.61268970017602,(float)45.76677898884916);
    vertex( (float)32.0,(float)188.10247423402564,(float)28.491849521501333);

    vertex((float) 56.0,(float)88.25218560204328,(float)12.036878675294194);
    vertex((float) 60.0,(float)144.49729617140488,(float)46.869886728176354);
    vertex((float) 76.0,(float)101.60603981125132,(float)7.27525015602069);

    vertex( (float)60.0,(float)144.49729617140488,(float)46.869886728176354);
    vertex( (float)64.0,(float)184.21835869332358,(float)3.9402252833719453);
    vertex( (float)76.0,(float)101.60603981125132,(float)7.27525015602069);

    vertex((float) 4.0,(float)268.3209514804444,(float)18.52950131584495);
    vertex( (float)52.0,(float)349.93516427695124,(float)8.772515956684678);
    vertex( (float)72.0,(float)338.2777711567361,(float)21.563995998820793);

Putting beginShape(TRIANGLES) and endShape() outside of the for loop has no effect. 将beginShape(TRIANGLES)和endShape()放在for循环之外无效。 Really puzzled by what's going on here :/ 真的为这里发生的事情感到困惑:/

Silly me! 傻我! It turns out that every triangle in the triangles ArrayList had the same value, thus in effect producing only 1 triangle during the display! 事实证明,三角形ArrayList中的每个三角形都具有相同的值,因此实际上在显示期间仅产生1个三角形!

The issue was because in transferring values over to the triangles ArrayList, I did: 问题是因为在将值转移到三角形ArrayList时,我做到了:

            float[] trianglesi=new float[9];
        for (int tt=0; tt<ntri; tt++)
    {

        trianglesi[0]=(float) points[triangles[tt].p1].x;
        trianglesi[1]=(float) points[triangles[tt].p1].y;
        trianglesi[2]=(float) points[triangles[tt].p1].z;

        trianglesi[3]=(float) points[triangles[tt].p2].x;
        trianglesi[4]=(float) points[triangles[tt].p2].y;
        trianglesi[5]=(float) points[triangles[tt].p2].z;

        trianglesi[6]=(float) points[triangles[tt].p3].x;
        trianglesi[7]=(float) points[triangles[tt].p3].y;
        trianglesi[8]=(float) points[triangles[tt].p3].z;

        triangles2.add(trianglesi);
    }

(Here triangles2 refer to the triangles ArrayList, and trianglesi refer to a "unit" triangle.) (这里,triangles2指的是三角形ArrayList,trianglesi指的是“单位”三角形。)

However, I had to initiate the trianglesi within the for loop! 但是,我必须在for循环内启动trianglesi!

Thanks laalto for leading me to relook at my triangles implementation, though. 不过,感谢laalto带领我重新审视了我的三角形实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM