Jump to content
FranzB

static array vs. dynamic array

Recommended Posts

I use  2 Polygon definitions as  in the code  below 

type   

TPolygon = array  of TPoint

T4Polygon = array [1..4]  of TPoint ;

....
function ProcessPolygon (aPoly :  TPolygon);
begin 



end; 

 

is there a better way  to call  the function  ProcessPolygon with a variable  of the T4Polygon ? 

I don't want to  copy  the data from one  data type  to the  other,   writing  overloaded  functions  seems to my also not the best solution , 

any better idea ?

 

 

 

Share this post


Link to post

Better use a class for TPolygon and put not only the data but also the processing. If the class don't know how to do some things, then use an event to externalize that. For example, the class could not be able to render the polygon but have the algorithm to do it. Then, you can have an event in TPloygon named OnDrawLine and having proper arguments to delegate (externalize) the line drawing.

 

Share this post


Link to post
14 minutes ago, FPiette said:

Better use a class for TPolygon and put not only the data but also the processing. If the class don't know how to do some things, then use an event to externalize that. For example, the class could not be able to render the polygon but have the algorithm to do it. Then, you can have an event in TPloygon named OnDrawLine and having proper arguments to delegate (externalize) the line drawing.

 

You can't know that. It is easy to imagine scenarios where a value type is essential for performance reasons. 

Share this post


Link to post
37 minutes ago, David Heffernan said:

It is easy to imagine scenarios where a value type is essential for performance reasons. 

That's quite a controversial question. It is easy to image that class inheritance will benefit a lot to the design of graphic application. And between a class and a record, there is only one more indirection in some case and none when value type are used thru pointers for performance reasons...

Share this post


Link to post

Hi,

 

The way your types are declared you most certainly will end up writing overloads or specific methods for each!

Have you considered the possibility work with only one type of array? (TPoligonA)

 

 

For example:

  TPoligon = Array of TPoint;
  T4Poligon = Array [1..4] of TPoint;

  TPoligonA = TArray<TPoint>;


  TForm17 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }

    function NewT4 : TPoligonA;
    function ProcessPoligon( const aPoligon : TPoligonA ) : Boolean;

  end;
  
  
  implementation
  
  function TForm17.NewT4: TPoligonA;
begin
  SetLength(Result,4);
end;

function TForm17.ProcessPoligon(const aPoligon: TPoligonA): Boolean;
begin

end;

 

NewT4 will return a zero base array.. Since you will loose delphi type checking and range checking, you will need to check if the array have 4 elements.
But, you will work with a unique type, which is what you asked

 

HTH
 

Share this post


Link to post
4 hours ago, FPiette said:

That's quite a controversial question. It is easy to image that class inheritance will benefit a lot to the design of graphic application. And between a class and a record, there is only one more indirection in some case and none when value type are used thru pointers for performance reasons...

Sometimes reference types are best, and sometimes value types are best. That's why we have both. You cannot know that a class is right here. 

Share this post


Link to post
5 hours ago, FPiette said:

And between a class and a record, there is only one more indirection in some case and none when value type are used thru pointers for performance reasons...

And then you put them into an array and have no contiguous memory where the data resides in but just a bunch of pointers pointing all over the heap -> bad.

Share this post


Link to post
9 hours ago, Stefan Glienke said:

And then you put them into an array and have no contiguous memory where the data resides in but just a bunch of pointers pointing all over the heap -> bad.

Hrm... yeah. I've got some of those :classic_blush:.

Share this post


Link to post
15 hours ago, FPiette said:

It is easy to image that class inheritance will benefit a lot to the design of graphic application

Not for every type. It's not helpful making these assumptions. 

Share this post


Link to post
procedure TSparks.drawSparksE;
var
  h, x, w, y, PtsIndex: integer;
  dataPos, startPos: integer;
  Xofs, widthDelta: double;
  CP: TChartPoint;
  //arPts: array[0..maxChart] of TPoint;    // had to remove - 1 for thing to work
  arrPts: Array of TPoint;
begin
  with A1Canvas do
  begin
    Brush.Color := clnavy;
    startPos := SparkPos;
    if sparkPos = 0 then
    begin
      Brush.Color := clcream;
      fillRect(Rect(0,0,220,220));
      SparkPos := cmaxChart - copenChart;
     /// StartPos := 0;
    end;
    assert ((Sparkpos - StartPos + 1) > 0);
    setlength(arrPts,Sparkpos - StartPos + 2);
    h := image.Height;
    w := image.width;
    widthdelta := w/cmaxchart;
    Xofs := widthdelta/2;
    for PtsIndex := low(sparkers) to high(sparkers) do
    begin
      CP := Sparkers[Ptsindex];
      y  := round (h * (CP.span - CP.data^ - CP.Zero)/(CP.span - CP.Zero));
      CP.lastY[dataindex] := y;
      for DataPos := StartPos to SparkPos + 1 do
      begin                      /// ^ polyline needs two points
        x := round(Xofs + dataPos * widthDelta);
        if way = -1 then
        x := w - x;
        arrPts[dataPos - startpos].x := round(x + Xofs * way);
        arrPts[dataPos - startpos].y := CP.lastY[(DataIndex + dataPos - sparkpos - 1 + cmaxData)mod cmaxData];
      end;
      Pen.Color:= CP.color;
      Pen.Width:= 5;
      polyline(arrPts);
     //works in Lz Windows.polyline(arpts,StartPos,sparkpos - startPos + 2);// <-- need local variab
    end;
  end;
  sparkPos := (SparkPos + 1) mod (cmaxChart); // subtracting the one fixed the jump back
end;

procedure TSparks.updateSparks(const MScount: integer);
begin
  dataindex := (dataindex + 1) Mod cmaxdata;
  drawSparksE;
end;

Example code of Spark class which contains a reference to a controls canvas and several spark lines Classes with ring buffer, color 

allowing better separation of code by passing the UI's canvas to this class yielding the canvas and its height and width for free!

 

Pat   

 

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×