从动态数组接收值

Receive values from dynamic array

我最近问了一个关于如何动态地使用元素 Edit1 的问题,现在我想问一些关于值的问题,这是我从动态数组中收到的。首先我尝试将图像划分为扇区:

  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);
    }
  }

然后我使用区域来计算每个扇区中的黑点,我想将值添加到元素备注:

  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]);
  }

因为我收到了一个人的建议,为了使用 TEdit 分配一个数组来指定我应该使用的数组计数,例如 DynamicArray:

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

我对 point_count 数组进行了相同的更改:

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

问题是,如果我动态地或静态地 (n=20) 执行此操作,我会收到不同的值。

静态:

动态:

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

访问静态数组和动态数组的元素没有任何区别。你的问题一定在别处。

例如,您的静态代码将所有数组元素初始化为 0,但您的动态代码没有这样做,因此它们将在循环之前具有随机值,然后递增它们。

试试这个:

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

或者:

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

此外,使用 Image1->Canvas->Pixels[][] 属性 非常慢。考虑使用 Image1->Picture->Bitmap->ScanLine[] 属性 来更快地访问原始像素。