### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: [solved]sorting array of path pointers by distance in 3D model  (Read 590 times)

#### tygraphics

• New Member
• Posts: 19
##### [solved]sorting array of path pointers by distance in 3D model
« on: August 09, 2024, 06:19:51 pm »
Hi,

I am working on a 3D vector application project using Free Pascal, and I need to sort an array of pointers referencing Path records. Each Path record contains a field that stores the distance from a camera to the center point of that path. The goal is to sort this array of pointers in descending order based on the distance values so that during the rendering process, paths that are furthest away from the camera are drawn first, and those closest are drawn last.

Thomas Young
« Last Edit: August 11, 2024, 08:52:34 pm by tygraphics »

#### Hartmut

• Hero Member
• Posts: 803
##### Re: sorting array of path pointers by distance in 3D model
« Reply #1 on: August 09, 2024, 06:55:49 pm »
can you show as the declaration of the records and the array of pointers? And how many data (about) do you want to sort?

#### tooldeveloper

• Newbie
• Posts: 4
##### Re: sorting array of path pointers by distance in 3D model
« Reply #2 on: August 09, 2024, 08:26:24 pm »
The TFPObjectList class in the Contnrs unit may solve your issue.  Assuming I am reading your question correctly then below is a quick example of doing what I think you are asking.

Code: Pascal  [Select][+][-]
1. uses
2.   Contnrs;
3.
4. { TForm1 }
5.
6.   type
7.     T3DVector = record
8.       CamDistance : Int64;
9.     end;
10.     P3DVector = ^T3DVector;
11.
12. function VectorSort( Item1 : pointer; Item2: Pointer ) : Integer;
13.   var
14.     Vec1 : P3DVector;
15.     Vec2 : P3DVector;
16.   begin
17.     // <0 Item1 is closer, 0=Same Distance, >0 Item2 is closer
18.     Vec1 := Item1;
19.     Vec2 := Item2;
20.
21.     result := (Vec2^.CamDistance - Vec1^.CamDistance);
22.   end;
23.
24. procedure TForm1.Button1Click(Sender: TObject);
25.   var
26.     fpo : TFPObjectList;
27.     idx : integer;
28.     Item1 : T3DVector;
29.     Item2 : T3DVector;
30.     Item3 : T3DVector;
31.     Item4 : T3DVector;
32.   begin
33.     fpo := TFPObjectList.Create;
34.     fpo.OwnsObjects := False;
35.
36.     Item1.CamDistance := 111;
37.     Item2.CamDistance := 222;
38.     Item3.CamDistance := 333;
39.     Item4.CamDistance := 444;
40.
45.
46.     Memo1.Lines.Clear;
48.     for idx := 0 to fpo.Count-1 do
49.       begin
51.       end;
52.
54.     Memo1.Lines.Add( 'Sorting the vectors' );
55.     fpo.Sort(@VectorSort);
56.
59.     for idx := 0 to fpo.Count-1 do
60.       begin
62.       end;
63.
64.     fpo.Free;
65.   end;
66.

The resulting output is:
Code: [Select]
`Pre-SortItems[0]=111Items[1]=222Items[2]=333Items[3]=444Sorting the vectorsPost-SortItems[0]=444Items[1]=333Items[2]=222Items[3]=111`

Explanation:
The TFPObjectList is storing the pointers to all of the items, in the above case they are simply pointers to simple records existing on the stack.

When I call the "Sort" method on the TFPObjectList object I'm supplying the custom VectorSort comparison function that compares the CamDistance for each of the two objects.

In a default Sort function it would "subtract" the second item from the first item. In the above example, the VectorSort function is subtracting the first item from the second to achieve the reverse sort.
« Last Edit: August 09, 2024, 08:44:42 pm by tooldeveloper »

#### bytebites

• Hero Member
• Posts: 664
##### Re: sorting array of path pointers by distance in 3D model
« Reply #3 on: August 09, 2024, 10:24:24 pm »
Use code tags.