Sierpinski三角形:程序运行不正常

时间:2014-05-08 22:02:53

标签: c

我有一个生成Sierpinski三角形的代码,它不能按照我想要的方式工作。似乎计算机只读取第三个子三角形(顶部)和左下部三角形的坐标,完全忽略第二个(右下角)!结果是我只有一半的初始三角形。如果他们的索引不同(一个是m而另一个是p),为什么会发生这种情况呢?如何更改它以便读取所有三个子三角形?我的代码用C代码:Blocks编写。

#include <stdio.h>

#define NTRIANGULOS (1<<3)

struct Triangulo
{
    float xA,yA;
    float xB,yB;
    float xC,yC;
};

int main(int argc, char *argv[])
{
    struct Triangulo triangulo[NTRIANGULOS];
    int n,m,p,ntriangulos;

    FILE*fout;
    fout=fopen("tri.txt","w");

    triangulo[0].xA = 0;
    triangulo[0].yA = 0;
    triangulo[0].xB = 1;
    triangulo[0].yB = 0;
    triangulo[0].xC = 0.5;
    triangulo[0].yC = 1;
    ntriangulos=1;

    do{

        for(n=ntriangulos-1; n>=0&&ntriangulos<NTRIANGULOS; n--){
            m=ntriangulos;

            /* segundo triangulo */
            triangulo[m].xA=triangulo[n].xA + (triangulo[n].xB - triangulo[n].xA)/2 ;
            triangulo[m].yA=triangulo[n].yA;

            triangulo[m].xB=triangulo[n].xB;
            triangulo[m].yB=triangulo[n].yB;

            triangulo[m].xC=triangulo[n].xA + 3*(triangulo[n].xB - triangulo[n].xA)/4;
            triangulo[m].yC=triangulo[n].yA + (triangulo[n].yC- triangulo[n].yA)/2 ;


            p=ntriangulos;
            /* tercer triangulo */
            triangulo[p].xA=triangulo[n].xA + (triangulo[n].xB - triangulo[n].xA)/4;
            triangulo[p].yA=triangulo[n].yA + (triangulo[n].yC - triangulo[n].yA)/2;

            triangulo[p].xB=triangulo[n].xA + 3*(triangulo[n].xB -triangulo[n].xA)/4;
            triangulo[p].yB=triangulo[n].yA + (triangulo[n].yC -triangulo[n].yB)/2;

            triangulo[p].xC= triangulo[n].xC;
            triangulo[p].yC= triangulo[n].yC;


            /* primer triangulo - inicial */

            triangulo[n].xC=triangulo[n].xA + (triangulo[n].xB - triangulo[n].xA)/4;
            triangulo[n].yC=triangulo[n].xA +(triangulo[n].yC - triangulo[n].yA)/2;

            triangulo[n].xB=triangulo[n].xA + (triangulo[n].xB - triangulo[n].xA)/2;
            triangulo[n].yB=triangulo[n].yB;

            triangulo[n].xA=triangulo[n].xA;
            triangulo[n].yA=triangulo[n].yA;

            ntriangulos++;
        }

    } while(ntriangulos<NTRIANGULOS);

    for(n=0;n<ntriangulos;n++)
    {
        fprintf(fout,"%g\t%g\n%g\t%g\n%g\t%g\n", triangulo[n].xA,  triangulo[n].yA, 
                triangulo[n].xB, triangulo[n].yB, triangulo[n].xC, triangulo[n].yC);
    }

    fclose(fout);
    return(0);
}

1 个答案:

答案 0 :(得分:1)

这一行

p=ntriangulos;

应该是

p=ntriangulos+1;

此外,你编写for循环的方式,它将在第一次循环后停止。

在第一次迭代中,

n = 0

在第二次迭代中,

n = -1

n>=0&&ntriangulos<NTRIANGULOS

时,表达式n = -1的计算结果为false

<强>更新

这是您的计划的修订版。我试图在评论中解释一些核心逻辑。

#include <stdio.h>

#define NTRIANGULOS 81
// (1<<3)


struct Triangulo
{
   float xA,yA;
   float xB,yB;
   float xC,yC;
};

