How to convert Vector{Point2{Float64}} to Vector{Tuple{Float64, Float64}}:

Actually the title says it all. How to perform this conversion ? I haven’t found anything.

Have a look at the function reinterpret

The Point2 type isn’t part of Julia Base. Which package are you using that defines it? I believe there’s one in Makie, Meshes, and probably a few other geometry or coordinate system related packages.

1 Like

Oh, I didn’t know that,

then lets suppose I got a simple vector like

using GeometryBasics

v = Point2.(rand(10))

this will create a

10-element Vector{Point2{Float64}}

In my eyes it must be possible to convert it to

10-element Vector{Tuple{Float64, Float64}}

this can be easily shown by

[(v[1][1], v[1][2]), (v[2][1], v[2][2]), (v[3][1], v[3][2])]

which will create a vector of the type above.

But I don’t want to to it all manually.

I already tried with a for-loop but no success.

Well this is over my head :sweat_smile:

julia> using GeometryBasics

julia> v = rand(Point2{Float64}, 10)
10-element Vector{Point2{Float64}}:
 [0.23398248544355948, 0.02452509374439149]
 [0.27844708107081617, 0.06249289970873717]
 [0.6555220179505118, 0.27570802615639156]
 [0.43663412028067594, 0.7334931851153921]
 [0.7282970726772982, 0.3053395319059993]
 [0.2857930185597616, 0.02425459211368952]
 [0.1996406989985261, 0.8910321768520889]
 [0.9761205749615253, 0.6585221941570446]
 [0.4635527211775988, 0.9869582972102782]
 [0.012031268178171461, 0.7939369315234106]

julia> reinterpret(Float64, v)
20-element reinterpret(Float64, ::Vector{Point2{Float64}}):
 0.23398248544355948
 0.02452509374439149
 0.27844708107081617
 0.06249289970873717
 0.6555220179505118
 0.27570802615639156
 0.43663412028067594
 0.7334931851153921
 0.7282970726772982
 0.3053395319059993
 0.2857930185597616
 0.02425459211368952
 0.1996406989985261
 0.8910321768520889
 0.9761205749615253
 0.6585221941570446
 0.4635527211775988
 0.9869582972102782
 0.012031268178171461
 0.7939369315234106

julia> reinterpret(Tuple{Float64, Float64}, v)
10-element reinterpret(Tuple{Float64, Float64}, ::Vector{Point2{Float64}}):
 (0.23398248544355948, 0.02452509374439149)
 (0.27844708107081617, 0.06249289970873717)
 (0.6555220179505118, 0.27570802615639156)
 (0.43663412028067594, 0.7334931851153921)
 (0.7282970726772982, 0.3053395319059993)
 (0.2857930185597616, 0.02425459211368952)
 (0.1996406989985261, 0.8910321768520889)
 (0.9761205749615253, 0.6585221941570446)
 (0.4635527211775988, 0.9869582972102782)
 (0.012031268178171461, 0.7939369315234106)
1 Like

Okay, thank you but actually that doesn’t help me in my case.

Probably I should have specified what I plan to do - I want to perform a triangulation for which I need to create boundary points like

using GeometryBasics, DelaunayTriangulation

points = [(0.0, 0.0), (1.0, 0.0), (1.0, 0.3), (0.8, 0.3), 
(0.8, 0.5), (1.0, 0.5), (1.0, 1.0), (0.0, 1.0), (0.0, 0.0)]

boundary_nodes, points = convert_boundary_points_to_indices(points)

The points specified here are of type

9-element Vector{Tuple{Float64, Float64}}

However as stated in the title my values are of type

v = Point2.(rand(9))

9-element Vector{Point2{Float64}}

so

boundary_nodes, points = convert_boundary_points_to_indices(v)

doesn’t work

also

v = reinterpret(Float64,v)

boundary_nodes, points = convert_boundary_points_to_indices(v)

does work neither. Any ideas ?

Did you try reinterpret(Tuple{Float64, Float64}, v)?

I am confused how you go from 10 Point2 to 9 Tuple?

Also here are a few other approaches.

julia> getproperty.(v, :data)
10-element Vector{Tuple{Float64, Float64}}:
 (0.23398248544355948, 0.02452509374439149)
 (0.27844708107081617, 0.06249289970873717)
 (0.6555220179505118, 0.27570802615639156)
 (0.43663412028067594, 0.7334931851153921)
 (0.7282970726772982, 0.3053395319059993)
 (0.2857930185597616, 0.02425459211368952)
 (0.1996406989985261, 0.8910321768520889)
 (0.9761205749615253, 0.6585221941570446)
 (0.4635527211775988, 0.9869582972102782)
 (0.012031268178171461, 0.7939369315234106)

julia> Tuple.(v)
10-element Vector{Tuple{Float64, Float64}}:
 (0.23398248544355948, 0.02452509374439149)
 (0.27844708107081617, 0.06249289970873717)
 (0.6555220179505118, 0.27570802615639156)
 (0.43663412028067594, 0.7334931851153921)
 (0.7282970726772982, 0.3053395319059993)
 (0.2857930185597616, 0.02425459211368952)
 (0.1996406989985261, 0.8910321768520889)
 (0.9761205749615253, 0.6585221941570446)
 (0.4635527211775988, 0.9869582972102782)
 (0.012031268178171461, 0.7939369315234106)
1 Like

So doing

using GeometryBasics, DelaunayTriangulation

v = rand(Point2{Float64}, 10)


v = reinterpret(Tuple{Float64, Float64}, v)

boundary_nodes, points = convert_boundary_points_to_indices(v)

gives

AssertionError: x[begin] ≈ x[end]

|

but when using points that form a closed shape, it does seem to work.

I’ll check if I can triangulate my polygon with that


Edit: It really does work, thanks again!

Try view(v, [1:10,1])?

julia> v = rand(Point2{Float64}, 10);

julia> v = reinterpret(NTuple{2,Float64}, v);

julia> v = @view v[[begin:end; begin]]
11-element view(reinterpret(Tuple{Float64, Float64}, ::Vector{Point2{Float64}}), [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1]) with eltype Tuple{Float64, Float64}:
 (0.045284339720533695, 0.9325640220771096)
 (0.3331647565133017, 0.15689121150971186)
 (0.9830499022615393, 0.6216043326795608)
 (0.030846773825717766, 0.5901168577657975)
 (0.17929582192192317, 0.19425072688193035)
 (0.001888257902482482, 0.9545544015748283)
 (0.8675845436130039, 0.48353984366916114)
 (0.8868045558380299, 0.38072206725730584)
 (0.8242620285065096, 0.8430903883691105)
 (0.8322873598355732, 0.8199610284020267)
 (0.045284339720533695, 0.9325640220771096)
1 Like

Again, your simple manipulation

reinterpret(Tuple{Float64, Float64}, v)

did the trick. :grin:

About your confusion about 9 or 10 points, it was just an example of different sets. Of course I wanted to convert 1 to 1.