Home > other >  Receive values from dynamic array
Receive values from dynamic array

Time:10-21

I recently asked question about how to work with element Edit1 dynamically, now I want to ask something about values, which I received from dynamical arrays. First I try to divide image into sectors:

  const n=20;
  unsigned short i, j, line_length, w = Image1->Width, h = Image1->Height, l = Left   Image1->Left, t = Top   Image1->Top;
  unsigned short border = (Width-ClientWidth)/2, topborder = Height-ClientHeight-border;


  Image1->Canvas->Pen->Color = clRed;
  for (i = 0; i <= n; i  )
  {
    Image1->Canvas->MoveTo(0, 0);
    line_length = w * tan(M_PI/2*i/n);
    if (line_length <= h)
      Image1->Canvas->LineTo(w, line_length);
    else
    {
      line_length = h * tan(M_PI/2*(1-1.*i/n));
      Image1->Canvas->LineTo(line_length, h);
    }
  }

Then I use regions to count black dots in each sector and I want to add values to element Memo:

  HRGN region[n];
  TPoint points[3];
  points[0] = Point(l   border, t   topborder);
  for (i = 0; i < n; i  )
  {
    for (j = 0; j <= 1; j  )
    {
      line_length = w * tan(M_PI/2*(i j)/n);
      if (line_length <= h)
        points[j 1] = Point(l   border   w, t   topborder   line_length);
      else
      {
        line_length = h * tan(M_PI/2*(1-1.*(i j)/n));
        points[j 1] = Point(l   border   line_length, t   topborder   h);
      }
    }
    region[i] = CreatePolygonRgn(points, 3, ALTERNATE);  // or WINDING ??  as u want
  }

  Byte k;
  unsigned __int64 point_count[n] = {0}, points_count = 0;
  for(j = 0; j < h; j  )
    for (i = 0; i < w; i  )
      if (Image1->Canvas->Pixels[i][j] == clBlack)
      {
        points_count  ;
        for (k = 0; k < n; k  )
          if (PtInRegion(region[k], l   border   i, t   topborder   j))
            point_count[k]  ;
      }

  unsigned __int64 sum = 0;
  for (i = 0; i < n; i  )
  {
    sum  = point_count[i];
    Memo1->Lines->Add(point_count[i]);
  }

As i received an advice from one man, in order to allocate an array using a TEdit to specify the array's count I should use, for example DynamicArray:

#include <sysdyn.h>
DynamicArray<HRGN> region;
...
int n = Edit1-> Text.ToInt(); 
region.Length = n;

I have made the same changes to point_count array:

Byte k;
DynamicArray<unsigned __int64> point_count;
point_count.Length = n;
unsigned __int64 /*point_count[n] = {0},*/ points_count = 0;
...

The problem is that I received different values if I do it dynamically or statically(n=20).

Statically: image

Dynamically: image

CodePudding user response:

The problem is that I received different values if I do it dynamically or statically(n=20)

There is no difference whatsoever in accessing elements of a static array vs a dynamic array. Your problem has to be elsewhere.

For instance, your static code is initializing all of the array elements to 0, but your dynamic code is not doing that, so they will have random values before your loop then increments them.

Try this:

DynamicArray<unsigned __int64> point_count;
point_count.Length = n;
for(int i = 0; i < n;   i) {
    point_count[i] = 0;
}
...

Alternatively:

DynamicArray<unsigned __int64> point_count;
point_count.Length = n;
ZeroMemory(&point_count[0], sizeof(unsigned __int64) * n);
...

Also, using the Image1->Canvas->Pixels[][] property is very slow. Consider using the Image1->Picture->Bitmap->ScanLine[] property instead for faster access to the raw pixels.

  • Related