Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
storing coordinates in a table — Gideros Forum

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

storing coordinates in a table

Member
hello there, gideros people.

Can anybody tell me how to store coordinates in a table, please?

What I am trying to achieve:
I have a set of coordinates that I would like to use to draw a shape:

[[[79,39],[82,41],[84,48]],
[[84,48],[89,52],[90,59]],
[[70,29],[77,35],[79,39]],
[[66,67],[63,54],[66,50]],

I would like to store these coordinates in a table and draw the shape something like:
 ```shape:lineTo(0, -40) shape:lineTo(20, 0) shape:lineTo(-20, 0) shape:lineTo(0, -40)```
But using a for loop:
 ```for c = 1, #coords do shape:lineTo(coords[c].x, coords[c].y) end```
Any help would be appreciated. Thank you.
my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
Tagged:

Comments

• Member
edited April 2019 Accepted Answer
(sorry, maybe I didn't fully understand what you're trying to do)
Lua tables are very convenient.
 ```local coords={}   coords[1]={x=0,y=100} coords[2]={x=100,y=100} coords[3]={x=100,y=0} coords[4]={x=0,y=0}   square_shape:beginPath()   for c = 1, #coords do square_shape:lineTo(coords[c].x, coords[c].y) end   square_shape:endPath()   --you can have lots of nested tables: local otherCoordsTab={}   otherCoordsTab[1]={ {79,39},{82,41},{84,48} }   --to access it: local num=otherCoordsTab[1,3,1] print(num) --prints '84'```

Useful info about original Lua tables construction syntax:
https://www.lua.org/pil/3.6.html
And info about enhanced table syntax in Gideros:
http://docs.giderosmobile.com/reference/enhancement/easyarray#Better and easier syntax for arrays

Likes: MoKaLux

> Newcomers roadmap: from where to start learning Gideros
"What one programmer can do in one month, two programmers can do in two months." - Fred Brooks
“The more you do coding stuff, the better you get at it.” - Aristotle (322 BC)﻿
+1 -1 (+1 / -0 )Share on Facebook
• Member
hi appolo14, thank you for your prompt reply.

That worked! What I did:
 ``` -- shape local shape_points = {} table.insert(shape_points, {x=0, y=-40}) table.insert(shape_points, {x=20, y=0}) table.insert(shape_points, {x=-20, y=0}) table.insert(shape_points, {x=0, y=-40})   local shape = Shape.new() shape:setFillStyle(Shape.SOLID, 0xeeeeee) shape:setLineStyle(2, 0x001100, 1.0) shape:beginPath() shape:moveTo(0, 0)   for p = 1, #shape_points do shape:lineTo(shape_points[p].x, shape_points[p].y) end   shape:endPath()   self:addChild(shape)```

Thank you very much for your help. Do you think that is the right/best way of doing it?
my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
• Member
edited April 2019
MoKaLux said:

Do you think that is the right/best way of doing it?

I guess yes.
Perfomance-wise, 'table.insert(shape_points, {0,40}' is slower than 'shape_points[1]={0,40}'
(with 'table.insert' engine will have to pre-check what stuff is already there in table)

But you won't notice it unless you are filling lots of tables in one frame
> Newcomers roadmap: from where to start learning Gideros
"What one programmer can do in one month, two programmers can do in two months." - Fred Brooks
“The more you do coding stuff, the better you get at it.” - Aristotle (322 BC)﻿
• Member
Thank you apollo14.

Likes: SinisterSoft

my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
+1 -1 (+1 / -0 )Share on Facebook
• Maintainer
As above, but could be written as:
 `coords={{x=0,y=100},{x=100,y=100},{x=100,y=0},{x=0,y=0}}`
or simply
 `coords={{0,100},{100,100},{100,0},{0,0}}`
with coords[1][1] being 0 and [1][2] being 100

Likes: MoKaLux

Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
https://deluxepixel.com
+1 -1 (+1 / -0 )Share on Facebook
• Member
thank you sinistersoft.

Likes: SinisterSoft

my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
+1 -1 (+1 / -0 )Share on Facebook
• Member
edited April 2019
I do so:
 ``` coords.x= { {1,2,3,4,5}, {1,2,3,4,5}, {1,2,3,4,5}, {1,2,3,4,5} } coords.y={ {1,2,3,4,5}, {1,2,3,4,5}, {1,2,3,4,5}, {1,2,3,4,5} }```

or
 ```coords={ {{79,39},{82,41},{84,48}}, {{84,48},{89,52},{90,59}}, {{70,29},{77,35},{79,39}}, {{66,67},{63,54},{66,50}} }```
my games:
https://play.google.com/store/apps/developer?id=razorback456
мій блог по гідерос https://simartinfo.blogspot.com
Слава Україні!
+1 -1 (+2 / -0 )Share on Facebook
• Member
yes, I did the latest! Thank you oleg.
my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
• Member
edited April 2019
I don't know how the code you have actually works correctly. In theory you need to move the cursor to the first coordinate and then loop from 2, #points.

Not doing so would draw a funny line from 0,0 to the first actual coordinate where you want to actually begin drawing from.

And with tables.. use table[#table + 1] = whatever to append to the end of a table quicker than table.insert. I suppose you could always use table[n] = whatever but I think table[#table + 1] = whatever is better
• Member
hello antix, that code worked because all my shapes are drawn from the 0, 0 origins. If the starting point is not at 0, 0 then I use moveto(x, y) and loop from 2, #points.

Ok, from now on when inserting things in tables I will use table[#table + 1] = whatever.

Thanks a lot all of you for your help and your precious advice.

I need more practice but I don't want to burn out!
my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
• Member
@MoKaLux just incase you decide to start drawing from an origin other than 0,0 I'd suggest modifying the code now so it is more robust

and if you burn out.. don't worry.. it will only be for a little while.. and then you'll be right back at it!

Likes: MoKaLux, talis

+1 -1 (+2 / -0 )Share on Facebook
Sign In or Register to comment.