Maintenance notice: These forum archives are read-only, and will be removed shortly. Please visit our forums at their new location, https://www.evilmadscientist.com/forums/.

Meggy Jr. Programming Syntax Question

edited November 2012 in LED Matrix Kits
I'm programming for the Meggy Jr. using the Arduino environment.

I am trying to use an array to store a series of coordinates to describe a shape. I used to use two separate arrays of ints, linked by index (e.g., DrawPx(xcoord[0], ycoord[0], Blue) and that worked okay but it was cumbersome.

Now instead, I'm trying to use a struct that I have defined like so:

struct Point{
 int x;
 int y;
};

Point s1 = {3,2};
Point s2 = {3,3};
Point myArray = {s1, s2}; // This works fine

The problem is that I am hoping to add a bunch of Points to the one array and I don't want to have to create each one as a separate variable prior to adding it to the array. What is the syntax for creating a new Point on the fly? Something like:

Point myArray = {new Point(3,2), new Point(3,3)}; // Doesn't work

I know this works in Java, I'm new to structs and am a little confused as to what the proper syntax would be (or if it's even possible).

Thanks for any insights,

--Doug.

Comments

  • edited November 2012
    In what you've written above, it's failing because you are trying to declare a new Point, but the two elements of new Point both need to be of type int, not of type Point.  

    For the same reason, your line above marked "This works fine" actually should not work fine.  (When I test it, it fails-- like it should!)


    It looks like you want to instead declare an array of Points.  To do so, you might declare and initialize the array as follows:

    struct Point myArray[] = { s1, s2 };

     


    Alternately, declare and then initialize the array like so:
    struct Point myArray[2];
    myArray[0] = s1;
    myArray[1] = s2;



    To change the values of the int values within a Point, you can use:

    s1.x = 4;
    s1.y = 5;

    After doing so, you can then the values in the array:

    myArray[1] = s1;

  • Thanks. I was writing the code from my faulty memory. You are right. When I checked the code that I wrote that worked, it looked like this:

    struct Point
    {
      int x;
      int y;
    };

    Point s1 = {3,4};
    Point s2 = {4,4};
    Point s3 = {5,4};
    Point s4 = {6,4};
    Point myArray[64] = {s1,s2,s3,s4};  //Shape coordinates

    void drawShape()
    {
      for (int i = 0; i < 4; i++)
      {
        DrawPx(myArray[i].x,myArray[i].y,Red);
      }
    }

    ..so, that does work. I next want to declare a few shapes that each consist of about sixteen separate Point objects. I am reluctant to declare them as individual variables (Point s1 = {3,4}, Point s2 = (4,4}, etc.) just for the purposes of adding them to the array.

    Is there a way to add Point objects to the array directly without taking the intermediary step of creating them as separate Point objects, each with their own variable name?

    In Java, I would have to create a class with a constructor that takes parameters:

    public class Point{
    int myX;
    int myY;
    public Point (int x, int y){
     myX = x;
     myY = y;
    }
    }

    but then I could initialize an array and add Points like this:

    Point[] myArray = {new Point(3,4), new Point(4,4), new Point(5,4), new Point(6,4)};

    Does Processing support anything similar?

    Thanks,

    --Doug.
  • Processing supports things like that-- it is, after all, java based --but Arduino (and hence, Meggy Jr) do not.

    You do not need to create separate variable names for each element of the array-- I don't see any case where that would be a good idea. 

    To use exactly zero named Point variables, use something like this:


    struct Point myArray[3];

    myArray[0] = (struct Point) {1,1};

    myArray[1] = (struct Point) {4,5};

    myArray[2] = (struct Point) {4,1};



  • Ah, great. That's exactly what I was looking for! Thanks so much for understanding my convoluted question. :-)

    --Doug.
Sign In or Register to comment.