# Dynamic 2-dimensional array

**URL:** <https://forum.xojo.com/t/dynamic-2-dimensional-array/81201>\
**Category:** Getting Started\
**Created:** [July 30, 2024, 11:50am UTC](https://forum.xojo.com/t/dynamic-2-dimensional-array/81201 "2024-07-30T11:50:10Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Richard\_Klingler](https://forum.xojo.com/user_avatar/forum.xojo.com/richard_klingler/32/8198_2.png) [@Richard\_Klingler](https://forum.xojo.com/u/Richard_Klingler)\
**Post date:** [July 30, 2024, 11:50am UTC](https://forum.xojo.com/t/dynamic-2-dimensional-array/81201/1 "2024-07-30T11:50:10Z")

</div>

Good afternoon (o;

Have a small Gcode controller here which draws text and lines onto stainless steel tubes.  
Serial handling of the GRBL board is all fine. But now comes a challenge for me (o;

For text to be drawn I use normalized XY coordinates and move commands, which are of course of different sizes. The letter “I” uses less coordinates than letter “G”.

So what would be the best approach to store all capital letters and numbers in an array?

Can I just define a fixed size Array with the count being the number of all characters and then assign each element a different size array?

thanks in advance  
richard

---

<div class="post-metadata">

**Author:** ![Marius\_Dieter\_Noetzel](https://forum.xojo.com/user_avatar/forum.xojo.com/marius_dieter_noetzel/32/661_2.png) [@Marius\_Dieter\_Noetzel](https://forum.xojo.com/u/Marius_Dieter_Noetzel)\
**Post date:** [July 30, 2024, 1:01pm UTC](https://forum.xojo.com/t/dynamic-2-dimensional-array/81201/2 "2024-07-30T13:01:12Z")

</div>

If I’m not completely misunderstanding you, do you want to have an array that then contains several characters, or rather their gcode?  
I would build a class that contains the gcode, then one instance of it for each character.  
Now you create an array of the type “your class” and can add as many characters as you like.

---

<div class="post-metadata">

**Author:** ![Julia\_Truchsess](https://forum.xojo.com/user_avatar/forum.xojo.com/julia_truchsess/32/499_2.png) [@Julia\_Truchsess](https://forum.xojo.com/u/Julia_Truchsess)\
**Post date:** [July 30, 2024, 1:12pm UTC](https://forum.xojo.com/t/dynamic-2-dimensional-array/81201/3 "2024-07-30T13:12:50Z")

</div>

What @Marius_Dieter_Noetzel said. Store the coordinates as an array of Pairs in a property of the class, and give it a “Draw” method that iterates over the array to control the machine.

---

<div class="post-metadata">

**Author:** ![Marius\_Dieter\_Noetzel](https://forum.xojo.com/user_avatar/forum.xojo.com/marius_dieter_noetzel/32/661_2.png) [@Marius\_Dieter\_Noetzel](https://forum.xojo.com/u/Marius_Dieter_Noetzel)\
**Post date:** [July 30, 2024, 1:16pm UTC](https://forum.xojo.com/t/dynamic-2-dimensional-array/81201/4 "2024-07-30T13:16:04Z")

</div>

> [@Julia\_Truchsess](#):
>
> Store the coordinates as an array of Pairs

I would consider using an array of points 😉

---

<div class="post-metadata">

**Author:** ![Eric\_Williams](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/ecccb3/32.png) [@Eric\_Williams](https://forum.xojo.com/u/Eric_Williams)\
**Post date:** [July 30, 2024, 1:18pm UTC](https://forum.xojo.com/t/dynamic-2-dimensional-array/81201/5 "2024-07-30T13:18:33Z")

</div>

What you’ve suggested is possible - an array of arrays - but Xojo’s array handling makes this awkward at best. For that reason alone, an array attached to a custom class (as suggested above) is a cleaner solution.

This would have the advantage of giving you a place to store other useful information about the character, such as the overall character width.

---

<div class="post-metadata">

**Author:** ![Julia\_Truchsess](https://forum.xojo.com/user_avatar/forum.xojo.com/julia_truchsess/32/499_2.png) [@Julia\_Truchsess](https://forum.xojo.com/u/Julia_Truchsess)\
**Post date:** [July 30, 2024, 1:36pm UTC](https://forum.xojo.com/t/dynamic-2-dimensional-array/81201/6 "2024-07-30T13:36:36Z")

</div>

> [@Marius\_Dieter\_Noetzel](#):
>
> I would consider using an array of points

TIL that Xojo has a Point object 🙂

---

<div class="post-metadata">

**Author:** ![Tim\_Parnell](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_parnell/32/161_2.png) [@Tim\_Parnell](https://forum.xojo.com/u/Tim_Parnell)\
**Post date:** [July 30, 2024, 1:56pm UTC](https://forum.xojo.com/t/dynamic-2-dimensional-array/81201/7 "2024-07-30T13:56:19Z")

</div>

Point, Size, and Rect are often overlooked but very useful classes!

---

<div class="post-metadata">

**Author:** ![Jared\_Feder](https://forum.xojo.com/user_avatar/forum.xojo.com/jared_feder/32/3036_2.png) [@Jared\_Feder](https://forum.xojo.com/u/Jared_Feder)\
**Post date:** [July 30, 2024, 1:59pm UTC](https://forum.xojo.com/t/dynamic-2-dimensional-array/81201/8 "2024-07-30T13:59:36Z")

</div>

I agree with the others that a class is the way to go, but you can create an array of arrays of different dimensions like this:

```auto
dim characters() as Variant

dim a() as Point
a.Append new Point(1,2)

characters.Append a

dim b() as Point
b.Append new Point(3,4)
b.Append new Point(5,6)

characters.Append b

```

---

<div class="post-metadata">

**Author:** ![MarkusR](https://forum.xojo.com/user_avatar/forum.xojo.com/markusr/32/685_2.png) [@MarkusR](https://forum.xojo.com/u/MarkusR)\
**Post date:** [July 30, 2024, 3:05pm UTC](https://forum.xojo.com/t/dynamic-2-dimensional-array/81201/9 "2024-07-30T15:05:28Z")

</div>

> The letter “I” uses less coordinates than letter “G”.

> [@Richard\_Klingler](#):
>
> Can I just define a fixed size Array with the count being the number of all characters and then assign each element a different size array?

a class could contain this propeties  
String “A”  
Width 8  
Height 8  
you create this object via a shared method or own constructor and add it to an array.

---

<div class="post-metadata">

**Author:** ![Paul\_Rodman](https://forum.xojo.com/user_avatar/forum.xojo.com/paul_rodman/32/349_2.png) [@Paul\_Rodman](https://forum.xojo.com/u/Paul_Rodman)\
**Post date:** [July 30, 2024, 3:10pm UTC](https://forum.xojo.com/t/dynamic-2-dimensional-array/81201/10 "2024-07-30T15:10:13Z")

</div>

How about a Dictionary, with the keys being the characters to be drawn, and the associated values being JSONItem objects containing an array of coordinates.

---

<div class="post-metadata">

**Author:** ![Robert\_Livingston](https://forum.xojo.com/user_avatar/forum.xojo.com/robert_livingston/32/255_2.png) [@Robert\_Livingston](https://forum.xojo.com/u/Robert_Livingston)\
**Post date:** [July 30, 2024, 3:44pm UTC](https://forum.xojo.com/t/dynamic-2-dimensional-array/81201/11 "2024-07-30T15:44:06Z")

</div>

I also thought that a Dictionary would be efficient

```auto
// Fill Dictionary with Ascii(Char) as Keys and Points As values
Var dChar As New Dictionary
Var asciiValue As Integer

Var p_A As New Point
asciiValue = Asc("A")
p_A.X = 3
p_A.Y = 5
dChar.Value(asciiValue) = p_A

Var p_B As New Point
asciiValue = Asc("B")
p_B.X = 1
p_B.Y = 2
dChar.Value(asciiValue) = p_B
// …
Var p_lowerA As New Point
asciiValue = Asc("a")
p_lowerA.X = 7
p_lowerA.Y = 4
dChar.Value(asciiValue) = p_lowerA
// etc.

// retrieve from Dictionary (probably make a method for this)
Var someChar As String
someChar = "A"
Var somePt As New Point
Var xValue, yValue As Integer
somePt = dChar.Lookup(asc(someChar), "")
xValue = somePt.X
yValue = somePt.Y

```

---

<div class="post-metadata">

**Author:** ![Tim\_Parnell](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_parnell/32/161_2.png) [@Tim\_Parnell](https://forum.xojo.com/u/Tim_Parnell)\
**Post date:** [July 30, 2024, 4:09pm UTC](https://forum.xojo.com/t/dynamic-2-dimensional-array/81201/12 "2024-07-30T16:09:31Z")

</div>

A class would force self-documenting code that the next person (which could be you in 12 months!) can easily read and understand:

```auto
var oLetter as new Letter
oLetter.Character = "A"
oLetter.Coordinates = new Point(10, 20)

aroLetters.Add(oLetter)

```

One could even add a function to the Letter class to return the sizing information.

![Screen Shot 2024-07-30 at 12.04.53](https://forum.xojo.com/uploads/default/original/3X/8/6/8671600a9f44aa6a073116628cb2f14d3f7e2bf9.png)

Using a `Dictionary` or `JSONItem` here yields _ **no** _ benefits.

---

<div class="post-metadata">

**Author:** ![Richard\_Klingler](https://forum.xojo.com/user_avatar/forum.xojo.com/richard_klingler/32/8198_2.png) [@Richard\_Klingler](https://forum.xojo.com/u/Richard_Klingler)\
**Post date:** [July 30, 2024, 5:15pm UTC](https://forum.xojo.com/t/dynamic-2-dimensional-array/81201/13 "2024-07-30T17:15:31Z")

</div>

Wow…so many suggestion…thanks to all 🙂

Have to read through all carefully tomorrow and see what works best for me…  
The hint with the Point variable is very good…though somehow I have to add up/down movements as well…but that’s up to me now (o;

Been a while I last used Xojo…2022 IIRC (o;

---

<div class="post-metadata">

**Author:** ![Jeff\_Tullin](https://forum.xojo.com/user_avatar/forum.xojo.com/jeff_tullin/32/693_2.png) [@Jeff\_Tullin](https://forum.xojo.com/u/Jeff_Tullin)\
**Post date:** [July 30, 2024, 10:26pm UTC](https://forum.xojo.com/t/dynamic-2-dimensional-array/81201/14 "2024-07-30T22:26:10Z")

</div>

> [@Richard\_Klingler](#):
>
> The hint with the Point variable is very good…though somehow I have to add up/down movements as well

A rect, then… 🙂  
But a class with custom properties is best and allows for expansion

---

<div class="post-metadata">

**Author:** ![Marius\_Dieter\_Noetzel](https://forum.xojo.com/user_avatar/forum.xojo.com/marius_dieter_noetzel/32/661_2.png) [@Marius\_Dieter\_Noetzel](https://forum.xojo.com/u/Marius_Dieter_Noetzel)\
**Post date:** [July 31, 2024, 7:57am UTC](https://forum.xojo.com/t/dynamic-2-dimensional-array/81201/15 "2024-07-31T07:57:27Z")

</div>

> [@Richard\_Klingler](#):
>
> though somehow I have to add up/down movements as well

You can subclass the point-Object and add ‘Z’.

---

<div class="post-metadata">

**Author:** ![Eric\_Williams](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/ecccb3/32.png) [@Eric\_Williams](https://forum.xojo.com/u/Eric_Williams)\
**Post date:** [July 31, 2024, 12:34pm UTC](https://forum.xojo.com/t/dynamic-2-dimensional-array/81201/16 "2024-07-31T12:34:49Z")

</div>

I wouldn’t recommend this from an object hierarchy standpoint. Point has a number of useful methods, like Clone and DistanceTo, that work very differently in two dimensions versus three - subclassing shouldn’t disturb how the base class works with its data.

Besides, since he’s plotting these characters with a motorized tool, my hunch is that he’s describing a “tool up” and “tool down” action, not a true Z axis coordinate for each point.

---

<div class="post-metadata">

**Author:** ![Marius\_Dieter\_Noetzel](https://forum.xojo.com/user_avatar/forum.xojo.com/marius_dieter_noetzel/32/661_2.png) [@Marius\_Dieter\_Noetzel](https://forum.xojo.com/u/Marius_Dieter_Noetzel)\
**Post date:** [July 31, 2024, 12:39pm UTC](https://forum.xojo.com/t/dynamic-2-dimensional-array/81201/17 "2024-07-31T12:39:57Z")

</div>

> [@Eric\_Williams](#):
>
> Point has a number of useful methods, like Clone and DistanceTo, that work very differently in two dimensions versus three - subclassing shouldn’t disturb how the base class works with its data

Yes, and these continue to work, as Z is used here to raise and lower the tool and is not used to calculate the actual travel distance. Z can therefore be used for this special project without any problems. Of course, this cannot be used for points in “real” 3D space.

> [@Eric\_Williams](#):
>
> Besides, since he’s plotting these characters with a motorized tool, my hunch is that he’s describing a “tool up” and “tool down” action, not a true Z axis coordinate for each point.

But, perhaps he needs different hight. If you use a CNC or a 3D printer, for example, the tool height changes. It is also possible that there is a height adjustment because the Z height varies depending on X and Y.

---

<div class="post-metadata">

**Author:** ![Eric\_Williams](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/ecccb3/32.png) [@Eric\_Williams](https://forum.xojo.com/u/Eric_Williams)\
**Post date:** [July 31, 2024, 12:48pm UTC](https://forum.xojo.com/t/dynamic-2-dimensional-array/81201/18 "2024-07-31T12:48:58Z")

</div>

Thanks for rephrasing the second paragraph in my post. 😉 I’ve written plotter control software so this is a situation I’m familiar with.

If I were modeling this data, I’d store multiple arrays of data for each shape being plotted. Each line that the plotter draws would be its own array; so the letter “T”, for example, might have one array of points for the vertical part and a second for the cross at the top. This would allow the plotting software to intrinsically know when to lower the tool and when to raise it.

Furthermore, if you model the data in this fashion the plotting software can make certain inferences from the data and optimize its operations; for example, it could analyze the lines and determine the most efficient order in which to draw them based on their starting and ending coordinates.

---

<div class="post-metadata">

**Author:** ![Marius\_Dieter\_Noetzel](https://forum.xojo.com/user_avatar/forum.xojo.com/marius_dieter_noetzel/32/661_2.png) [@Marius\_Dieter\_Noetzel](https://forum.xojo.com/u/Marius_Dieter_Noetzel)\
**Post date:** [July 31, 2024, 12:55pm UTC](https://forum.xojo.com/t/dynamic-2-dimensional-array/81201/19 "2024-07-31T12:55:18Z")

</div>

> [@Eric\_Williams](#):
>
> I’ve written plotter control software so this is a situation I’m familiar with.

Correct, plotters don’t have a real Z. They only know up and down. However, there are other machines that work with real Z values for the tool and these values also change during the process.

---

<div class="post-metadata">

**Author:** ![Eric\_Williams](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/ecccb3/32.png) [@Eric\_Williams](https://forum.xojo.com/u/Eric_Williams)\
**Post date:** [July 31, 2024, 12:56pm UTC](https://forum.xojo.com/t/dynamic-2-dimensional-array/81201/20 "2024-07-31T12:56:43Z")

</div>

Yeah, CNC routers do that. Doesn’t sound like what’s he’s working with here, but perhaps I’m mistaken.

[Next page](https://forum.xojo.com/t/dynamic-2-dimensional-array/81201.md?page=2)
