POV-Ray : Newsgroups : povray.advanced-users : Negative subscript in while loop : Negative subscript in while loop Server Time
24 Feb 2024 16:53:46 EST (-0500)
  Negative subscript in while loop  
From: Bald Eagle
Date: 15 Jan 2023 18:15:00
Message: <web.63c487f2edb901151f9dae3025979125@news.povray.org>
So, I'm trying to convert some online code to SDL,

( https://github.com/xmdi/CAD-from-Scratch/blob/main/016/geom.c  )

and the conditions in the while statement
     #while( j >= 0 & bins[j] > key)
don't protect against j being negative.

line 226: Parse Error: Negative subscript

Render failed

Probably because they're collected in one line / statement and are therefore all
evaluated together, and the while loop doesn't terminate until that expression
is fully evaluated.

I managed to hacktastically bypass / protect against this with some additional
protective wrappers to the loop, but this doesn't seem like it ought to be
necessary, or else there ought to be a more elegant way to correct this in SDL.

Unless the while source code has a bug that I've triggered, and _I've broken
POV-Ray AGAIN_.   :\

 // sort points by proximity
 #local NbinRows = ceil(pow(numpoints, 0.25));
 #local bins = array [numpoints+1];

 #for (i, 0, numpoints)
  #local  p = (points[i][1] * NbinRows*0.999); // bin row
  #local  q = (points[i][0] * NbinRows*0.999); // bin column
  #if (mod(p, 2))
   #local bins[i] = (p+1) * NbinRows - q;
   #local bins[i] = p * NbinRows + q + 1;

 #for (i, 1, numpoints) // insertion sort
  #local key = bins[i];
  #local tempF = array {points[i][0], points[i][1]};
  #local  j = i - 1;
  #debug "------------------------------------------------\n"
  #debug concat ("      i = ", str (i, 0, 0), "\n")
  #debug concat ("bins[i] = ", str (bins[i], 0, 0), "\n")
  #debug concat ("bins[j] = ", str (bins[j], 0, 0), "\n")
  #if (j > 0)
  #while( j >= 0 & bins[j] > key)
   #local bins[j+1] = bins[j];
   #local points[j+1][0] = points[j][0];
   #local points[j+1][1] = points[j][1];
   #local j = j-1;

   #debug concat ("j = ", str (j, 0, 0), "\n")
   #debug concat ("bins[i] = ", str (bins[i], 0, 0), "\n")
   #if (j<0) #break #end
  #end // end while

  #local bins[j+1] = key;
  #local points[j+1][0] = tempF[0];
  #local points[j+1][1] = tempF[1];

Also, if anyone can tell me if this is C, C++, or C#, and how to properly
interpret and convert statements such as:

int* bins=malloc(numPoints*sizeof(int));
int (*verts)[3]=malloc(3*sizeof(int));
int (*tris)[3]=malloc(3*sizeof(int));
int *triangleStack=malloc((numPoints-3)*sizeof(int));
int *renumberAdj=calloc(nT,sizeof(int));
bool *deadTris=calloc(nT,sizeof(bool));
int (*verts_final)[3]=malloc(3*nT_final*sizeof(int));
int (*tris_final)[3]=malloc(3*nT_final*sizeof(int));

that would be great.  I kinda get it, but a clear explanation from someone who
know the native language PLUS SDL, would help doing any such future conversions.

Maybe I could even write a small macro to make declaring such arrays a bit more

Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.