Petr Schreiber
14-06-2015, 21:07
In case anyone would have need for Point2D and collection of such a data, I prepared units for it.
They are fully unit tested and I use them in my projects, so I trust them enough to share.
Key advantages:
smart memory management (adding points does not always mean reallocating)
built in serialization/deserialization to/from string, so you can easily store/load from files, if needed for example
Full code in the attachements, here a little code to give you idea:
Uses "Console"
#INCLUDE "Point2DCollection.tBasicU" ' -- All you need to use the type
Dim points As Point2DCollection
points.Initialize() ' -- Allocates internal structures
points.AddPoint(1, 2)
points.AddPoint(3, 4)
points.SetPoint(2,
30, 40) ' -- Alter the data for second point
points.AddPoint(5, 6)
Long i
Dim p As Point2D
PrintL "Point set, one by one:"
For i = 1 To points.GetPointCount()
p = points.GetPoint(i) ' -- Retrieves copy of the data you can work further with
PrintL p.ToString() ' -- Serialization to string
Next
String serializedBackup = points.ToString()
PrintL
PrintL "Point set, in one go: " + points.ToString()
points.RemovePoint(1) ' -- Remove one point, or...
PrintL
PrintL "Point set with first point gone, in one go: " + points.ToString()
points.RemoveAll() ' -- all of them
PrintL
PrintL "Point set with all points gone, in one go (nothing, because empty): " + points.ToString()
PrintL
points.FromString(serializedBackup) ' -- Deserialization from string is built in
PrintL "New set, loaded from backup string:" + points.ToString()
points.Release() ' -- When you are done, release memory
PrintL
PrintL "Press any key to quit..."
WaitKey
Feel free to use in your projects...
Petr
They are fully unit tested and I use them in my projects, so I trust them enough to share.
Key advantages:
smart memory management (adding points does not always mean reallocating)
built in serialization/deserialization to/from string, so you can easily store/load from files, if needed for example
Full code in the attachements, here a little code to give you idea:
Uses "Console"
#INCLUDE "Point2DCollection.tBasicU" ' -- All you need to use the type
Dim points As Point2DCollection
points.Initialize() ' -- Allocates internal structures
points.AddPoint(1, 2)
points.AddPoint(3, 4)
points.SetPoint(2,
30, 40) ' -- Alter the data for second point
points.AddPoint(5, 6)
Long i
Dim p As Point2D
PrintL "Point set, one by one:"
For i = 1 To points.GetPointCount()
p = points.GetPoint(i) ' -- Retrieves copy of the data you can work further with
PrintL p.ToString() ' -- Serialization to string
Next
String serializedBackup = points.ToString()
PrintL
PrintL "Point set, in one go: " + points.ToString()
points.RemovePoint(1) ' -- Remove one point, or...
PrintL
PrintL "Point set with first point gone, in one go: " + points.ToString()
points.RemoveAll() ' -- all of them
PrintL
PrintL "Point set with all points gone, in one go (nothing, because empty): " + points.ToString()
PrintL
points.FromString(serializedBackup) ' -- Deserialization from string is built in
PrintL "New set, loaded from backup string:" + points.ToString()
points.Release() ' -- When you are done, release memory
PrintL
PrintL "Press any key to quit..."
WaitKey
Feel free to use in your projects...
Petr