int main(int argc, char *argv[])
{
   struct Triangulo triangulo[NTRIANGULOS];
   int n,m,p,ntriangulos;

   FILE*fout;
   fout=fopen("tri.txt","w");

   triangulo[0].xA = 0;
   triangulo[0].yA = 0;
   triangulo[0].xB = 1;
   triangulo[0].yB = 0;
   triangulo[0].xC = 0.5;
   triangulo[0].yC = 1;
   ntriangulos=1;

   // In the first round, ntriangulos = 1
   // We have to create 3 triangles out of them.
   // In the second round, ntriangulos = 3
   // We have to create 9 triangles out of them.
   // In the third round, ntriangulos = 9
   // We have to create 27 triangles out of them.

   // We need to stop the iteration when ntriangulos*3 > NTRIANGULOS

   while(3*ntriangulos <= NTRIANGULOS)
   {
      // The loop can also be:
      // for( n = 0 n < ntriangulos; n++)
      for(n=ntriangulos-1; n >= 0 ; n--)
      {
         // When ntriangulos = 3
         // The additional triangles need to be
         // added to locations with index 3,4,5, and 6.
         // When n = 0, we need to have m = 3, p = 4.
         // When n = 1, we need to have m = 5, p = 6.

         m=ntriangulos+n*2;
         p=ntriangulos+n*2+1;

         /* segundo triangulo */
         triangulo[m].xA=triangulo[n].xA + (triangulo[n].xB - triangulo[n].xA)/2 ;
         triangulo[m].yA=triangulo[n].yA;

         triangulo[m].xB=triangulo[n].xB;
         triangulo[m].yB=triangulo[n].yB;

         triangulo[m].xC=triangulo[n].xA + 3*(triangulo[n].xB - triangulo[n].xA)/4;
         triangulo[m].yC=triangulo[n].yA + (triangulo[n].yC- triangulo[n].yA)/2 ;

         /* tercer triangulo */
         triangulo[p].xA=triangulo[n].xA + (triangulo[n].xB - triangulo[n].xA)/4;
         triangulo[p].yA=triangulo[n].yA + (triangulo[n].yC - triangulo[n].yA)/2;

         triangulo[p].xB=triangulo[n].xA + 3*(triangulo[n].xB -triangulo[n].xA)/4;
         triangulo[p].yB=triangulo[n].yA + (triangulo[n].yC -triangulo[n].yB)/2;

         triangulo[p].xC= triangulo[n].xC;
         triangulo[p].yC= triangulo[n].yC;


         /* primer triangulo - inicial */

         triangulo[n].xC=triangulo[n].xA + (triangulo[n].xB - triangulo[n].xA)/4;
         triangulo[n].yC=triangulo[n].xA +(triangulo[n].yC - triangulo[n].yA)/2;

         triangulo[n].xB=triangulo[n].xA + (triangulo[n].xB - triangulo[n].xA)/2;
         triangulo[n].yB=triangulo[n].yB;

         triangulo[n].xA=triangulo[n].xA;
         triangulo[n].yA=triangulo[n].yA;
      }

      // After we have gone through dividing 
      // the triangles, the number of triangles have 
      // been tripled.
      ntriangulos *= 3;
   }

   for(n=0;n<ntriangulos;n++)
   {
      fprintf(fout,"(%g\t%g) (%g\t%g) (%g\t%g)\n",
              triangulo[n].xA, triangulo[n].yA, 
              triangulo[n].xB, triangulo[n].yB,
              triangulo[n].xC, triangulo[n].yC);
   }

   fclose(fout);
   return(0);
}

更新2

您的代码中可能存在计算三角形的逻辑错误。这是我能理解的版本。

      for(n=ntriangulos-1; n >= 0 ; n--)
      {
         // When ntriangulos = 3
         // The additional triangles need to be
         // added to locations with index 3,4,5, and 6.
         // When n = 0, we need to have m = 3, p = 4.
         // When n = 1, we need to have m = 5, p = 6.

         m=ntriangulos+n*2;
         p=ntriangulos+n*2+1;

         x1 = triangulo[n].xA;
         x2 = triangulo[n].xB;
         x3 = triangulo[n].xC;

         y1 = triangulo[n].yA;
         y2 = triangulo[n].yB;
         y3 = triangulo[n].yC;

         x12 = 0.5*(x1+x2);
         x23 = 0.5*(x2+x3);
         x31 = 0.5*(x3+x1);

         y12 = 0.5*(y1+y2);
         y23 = 0.5*(y2+y3);
         y31 = 0.5*(y3+y1);

         /* segundo triangulo */
         triangulo[m].xA = x12;
         triangulo[m].yA = y12;

         triangulo[m].xB = x2;
         triangulo[m].yB = y2;

         triangulo[m].xC = x23;
         triangulo[m].yC = y23;

         /* tercer triangulo */
         triangulo[p].xA = x31;
         triangulo[p].yA = y31;

         triangulo[p].xB = x23;
         triangulo[p].yB = y23;

         triangulo[p].xC = x3;
         triangulo[p].yC = y3;


         /* primer triangulo - inicial */

         // triangulo[n].xA and triangulo[n].yA remain unchanged.

         triangulo[n].xB = x12;
         triangulo[n].yB = y12;

         triangulo[n].xC = x31;
         triangulo[n].yC = y31;

      }