Wish B4xPoint

stevel05

Expert
Licensed User
Longtime User
I quite often need to use a Point construct that holds 2 values. They are usually Doubles, but Floats would be fine if that's is more cross platform compatible.

I have used Types for this as needed, but end up with the same types in different libraries and then either have to go back and create a dependency and remove one of them, or rename one, or just use an array, which is quite an ugly solution.

It would be far better from a library perspective it there was a B4xPoint(X,Y) object in the XUI library along with B4xRect, which I am using more and more when dealing with Bounds etc. as well as drawing.
 

William Lancee

Well-Known Member
Licensed User
Longtime User
I have used Types and Classes for this object, but as you say there are annoyances in both approaches.
One could create a template that automatically adds a Point class to one's project.
I am thinking of doing that. For our 64bit machines, double is rarely slower that float.
So that would be my preference.

It would also be nice to have built-in functions.

B4X:
Dim pt1 As Point
pt1.Initialize(2.67, 3.12)

Dim pt2 As Point
pt2.Initialize(2.67, 9.257)

pt1.X
pt1.Y
pt1.Copy
pt1.Add(pt2)
pt1.Subtract(pt2)
pt1.AddScalar(100.55)
pt1.MultScalar(100.55)
pt1.DistanceTo(pt2)
pt1.MidTo(pt2)
pt1.FractionTo(pt2, .75)
 

William Lancee

Well-Known Member
Licensed User
Longtime User
I did the template thing, and now it works much better for me. All my future projects will have automatic inclusion of my Point class.
But a XUI version would be more consistent with B4XRect.
 

Attachments

  • testWiL.zip
    10.8 KB · Views: 61

emexes

Expert
Licensed User
I often just use an array, has the advantage that it can also represent a series of points eg path, polygon, rectangular area.

B4X:
Dim TappedXY() As Float

Dim RectangleXYXY() As Float = Array As Float( _
    Button1.Left                , Button1.Top, _
    Button1.Left + Button1.Width, Button1.Top + Button1.Height _
)

Dim PathXY() As Float = Array As Float(X1, Y1, X2, Y2, X3, Y3, X4, Y4)
 

emexes

Expert
Licensed User
For our 64bit machines, double is rarely slower that float.

Even more rarely less than twice the size of float.

And for point references are screen-related, the extra 29 bits of precision doesn't do much other than exercise the silicon.

But I agree it would be useful if you have points that are close together in a coordinate system with origin far away, eg latitude/longitude outside of England.
 

emexes

Expert
Licensed User
or just use an array, which is quite an ugly solution.

Lol I just spotted this.

That seems a bit harsh.

Arrays are just trying to help. Personally, I quite like them, but maybe that's coming from a C-background where you can cast arrays into rows and step through an array using a pointer of a type composed of multiple single elements of the array.

eg the array looks like this in memory:

x, y, x, y, x, y, x, y, x, y

and it is easy to step through them in groups of (x, y)
 
Last edited:

stevel05

Expert
Licensed User
Longtime User
That seems a bit harsh.
Possibly, but to my mind, a simple Point structure that has little use for anything else would lead to zero confusion or thinking time when going back over old code.
 
Last edited:

peacemaker

Expert
Licensed User
Longtime User
Maybe better to make once just universal set:


B4X:
Type B4X2Dpoint (x As Float, y As Float)
Type B4X3Dpoint (x As Float, y As Float, z As Float)
Type B4X4Dpoint (x As Float, y As Float, z As Float, t As Long)
 
Last edited:
Top